ahmrf

Untitled

Nov 8th, 2015
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int MAX = 100005;
  5.  
  6. struct node {
  7.     int z, t, o, lazy; // z = zero, o = one, t = two
  8.     node() {
  9.         z = 1;
  10.         o = t = lazy = 0;
  11.     }
  12. };
  13.  
  14. struct Tree {
  15.     int N;
  16.     node tree[MAX << 2];
  17.    
  18.     Tree(int n) { N = n; }
  19.  
  20.     void build() { build(1, 1, N); }
  21.     void update(int l, int r) { update(1, 1, N, l, r); }
  22.     int query(int l, int r) { return query(1, 1, N, l, r).z; }
  23.  
  24.     node merge(node p, node q) {
  25.         node par;
  26.         par.z = p.z + q.z;
  27.         par.o = p.o + q.o;
  28.         par.t = p.t + q.t;
  29.         return par;
  30.     }
  31.  
  32.     void build(int idx, int a, int b) {
  33.         if(a == b) {
  34.             tree[idx] = node();
  35.             return;
  36.         }
  37.  
  38.         int c = (a + b) / 2, d = c + 1, x = idx << 1, y = x | 1;
  39.         build(x, a, c);
  40.         build(y, d, b);
  41.         tree[idx] = merge(tree[x], tree[y]);
  42.     }
  43.  
  44.     void getdown(int idx, int a, int b) {
  45.         tree[idx].lazy %= 3;
  46.         int z = tree[idx].z, o = tree[idx].o, t = tree[idx].t;
  47.         if(!tree[idx].lazy) return;
  48.         if(tree[idx].lazy == 1) {
  49.             tree[idx].z = t;
  50.             tree[idx].o = z;
  51.             tree[idx].t = o;
  52.         } else {
  53.             tree[idx].z = o;
  54.             tree[idx].o = t;
  55.             tree[idx].t = z;
  56.         }
  57.         if(a != b) {
  58.             int x = idx << 1, y = x | 1;
  59.             tree[x].lazy += tree[idx].lazy;
  60.             tree[y].lazy += tree[idx].lazy;
  61.         }
  62.         tree[idx].lazy = 0;
  63.     }
  64.  
  65.     void update(int idx, int a, int b, int l, int r) {
  66.         getdown(idx, a, b);
  67.         if(a == l && b == r) {
  68.             tree[idx].lazy++;
  69.             getdown(idx, a, b);
  70.             return;
  71.         }
  72.  
  73.         int c = (a + b) / 2, d = c + 1, x = idx << 1, y = x | 1;
  74.         if(r <= c) update(x, a, c, l, r);
  75.         else if(d <= l) update(y, d, b, l, r);
  76.         else {
  77.             update(x, a, c, l, c);
  78.             update(y, d, b, d, r);
  79.         }
  80.  
  81.         tree[idx] = merge(tree[x], tree[y]);
  82.     }
  83.  
  84.     node query(int idx, int a, int b, int l, int r) {
  85.         getdown(idx, a, b);
  86.         if (a == l && b == r) return tree[idx];
  87.  
  88.         int c = (a + b) / 2, d = c + 1, x = idx << 1, y = x | 1;
  89.  
  90.         if(r <= c) return query(x, a, c, l, r);
  91.         else if(d <= l) return query(y, d, b, l, r);
  92.         else return merge(query(x, a, c, l, c), query(y, d, b, d, r));
  93.     }
  94.  
  95. };
  96.  
  97. int main() {
  98.     int N, Q, type, l, r;
  99.     scanf("%d %d", &N, &Q);
  100.  
  101.     Tree tree(N);
  102.    
  103.     tree.build();
  104.    
  105.     while(Q--) {
  106.         scanf("%d %d %d", &type, &l, &r);
  107.         if(type) printf("%d\n", tree.query(++l, ++r));
  108.         else tree.update(++l, ++r);
  109.     }
  110.  
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment