fork download
  1. #include <bits/stdc++.h>
  2. #define REP(i,a,b) for(i=a;i<b;i++)
  3. #define rep(i,n) REP(i,0,n)
  4. #define ll long long
  5. #define ull unsigned ll
  6. #define MAX 100005
  7. #define gc getchar_unlocked
  8. using namespace std;
  9. ll arr[MAX],tree[4*MAX];
  10. inline void scanl(ll &x)
  11. {
  12. register int c = gc();
  13. x = 0;
  14. int neg = 0;
  15. for(;((c<48 || c>57) && c != '-');c = gc());
  16. if(c=='-') {neg=1;c=gc();}
  17. for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
  18. if(neg) x=-x;
  19. }
  20. inline void scan(int &x)
  21. {
  22. register int c = gc();
  23. x = 0;
  24. int neg = 0;
  25. for(;((c<48 || c>57) && c != '-');c = gc());
  26. if(c=='-') {neg=1;c=gc();}
  27. for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
  28. if(neg) x=-x;
  29. }
  30. inline void fastWrite(ll a)
  31. {
  32. char snum[20];
  33. int i=0;
  34. do
  35. {
  36. snum[i++]=a%10+48;
  37. a=a/10;
  38. }
  39. while(a!=0);
  40. i=i-1;
  41. while(i>=0)
  42. putchar_unlocked(snum[i--]);
  43. putchar_unlocked('\n');
  44. }
  45. void update_tree(int node,int a,int b,int i,int j,ll val)
  46. {
  47.  
  48. if(a>b || a>j || b<i)
  49. return;
  50. if(a==b)
  51. {
  52. tree[node]+=val;
  53. return;
  54. }
  55. update_tree(node*2,a,(a+b)/2,i,j,val);
  56. update_tree(node*2+1,(a+b)/2+1,b,i,j,val);
  57. tree[node]=(tree[node*2]+tree[node*2+1]);
  58. }
  59. ll query_tree(int node,int a,int b,int i,int j)
  60. {
  61. if(a>b || a>j || b<i)
  62. return 0;
  63. if(a>=i && b<=j)
  64. {
  65. return tree[node];
  66. }
  67. return query_tree(node*2,a,(a+b)/2,i,j)+query_tree(node*2+1,(a+b)/2+1,b,i,j);
  68. }
  69. int main()
  70. {
  71. int t,i,n,c,ch,l,r;
  72. ll val;
  73. scan(t);
  74. while(t--)
  75. {
  76. scan(n);
  77. scan(c);
  78. rep(i,n)
  79. arr[i]=0;
  80. while(c--)
  81. {
  82. scan(ch);
  83. scan(l);
  84. scan(r);
  85. --l;--r;
  86. if(ch==0)
  87. {
  88. scanl(val);
  89. update_tree(1,0,n-1,l,r,val);
  90. }
  91. else
  92. {
  93. fastWrite(query_tree(1,0,n-1,l,r));
  94. }
  95. }
  96. }
  97. return 0;
  98. }
Success #stdin #stdout 0s 7052KB
stdin
1
8 6
0 2 4 26
0 4 8 80
0 4 5 20
1 8 8 
0 5 7 14
1 4 8
stdout
80
508