fork download
  1. #include <iostream>
  2. #include <string.h>
  3. #include <vector>
  4. #include <set>
  5. using namespace std;
  6. //堀江伸一 会津大学オンラインジャッジ問0437 Copy and Sum 合格コード。めっちゃ試行錯誤した
  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. 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. segA[no].commit=commit;
  60. long long int res=calc(no,no2,no2,l,r,l2,r2,commit);
  61. if(r2<l || r<l2){
  62. return 0;
  63. }
  64.  
  65. if(l<=l2 && r2<=r){
  66. return res;
  67. }else{
  68. int m=(l2+r2)/2;
  69. res=fSegACalcAns(no*2+1,l,r,l2,m,no2,commit);
  70. res+=fSegACalcAns(no*2+2,l,r,m+1,r2,no2,commit);
  71. return res;
  72. }
  73. }
  74.  
  75. long long int fSegAUpData(int no,int l,int r,int l2,int r2,int no2,int no3,bool commit){
  76. commit=commit && segA[no].commit;
  77. no3=(no3<segA[no].no)?segA[no].no:no3;
  78. segA[no].commit=commit;
  79. long long int res=calc(no,no3,no3,l,r,l2,r2,commit);
  80.  
  81. if(r2<l || r<l2){
  82. return res;
  83. }
  84. res=calc(no,no2,no2,l,r,l2,r2,false);
  85. if(l2==r2){
  86. segA[no].no=no2;
  87. segA[no].num=res;
  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,no0,-1,true);
  147. no0++;
  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.02s 26092KB
stdin
11 40
74 60 41 56 44 43 40 37 79 35 49
29 10 7 17 55 87 73 26 45 12 72
1 1 8
1 2 9
1 5 8
0 11 7 1
0 10 10 1
0 11 9 1
0 10 8 1
1 5 11
0 11 8 1
0 5 3 6
1 4 10
1 1 3
1 9 11
1 3 5
1 1 5
0 1 9 2
0 7 4 2
1 8 11
1 5 11
0 8 2 3
1 2 11
1 5 8
0 6 11 1
1 1 9
1 8 9
0 8 8 3
1 4 5
0 3 6 5
1 4 8
1 1 1
0 11 8 1
1 1 1
0 9 6 2
1 2 3
0 9 8 2
1 1 7
0 10 4 1
1 5 8
1 4 11
1 10 10
stdout
395
400
164
314
321
175
125
104
238
180
221
210
51
267
17
63
182
45
45
99
300
109
251
17