Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<fstream>
  5. #include<vector>
  6. using namespace std;
  7. fstream in,out;
  8. int n;
  9.  
  10. struct nodo{
  11. int m1;
  12. int m2;
  13. int m0;
  14. };
  15.  
  16. vector<nodo> tree;
  17. vector<int>lazy;
  18.  
  19. void build(int s,int d, int pos){
  20. if(s==d){
  21. tree[pos].m0=1;
  22. tree[pos].m1=0;
  23. tree[pos].m2=0;
  24. return;
  25. }
  26. int mid=(s+d)/2;
  27. build(s,mid,pos*2+1);
  28. build(mid+1,d,pos*2+2);
  29. tree[pos].m0=tree[pos*2+1].m0+tree[pos*2+2].m0;
  30. tree[pos].m1=tree[pos*2+1].m1+tree[pos*2+2].m1;
  31. tree[pos].m2=tree[pos*2+1].m2+tree[pos*2+2].m2;
  32. }
  33.  
  34. void lazyupdate(int ups,int upd,int s,int d,int pos,int delta){
  35. if(s>d){
  36. return;
  37. }
  38. int app;
  39. if(lazy[pos]%3!=0){
  40. if(lazy[pos]%3==1){
  41.  
  42. app=tree[pos].m0;
  43. tree[pos].m0=tree[pos].m2;
  44. tree[pos].m2=tree[pos].m1;
  45. tree[pos].m1=app;
  46.  
  47. }
  48. if(lazy[pos]%3==2){
  49. app=tree[pos].m0;
  50. tree[pos].m0=tree[pos].m1;
  51. tree[pos].m1=tree[pos].m2;
  52. tree[pos].m2=app;
  53.  
  54. }
  55. if(s!=d){
  56. lazy[pos*2+1]=lazy[pos];
  57. lazy[pos*2+2]=lazy[pos];
  58. }
  59.  
  60. lazy[pos]=0;
  61.  
  62. }
  63.  
  64. if(d<ups||s>upd){ //no overlap
  65. return;
  66. }
  67.  
  68. if(ups<=s&&d<=upd){ //total overlap
  69. if(delta%3==1){
  70.  
  71. app=tree[pos].m0;
  72. tree[pos].m0=tree[pos].m2;
  73. tree[pos].m2=tree[pos].m1;
  74. tree[pos].m1=app;
  75.  
  76. }
  77. if(s!=d){
  78. lazy[pos*2+1]+=delta;
  79. lazy[pos*2+2]+=delta;
  80. }
  81. lazy[pos]=0;
  82. return;
  83. }
  84.  
  85. int mid=(s+d)/2;
  86. lazyupdate(ups,upd,s,mid,pos*2+1,delta);
  87. lazyupdate(ups,upd,mid+1,d,pos*2+2,delta);
  88.  
  89. tree[pos].m0=tree[pos*2+1].m0+tree[pos*2+2].m0;
  90. tree[pos].m1=tree[pos*2+1].m1+tree[pos*2+2].m1;
  91. tree[pos].m2=tree[pos*2+1].m2+tree[pos*2+2].m2;
  92. return;
  93. }
  94.  
  95. nodo lazyquery(int qs,int qd,int s,int d, int pos){
  96. nodo fake;
  97. if(s>d){
  98. //return of fake node
  99. fake.m0=0;
  100. fake.m1=0;
  101. fake.m2=0;
  102. return fake;
  103. }
  104. if(lazy[pos]!=0){ // control of propagation
  105. int app;
  106. if(lazy[pos]%3==1){
  107. app=tree[pos].m0;
  108. tree[pos].m0=tree[pos].m2;
  109. tree[pos].m2=tree[pos].m1;
  110. tree[pos].m1=app; //update of propagation on this node
  111. }
  112.  
  113. if(lazy[pos]%3==2){
  114. app=tree[pos].m0;
  115. tree[pos].m0=tree[pos].m1;
  116. tree[pos].m1=tree[pos].m2;
  117. tree[pos].m2=app;
  118. }
  119.  
  120. if(s!=d){ //propagation of delta to the sons
  121. lazy[pos*2+1]+=lazy[pos];
  122. lazy[pos*2+2]+=lazy[pos];
  123. }
  124. lazy[pos]=0; //update lazy node to 0 (propagation is done)
  125. }
  126.  
  127. if(s>qd||d<qs){ //no overlap
  128. //return of fake node
  129. fake.m0=0;
  130. fake.m1=0;
  131. fake.m2=0;
  132. return fake;
  133. }
  134.  
  135. if(qs<=s&&d<=qd){ //total overlap
  136. return tree[pos]; //return of node
  137. }
  138.  
  139. int mid=(s+d)/2;
  140.  
  141. nodo left;
  142. nodo right;
  143. left=lazyquery(qs,qd,s,mid,pos*2+1); //sum of queries of the sons
  144. right=lazyquery(qs,qd,mid+1,d,pos*2+2);
  145. fake.m0=left.m0+right.m0;
  146. fake.m1=left.m1+right.m1;
  147. fake.m2=left.m2+right.m2;
  148. return fake;
  149. }
  150.  
  151. int main(){
  152. in.open("input.txt",ios::in);
  153. out.open("output.txt",ios::out);
  154. int q,i,qtype,x,y,f;
  155. in>>n>>q;
  156. tree.resize(4*n);
  157. lazy.resize(4*n,0);
  158. build(0,n-1,0);
  159. for(i=0;i<q;i++){
  160. in>>qtype>>x>>y;
  161. if(qtype==0){
  162. lazyupdate(x,y,0,n-1,0,1);
  163. }else{
  164. f=lazyquery(x,y,0,n-1,0).m0;
  165.  
  166. out<<f<<endl;
  167. }
  168. }
  169. in.close();
  170. out.close();
  171. return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement