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