Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <string>
- #include <iostream>
- #include <algorithm>
- #include <cmath>
- #include <cstdio>
- #include <cstdlib>
- #include <fstream>
- #include <map>
- #include <set>
- #include <queue>
- using namespace std;
- #pragma comment(linker,"/STACK:16777216")
- //Loops
- #define FOR(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
- #define ROF(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
- #define rep(i,n) for (int (i) = 0; (i) < (n); ++(i))
- #define fe(i,a) for (int (i) = 0; (i) < int((a).size()); ++(i))
- #define MEM(a,b) memset((a),(b),sizeof(a))
- //Constants
- #define inf 1000000000
- #define pi 2*acos(0.0)
- #define N 100010
- #define eps 1e-9
- //Functions
- #define pb push_back
- #define ppb pop_back
- #define all(c) (c).begin(), (c).end()
- #define sz(x) int((x).size())
- #define sqr(a) (a)*(a)
- //Pairs
- #define mp(a,b) make_pair((a), (b))
- #define X first
- #define Y second
- //Input-Output
- #define FREOPEN(a,b) freopen(a,"r",stdin); freopen(b,"w",stdout);
- typedef vector<int> vint;
- typedef long long ll;
- typedef pair<int, int> pii;
- struct node
- {
- int add,t1,t2;
- };
- node tree[4*N];
- int t0=0,ans=0;
- void update(int root,int tl,int tr,int l,int r)
- {
- if(tl>tr || tr<l || tl>r || l>r)return;
- if(tl>=l && tr<=r)
- {
- t0=(tr-tl+1)-tree[root].t1-tree[root].t2;
- tree[root].add++;
- tree[root].t2=tree[root].t1;
- tree[root].t1=t0;
- } else
- {
- int tm=(tl+tr)/2;
- update(root*2+1,tl,tm,l,r);
- update(root*2+2,tm+1,tr,l,r);
- tree[root].t1=tree[root*2+1].t1+tree[root*2+2].t1;
- tree[root].t2=tree[root*2+1].t2+tree[root*2+2].t2;
- if(tree[root].add % 3 == 1)
- {
- t0=(tr-tl+1)-tree[root].t1-tree[root].t2;
- tree[root].t2=tree[root].t1;
- tree[root].t1=t0;
- } else
- if(tree[root].add % 3 == 2)
- {
- t0=(tr-tl+1)-tree[root].t1-tree[root].t2;
- tree[root].t1=tree[root].t2;
- tree[root].t2=t0;
- }
- }
- }
- void query(int root,int tl,int tr,int l,int r,int a)
- {
- if(tl>tr || tl>r || tr<l || l>r)return;
- if(tl>=l && tr<=r)
- {
- if(a % 3 == 1)ans+=tree[root].t2; else
- if(a % 3 == 2)ans+=tree[root].t1; else
- ans+=(tr-tl+1)-tree[root].t1-tree[root].t2;
- } else
- {
- int tm=(tl+tr)/2;
- a+=tree[root].add;
- query(root*2+1,tl,tm,l,r,a);
- query(root*2+2,tm+1,tr,l,r,a);
- }
- }
- int main()
- {
- FREOPEN("input.txt","output.txt");
- int n,q,type,l,r;
- scanf("%d%d",&n,&q);
- rep(i,q)
- {
- scanf("%d%d%d",&type,&l,&r);
- if(!type)update(0,0,n-1,l,r); else
- {
- ans=0;
- query(0,0,n-1,l,r,0);
- printf("%d\n",ans);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement