Advertisement
a53

addk

a53
Jan 4th, 2022
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int N=200010;
  5. int n,k,q,u[15];
  6. int64_t a[N],b[N],v[N],U[15];
  7. int64_t S(int p,int64_t *AIB)
  8. {
  9. int64_t ret=0;
  10. for(; p; p-=p&(-p))ret+=AIB[p];
  11. return ret;
  12. }
  13. void upd(int p,int64_t *AIB,int64_t val)
  14. {
  15. for(; p<=n; p+=p&(-p))AIB[p]+=val;
  16. }
  17.  
  18. void upd(int p,int64_t newVal)
  19. {
  20.  
  21. upd(p,b,1LL*p*(newVal-v[p]));
  22. upd(p,a,1LL*(newVal-v[p]));
  23. v[p]=newVal;
  24. }
  25. void upd()
  26. {
  27. for(int i=1;i<=k;i++)
  28. cin>>u[i];
  29. for(int i=1;i<k;i++)
  30. U[i]=v[u[i+1]];
  31. U[k]=v[u[1]];
  32. for(int i=1;i<=k;i++)
  33. upd(u[i],U[i]);
  34.  
  35. }
  36. int64_t trapez(int st,int dr)
  37. {
  38. return S(dr,b)-S(st-1,b);
  39. }
  40. int64_t dreptunghi(int st,int dr,int h)
  41. {
  42. return h*(S(dr,a)-S(st-1,a));
  43. }
  44. int64_t triunghi(int st,int dr)
  45. {
  46. return trapez(st,dr)-dreptunghi(st,dr,st-1);
  47. }
  48. int64_t r_triunghi(int st,int dr)
  49. {
  50. return dreptunghi(st,dr,dr+1)-trapez(st,dr);
  51. }
  52. int64_t solution(int64_t L,int64_t R,int64_t M)
  53. {
  54. int64_t H;
  55. if(R-L+1<=2*M-1)
  56. H=R-L-M+2;
  57. else
  58. H=M;
  59. return triunghi(L,L+H-2)+dreptunghi(L+H-1,R-H+1,H)+r_triunghi(R-H+2,R);
  60. }
  61.  
  62. int main()
  63. {
  64. cin>>n>>k;
  65. for(int i=1;i<=n;i++)
  66. {
  67. int64_t x;
  68. cin>>x;
  69. upd(i,x);
  70. }
  71. cin>>q;
  72. for(;q;q--)
  73. {
  74. int t;
  75. cin>>t;
  76. if(t==1)
  77. upd();
  78. else
  79. {
  80. int64_t L,R,M;
  81. cin>>L>>R>>M;
  82. cout<<solution(L,R,M)<<'\n';
  83. }
  84. }
  85.  
  86. return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement