fork(2) 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 no2=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. E2 e2=copyData[no2];
  43. if(e2.x<=l2 && r2<=e2.x+e2.z){
  44. int l3=e2.y+(l2-e2.x);
  45. int r3=l3+(r2-l2)+1;
  46. long long int t=areaSum[r3]-areaSum[l3];
  47. return t;
  48. }
  49. e2=copyData[no3];
  50. //cout<<3<<e2.x<<" "<<e2.z<<endl;
  51. return areaSum[e2.x+e2.z]-areaSum[e2.x];
  52. }
  53. }
  54.  
  55.  
  56. long long int fSegACalcAns(int no,int l,int r,int l2,int r2,int no2,bool commit){
  57. commit=commit && segA[no].commit;
  58. no2=(no2<segA[no].no)?segA[no].no:no2;
  59. long long int res=calc(no,no2,no2,l,r,l2,r2,commit);
  60. if(r2<l || r<l2)return 0;
  61.  
  62. if(l2==r2){
  63.  
  64. return res;
  65. }
  66. if(l<=l2 && r2<=r){
  67. return res;
  68. }else{
  69. int m=(l2+r2)/2;
  70. res=fSegACalcAns(no*2+1,l,r,l2,m,no2,commit);
  71. res+=fSegACalcAns(no*2+2,l,r,m+1,r2,no2,commit);
  72. return res;
  73. }
  74. }
  75.  
  76. long long int fSegAUpData(int no,int l,int r,int l2,int r2,int no2,int no3,bool commit){
  77. commit=commit && segA[no].commit;
  78. no3=(no3<segA[no].no)?segA[no].no:no3;
  79. long long int res=calc(no,no2,no3,l,r,l2,r2,commit);
  80. if(r2<l || r<l2)return res;
  81. res=calc(no,no2,no3,l,r,l2,r2,false);
  82. segA[no].commit=true;
  83. if(l2==r2){
  84.  
  85. segA[no].no=no2;
  86. segA[no].num=res;
  87. segA[no].commit=true;
  88. return res;
  89. }
  90. if(l<=l2 && r2<=r){
  91. segA[no].no=no2;
  92. segA[no].num=res;
  93. segA[no].commit=true;
  94. segA[no*2+1].commit=false;
  95. segA[no*2+2].commit=false;
  96. return res;
  97. }else{
  98. int m=(l2+r2)/2;
  99. res=fSegAUpData(no*2+1,l,r,l2,m,no2,no3,commit);
  100. res+=fSegAUpData(no*2+2,l,r,m+1,r2,no2,no3,commit);
  101. segA[no].num=res;
  102. segA[no].commit=true;
  103. return res;
  104. }
  105. }
  106.  
  107. int main() {
  108. for(int i=0;i<maxS;i++){
  109. segA[i].num=0;
  110. segA[i].no=-1;
  111. segA[i].commit=true;
  112. }
  113. int n,m;
  114. cin>>n>>m;
  115. for(int i=0;i<n;i++){
  116. long long int num;
  117. cin>>num;
  118. fSegASet(0,i,0,R,num);
  119. }
  120. areaSum.push_back(0);
  121. long long int sum=0;
  122. for(int i=0;i<n;i++){
  123. long long int num;
  124. cin>>num;
  125. sum+=num;
  126. areaSum.push_back(sum);
  127. }
  128. for(int i=0;i<maxS;i++){
  129. areaSum.push_back(sum);
  130. }
  131.  
  132. for(int i=0;i<m;i++){
  133. int c1,l,r,x,y,z;
  134. cin>>c1;
  135. if(c1==0){
  136. cin>>x>>y>>z;
  137. E2 e2;
  138. e2.l=y-1;
  139. e2.r=y-2+z;
  140. e2.x=x-1;
  141. e2.y=y-1;
  142. e2.z=z;
  143. l=x-1;
  144. r=x-2+z;
  145. copyData.push_back(e2);
  146. fSegAUpData(0,l,r,0,R,no2,-1,true);
  147. no2++;
  148. }else{
  149. cin>>l>>r;
  150. l--;
  151. r--;
  152. long long int t=fSegACalcAns(0,l,r,0,R,-1,true);
  153. cout<<t<<endl;
  154. }
  155. //for(int i=0;i<maxS;i++){
  156. //cout<<"("<<i<<","<<segA[i].num<<")";
  157. //}
  158. //cout<<endl;
  159. }
  160. return 0;
  161. }
Success #stdin #stdout 0.01s 26272KB
stdin
10 36
9 9 11 4 3 6 11 11 8 2
7 3 2 1 3 8 9 5 11 6
0 7 5 2
1 3 7
1 4 9
0 3 7 1
0 5 6 2
1 7 7
0 9 3 1
1 7 9
0 1 1 3
1 4 8
0 4 1 3
1 3 6
1 0 3
1 10 10
1 5 10
1 1 1
0 5 10 1
0 1 4 4
1 5 6
1 1 1
1 2 2
1 3 3
1 4 4
1 5 5
1 6 6
1 7 7
1 8 8
1 9 9
1 10 10
0 1 1 10
1 1 10
1 1 6
0 1 4 3
1 1 6
0 3 1 3
1 1 6
stdout
27
32
3
13
32
14
12
2
20
7
6
1
3
8
9
6
2
3
8
2
2
55
24
32
17