Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int MAX = 100005;
- struct node {
- int z, t, o, lazy; // z = zero, o = one, t = two
- node() {
- z = 1;
- o = t = lazy = 0;
- }
- };
- struct Tree {
- int N;
- node tree[MAX << 2];
- Tree(int n) { N = n; }
- void build() { build(1, 1, N); }
- void update(int l, int r) { update(1, 1, N, l, r); }
- int query(int l, int r) { return query(1, 1, N, l, r).z; }
- node merge(node p, node q) {
- node par;
- par.z = p.z + q.z;
- par.o = p.o + q.o;
- par.t = p.t + q.t;
- return par;
- }
- void build(int idx, int a, int b) {
- if(a == b) {
- tree[idx] = node();
- return;
- }
- int c = (a + b) / 2, d = c + 1, x = idx << 1, y = x | 1;
- build(x, a, c);
- build(y, d, b);
- tree[idx] = merge(tree[x], tree[y]);
- }
- void getdown(int idx, int a, int b) {
- tree[idx].lazy %= 3;
- int z = tree[idx].z, o = tree[idx].o, t = tree[idx].t;
- if(!tree[idx].lazy) return;
- if(tree[idx].lazy == 1) {
- tree[idx].z = t;
- tree[idx].o = z;
- tree[idx].t = o;
- } else {
- tree[idx].z = o;
- tree[idx].o = t;
- tree[idx].t = z;
- }
- if(a != b) {
- int x = idx << 1, y = x | 1;
- tree[x].lazy += tree[idx].lazy;
- tree[y].lazy += tree[idx].lazy;
- }
- tree[idx].lazy = 0;
- }
- void update(int idx, int a, int b, int l, int r) {
- getdown(idx, a, b);
- if(a == l && b == r) {
- tree[idx].lazy++;
- getdown(idx, a, b);
- return;
- }
- int c = (a + b) / 2, d = c + 1, x = idx << 1, y = x | 1;
- if(r <= c) update(x, a, c, l, r);
- else if(d <= l) update(y, d, b, l, r);
- else {
- update(x, a, c, l, c);
- update(y, d, b, d, r);
- }
- tree[idx] = merge(tree[x], tree[y]);
- }
- node query(int idx, int a, int b, int l, int r) {
- getdown(idx, a, b);
- if (a == l && b == r) return tree[idx];
- int c = (a + b) / 2, d = c + 1, x = idx << 1, y = x | 1;
- if(r <= c) return query(x, a, c, l, r);
- else if(d <= l) return query(y, d, b, l, r);
- else return merge(query(x, a, c, l, c), query(y, d, b, d, r));
- }
- };
- int main() {
- int N, Q, type, l, r;
- scanf("%d %d", &N, &Q);
- Tree tree(N);
- tree.build();
- while(Q--) {
- scanf("%d %d %d", &type, &l, &r);
- if(type) printf("%d\n", tree.query(++l, ++r));
- else tree.update(++l, ++r);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment