fork download
  1. #include <iostream>
  2. #include <string.h>
  3. #include <vector>
  4. #include <set>
  5. using namespace std;
  6.  
  7. struct E{
  8. long long int num,no;
  9. bool commit;
  10. };
  11. struct E2{
  12. int l,r,x,y,z;
  13. };
  14. const int maxS=600000;
  15. int R=262143;
  16. int no0=0;
  17. vector<E2> copyData;
  18. vector<long long int> areaSum;
  19. E segA[maxS];
  20.  
  21. long long int fSegASet(int no,int p1,int l2,int r2,long long int num){
  22. if(r2<p1 || p1<l2)return segA[no].num;
  23. if(l2==r2){
  24. segA[no].num=num;
  25. return segA[no].num;
  26. }
  27. int m=(l2+r2)/2;
  28. long long int res=fSegASet(no*2+1,p1,l2,m,num);
  29. res+=fSegASet(no*2+2,p1,m+1,r2,num);
  30. segA[no].num=res;
  31. return res;
  32. }
  33.  
  34. long long int calc(int no,int no2,int no3,int l,int r,int l2,int r2,bool commit){
  35. //cout<<"<"<<commit<<" "<<no<<",no2="<<no2<<",no3="<<no3<<",l="<<l<<",r="<<r<<","<<l2<<","<<r2<<",>";
  36.  
  37. if(commit==true){
  38. //cout<<1<<" "<<segA[no].num<<endl;
  39.  
  40. return segA[no].num;
  41. }else{
  42. if(no2!=-1){
  43. E2 e2=copyData[no2];
  44. if(e2.x<=l2 && r2<e2.x+e2.z){
  45. int l3=e2.y+(l2-e2.x);
  46. int r3=l3+(r2-l2)+1;
  47. long long int t=areaSum[r3]-areaSum[l3];
  48. return t;
  49. }
  50. }
  51. E2 e2=copyData[no3];
  52. //cout<<3<<e2.x<<" "<<e2.z<<endl;
  53. return areaSum[e2.x+e2.z]-areaSum[e2.x];
  54. }
  55. }
  56. long long int calc2(int no,int no2,int no3,int l,int r,int l2,int r2,bool commit){
  57. //cout<<"<"<<commit<<" "<<no<<",no2="<<no2<<",no3="<<no3<<",l="<<l<<",r="<<r<<","<<l2<<","<<r2<<",>";
  58.  
  59. if(no2!=-1){
  60. E2 e2=copyData[no2];
  61. if(e2.x<=l2 && r2<e2.x+e2.z){
  62. int l3=e2.y+(l2-e2.x);
  63. int r3=l3+(r2-l2)+1;
  64. long long int t=areaSum[r3]-areaSum[l3];
  65. return t;
  66. }
  67. }
  68.  
  69. if(commit==true){
  70. //cout<<1<<" "<<segA[no].num<<endl;
  71.  
  72. return segA[no].num;
  73. }else{
  74.  
  75. E2 e2=copyData[no3];
  76. //cout<<3<<e2.x<<" "<<e2.z<<endl;
  77. return areaSum[e2.x+e2.z]-areaSum[e2.x];
  78. }
  79. }
  80.  
  81. long long int fSegACalcAns(int no,int l,int r,int l2,int r2,int no2,bool commit){
  82. commit=commit && segA[no].commit;
  83. no2=(no2<segA[no].no)?segA[no].no:no2;
  84. long long int res=calc(no,no2,no2,l,r,l2,r2,commit);
  85. if(r2<l || r<l2){
  86. return 0;
  87. }
  88.  
  89. if(l<=l2 && r2<=r){
  90. return res;
  91. }else{
  92. int m=(l2+r2)/2;
  93. res=fSegACalcAns(no*2+1,l,r,l2,m,no2,commit);
  94. res+=fSegACalcAns(no*2+2,l,r,m+1,r2,no2,commit);
  95. return res;
  96. }
  97. }
  98.  
  99. long long int fSegAUpData(int no,int l,int r,int l2,int r2,int no2,int no3,bool commit){
  100. commit=commit && segA[no].commit;
  101. no3=(no3<segA[no].no)?segA[no].no:no3;
  102.  
  103. long long int res=calc2(no,no2,no3,l,r,l2,r2,commit);
  104.  
  105. if(r2<l || r<l2){
  106. return res;
  107. }
  108. res=calc2(no,no2,no3,l,r,l2,r2,false);
  109. if(l2==r2){
  110. segA[no].no=no2;
  111. segA[no].num=res;
  112. return res;
  113. }
  114. if(l<=l2 && r2<=r){
  115. segA[no].no=no2;
  116. segA[no].num=res;
  117. segA[no].commit=true;
  118. segA[no*2+1].commit=false;
  119. segA[no*2+2].commit=false;
  120. return res;
  121. }else{
  122. int m=(l2+r2)/2;
  123. res=fSegAUpData(no*2+1,l,r,l2,m,no2,no3,commit);
  124. res+=fSegAUpData(no*2+2,l,r,m+1,r2,no2,no3,commit);
  125. segA[no].num=res;
  126. segA[no].commit=true;
  127. return res;
  128. }
  129. }
  130.  
  131. int main() {
  132. for(int i=0;i<maxS;i++){
  133. segA[i].num=0;
  134. segA[i].no=-1;
  135. segA[i].commit=true;
  136. }
  137. int n,m;
  138. cin>>n>>m;
  139. for(int i=0;i<n;i++){
  140. long long int num;
  141. cin>>num;
  142. fSegASet(0,i,0,R,num);
  143. }
  144. areaSum.push_back(0);
  145. long long int sum=0;
  146. for(int i=0;i<n;i++){
  147. long long int num;
  148. cin>>num;
  149. sum+=num;
  150. areaSum.push_back(sum);
  151. }
  152. for(int i=0;i<maxS;i++){
  153. areaSum.push_back(sum);
  154. }
  155.  
  156. for(int i=0;i<m;i++){
  157. int c1,l,r,x,y,z;
  158. cin>>c1;
  159. if(c1==0){
  160. cin>>x>>y>>z;
  161. E2 e2;
  162. e2.l=y-1;
  163. e2.r=y-2+z;
  164. e2.x=x-1;
  165. e2.y=y-1;
  166. e2.z=z;
  167. l=x-1;
  168. r=x-2+z;
  169. copyData.push_back(e2);
  170. fSegAUpData(0,l,r,0,R,no0,-1,true);
  171. no0++;
  172. }else{
  173. cin>>l>>r;
  174. l--;
  175. r--;
  176. long long int t=fSegACalcAns(0,l,r,0,R,-1,true);
  177. cout<<t<<endl;
  178. }
  179. //for(int i=0;i<maxS;i++){
  180. //cout<<"("<<i<<","<<segA[i].num<<")";
  181. //}
  182. //cout<<endl;
  183. }
  184. return 0;
  185. }
Success #stdin #stdout 0.01s 27260KB
stdin
6 8
1 2 3 4 5 6
2 5 6 3 4 1
0 1 1 6
0 2 3 3
1 1 1
1 1 2
1 1 3
1 1 4
1 1 5
1 1 6
stdout
1
27
30
34
38
39