Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<stdio.h>
- #include<stdlib.h>
- #include<fstream>
- #include<vector>
- using namespace std;
- fstream in,out;
- int n;
- struct nodo{
- int m1;
- int m2;
- int m0;
- };
- vector<nodo> tree;
- vector<int>lazy;
- void build(int s,int d, int pos){
- if(s==d){
- tree[pos].m0=1;
- tree[pos].m1=0;
- tree[pos].m2=0;
- return;
- }
- int mid=(s+d)/2;
- build(s,mid,pos*2+1);
- build(mid+1,d,pos*2+2);
- tree[pos].m0=tree[pos*2+1].m0+tree[pos*2+2].m0;
- tree[pos].m1=tree[pos*2+1].m1+tree[pos*2+2].m1;
- tree[pos].m2=tree[pos*2+1].m2+tree[pos*2+2].m2;
- }
- void lazyupdate(int ups,int upd,int s,int d,int pos,int delta){
- if(s>d){
- return;
- }
- int app;
- if(lazy[pos]%3!=0){
- if(lazy[pos]%3==1){
- app=tree[pos].m0;
- tree[pos].m0=tree[pos].m2;
- tree[pos].m2=tree[pos].m1;
- tree[pos].m1=app;
- }
- if(lazy[pos]%3==2){
- app=tree[pos].m0;
- tree[pos].m0=tree[pos].m1;
- tree[pos].m1=tree[pos].m2;
- tree[pos].m2=app;
- }
- if(s!=d){
- lazy[pos*2+1]=lazy[pos];
- lazy[pos*2+2]=lazy[pos];
- }
- lazy[pos]=0;
- }
- if(d<ups||s>upd){ //no overlap
- return;
- }
- if(ups<=s&&d<=upd){ //total overlap
- if(delta%3==1){
- app=tree[pos].m0;
- tree[pos].m0=tree[pos].m2;
- tree[pos].m2=tree[pos].m1;
- tree[pos].m1=app;
- }
- if(s!=d){
- lazy[pos*2+1]+=delta;
- lazy[pos*2+2]+=delta;
- }
- lazy[pos]=0;
- return;
- }
- int mid=(s+d)/2;
- lazyupdate(ups,upd,s,mid,pos*2+1,delta);
- lazyupdate(ups,upd,mid+1,d,pos*2+2,delta);
- tree[pos].m0=tree[pos*2+1].m0+tree[pos*2+2].m0;
- tree[pos].m1=tree[pos*2+1].m1+tree[pos*2+2].m1;
- tree[pos].m2=tree[pos*2+1].m2+tree[pos*2+2].m2;
- return;
- }
- nodo lazyquery(int qs,int qd,int s,int d, int pos){
- nodo fake;
- if(s>d){
- //return of fake node
- fake.m0=0;
- fake.m1=0;
- fake.m2=0;
- return fake;
- }
- if(lazy[pos]!=0){ // control of propagation
- int app;
- if(lazy[pos]%3==1){
- app=tree[pos].m0;
- tree[pos].m0=tree[pos].m2;
- tree[pos].m2=tree[pos].m1;
- tree[pos].m1=app; //update of propagation on this node
- }
- if(lazy[pos]%3==2){
- app=tree[pos].m0;
- tree[pos].m0=tree[pos].m1;
- tree[pos].m1=tree[pos].m2;
- tree[pos].m2=app;
- }
- if(s!=d){ //propagation of delta to the sons
- lazy[pos*2+1]+=lazy[pos];
- lazy[pos*2+2]+=lazy[pos];
- }
- lazy[pos]=0; //update lazy node to 0 (propagation is done)
- }
- if(s>qd||d<qs){ //no overlap
- //return of fake node
- fake.m0=0;
- fake.m1=0;
- fake.m2=0;
- return fake;
- }
- if(qs<=s&&d<=qd){ //total overlap
- return tree[pos]; //return of node
- }
- int mid=(s+d)/2;
- nodo left;
- nodo right;
- left=lazyquery(qs,qd,s,mid,pos*2+1); //sum of queries of the sons
- right=lazyquery(qs,qd,mid+1,d,pos*2+2);
- fake.m0=left.m0+right.m0;
- fake.m1=left.m1+right.m1;
- fake.m2=left.m2+right.m2;
- return fake;
- }
- int main(){
- in.open("input.txt",ios::in);
- out.open("output.txt",ios::out);
- int q,i,qtype,x,y,f;
- in>>n>>q;
- tree.resize(4*n);
- lazy.resize(4*n,0);
- build(0,n-1,0);
- for(i=0;i<q;i++){
- in>>qtype>>x>>y;
- if(qtype==0){
- lazyupdate(x,y,0,n-1,0,1);
- }else{
- f=lazyquery(x,y,0,n-1,0).m0;
- out<<f<<endl;
- }
- }
- in.close();
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement