Advertisement
Guest User

Special LCM of Subarray

a guest
Jul 23rd, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 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.   const int x = a[position];
  40.   cnt[x]++;
  41.   int coef = x % 3;
  42.   if (!coef) {
  43.     was_three ^= 1;
  44.   } else {
  45.     answer = answer * coef % 3;
  46.   }
  47. }
  48.  
  49. void remove(int position) {
  50.   const int x = a[position];
  51.   cnt[x]--;
  52.   int coef = x % 3;
  53.   if (coef) {
  54.     answer = answer * coef % 3;
  55.   } else {
  56.     was_three ^= 1;
  57.   }
  58. }
  59.  
  60. int main() {
  61. #ifdef LOCAL_HOST
  62.   freopen("input.txt", "r", stdin);
  63. // freopen("output.txt","w",stdout);
  64. #endif
  65.  
  66.   memset(cnt, 0, sizeof(cnt));
  67.  
  68.   int n, m;
  69.   scanf("%d %d", &n, &m);
  70.   for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  71.  
  72.   for (int i = 0; i < m; i++) {
  73.     scanf("%d%d", &q[i].L, &q[i].R);
  74.     q[i].L--;
  75.     q[i].R--;
  76.     q[i].i = i;
  77.   }
  78.  
  79.   sort(q, q + m, cmp);
  80.  
  81.   answer = 1;
  82.   was_three = 0;
  83.  
  84.   int currentL = 0, currentR = 0;
  85.   for (int i = 0; i < m; i++) {
  86.     int L = q[i].L, R = q[i].R;
  87.  
  88.     if (L > R) {
  89.       ans[q[i].i] = 0;
  90.       continue;
  91.     }
  92.  
  93.     while (currentL < L) {
  94.       remove(currentL);
  95.       currentL++;
  96.     }
  97.     while (currentL > L) {
  98.       add(currentL - 1);
  99.       currentL--;
  100.     }
  101.     while (currentR <= R) {
  102.       add(currentR);
  103.       currentR++;
  104.     }
  105.     while (currentR > R + 1) {
  106.       remove(currentR - 1);
  107.       currentR--;
  108.     }
  109.     ans[q[i].i] = answer * (1 - was_three);
  110.   }
  111.  
  112.   for (int i = 0; i < m; i++) printf("%d\n", ans[i]);
  113.  
  114. #ifdef LOCAL_HOST
  115.   printf("TIME: %.3lf\n", double(clock()) / CLOCKS_PER_SEC);
  116. #endif
  117.  
  118.   return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement