Advertisement
Guest User

Special LCM of Subarray

a guest
Jul 23rd, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <algorithm>
  2. #include <string>
  3. #include <vector>
  4. #include <queue>
  5. #include <iostream>
  6. #include <cmath>
  7. #include <sstream>
  8. #include <map>
  9. #include <set>
  10. #include <numeric>
  11. #include <memory.h>
  12. #include <cstdio>
  13. #include <assert.h>
  14.  
  15. using namespace std;
  16.  
  17. /*** TEMPLATE CODE ENDS HERE */
  18.  
  19. #define N 100010
  20. #define A 1000010
  21. #define BLOCK 555  // ~sqrt(N)
  22.  
  23. int cnt[A], a[N], ans[N], answer = 0, was_three = 0;
  24.  
  25. struct node {
  26.   int L, R, i;
  27. } q[N];
  28.  
  29. bool cmp(node x, node y) {
  30.   if (x.L / BLOCK != y.L / BLOCK) {
  31.     // different blocks, so sort by block.
  32.     return x.L / BLOCK < y.L / BLOCK;
  33.   }
  34.   // same block, so sort by R value
  35.   return x.R < y.R;
  36. }
  37.  
  38. void add(int position) {
  39.   cnt[a[position]]++;
  40.   if (cnt[a[position]] == 1) {
  41.     int coef = a[position] % 3;
  42.     if (coef) {
  43.       answer = answer * coef % 3;
  44.     } else {
  45.       was_three = 1;
  46.     }
  47.   }
  48. }
  49.  
  50. void remove(int position) {
  51.   cnt[a[position]]--;
  52.   if (cnt[a[position]] == 0) {
  53.     int coef = a[position] % 3;
  54.     if (coef) {
  55.       answer = answer * coef % 3;
  56.     } else {
  57.       was_three = 0;
  58.     }
  59.   }
  60. }
  61.  
  62. int main() {
  63. #ifdef LOCAL_HOST
  64.   freopen("input.txt", "r", stdin);
  65. // freopen("output.txt","w",stdout);
  66. #endif
  67.  
  68.   memset(cnt, 0, sizeof(cnt));
  69.  
  70.   int n, m;
  71.   scanf("%d %d", &n, &m);
  72.   for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  73.  
  74.   for (int i = 0; i < m; i++) {
  75.     scanf("%d%d", &q[i].L, &q[i].R);
  76.     q[i].L--;
  77.     q[i].R--;
  78.     q[i].i = i;
  79.   }
  80.  
  81.   sort(q, q + m, cmp);
  82.  
  83.   answer = 1;
  84.   was_three = 0;
  85.  
  86.   int currentL = 0, currentR = 0;
  87.   for (int i = 0; i < m; i++) {
  88.     int L = q[i].L, R = q[i].R;
  89.  
  90.     if (L > R) {
  91.       ans[q[i].i] = 0;
  92.       continue;
  93.     }
  94.  
  95.     while (currentL < L) {
  96.       remove(currentL);
  97.       currentL++;
  98.     }
  99.     while (currentL > L) {
  100.       add(currentL - 1);
  101.       currentL--;
  102.     }
  103.     while (currentR <= R) {
  104.       add(currentR);
  105.       currentR++;
  106.     }
  107.     while (currentR > R + 1) {
  108.       remove(currentR - 1);
  109.       currentR--;
  110.     }
  111.     ans[q[i].i] = answer * (1 - was_three);
  112.   }
  113.  
  114.   for (int i = 0; i < m; i++) printf("%d\n", ans[i]);
  115.  
  116. #ifdef LOCAL_HOST
  117.   printf("TIME: %.3lf\n", double(clock()) / CLOCKS_PER_SEC);
  118. #endif
  119.  
  120.   return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement