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