1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include <bits/stdc++.h> using namespace std;
const int N = 2000006;
struct node{ int turn, reset, data1, data2, data3; }seg[N*4];
void build(int l, int r, int idx){ seg[idx] = {0,0,1,0,0}; if(l==r-1) return; int mid = (l+r)/2; build(l, mid, idx*2+1), build(mid, r, idx*2+2); seg[idx].data1 = seg[idx*2+1].data1 + seg[idx*2+2].data1; }
void push(int l, int r, int idx){ if(seg[idx].reset){ seg[idx].data1 += seg[idx].data2 + seg[idx].data3; seg[idx].data2 = 0; seg[idx].data3 = 0; seg[idx].reset=0; if(r>l+1) seg[idx*2+1].reset=1, seg[idx*2+1].turn=0; if(r>l+1) seg[idx*2+2].reset=1, seg[idx*2+2].turn=0; } if(seg[idx].turn){ seg[idx].turn %= 3; if(r>l+1) seg[idx*2+1].turn+=seg[idx].turn; if(r>l+1) seg[idx*2+2].turn+=seg[idx].turn; while(seg[idx].turn-- > 0) swap(seg[idx].data1, seg[idx].data2), swap(seg[idx].data1, seg[idx].data3); seg[idx].turn = 0; } }
int count(int l, int r, int ql, int qr, int idx){ push(l,r,idx); if(l==ql && r==qr) return seg[idx].data1; int mid = (l+r)/2; if(ql>=mid) return count(mid, r, ql, qr, idx*2+2); if(qr<=mid) return count(l, mid, ql, qr, idx*2+1); return count(l, mid, ql, mid, idx*2+1) + count(mid, r, mid, qr,idx*2+2); }
void turn(int l, int r, int ml, int mr, int idx){ push(l,r,idx); int mid = (l+r)/2; if(l==ml && r==mr){ seg[idx].turn++; return; }else if(ml>=mid) turn(mid, r, ml, mr, idx*2+2); else if(mr<=mid) turn(l, mid, ml, mr, idx*2+1); else turn(l, mid, ml, mid, idx*2+1), turn(mid, r, mid, mr,idx*2+2); push(l,mid,idx*2+1); push(mid,r,idx*2+2); seg[idx].data1=seg[idx*2+1].data1+seg[idx*2+2].data1; seg[idx].data2=seg[idx*2+1].data2+seg[idx*2+2].data2; seg[idx].data3=seg[idx*2+1].data3+seg[idx*2+2].data3; }
void reset(int l, int r, int ml, int mr, int idx){ push(l,r,idx); int mid = (l+r)/2; if(l==ml && r==mr){ seg[idx].reset = 1; return; }else if(ml>=mid) reset(mid, r, ml, mr, idx*2+2); else if(mr<=mid) reset(l, mid, ml, mr, idx*2+1); else reset(l, mid, ml, mid, idx*2+1), reset(mid, r, mid, mr,idx*2+2); push(l,mid,idx*2+1); push(mid,r,idx*2+2); seg[idx].data1=seg[idx*2+1].data1+seg[idx*2+2].data1; seg[idx].data2=seg[idx*2+1].data2+seg[idx*2+2].data2; seg[idx].data3=seg[idx*2+1].data3+seg[idx*2+2].data3; }
signed main(){ int n, m; cin >> n >>m; build(0, n, 0); while(m--){ string cmd; int a, b; cin >> cmd >> a >> b; if(cmd == "TURN") turn(0, n, --a, b, 0); if(cmd == "RESET") reset(0, n, --a, b, 0); if(cmd == "COUNT") cout << count(0, n, --a, b, 0) << '\n'; } return 0; }
|