fork download
  1. #include <bits/stdc++.h>
  2.  
  3.  
  4. #define gc getchar
  5. #define pc putchar
  6.  
  7. using namespace std;
  8.  
  9. /*#include <ext/pb_ds/tree_policy.hpp>
  10. #include <ext/pb_ds/detail/standard_policies.hpp>
  11. #include <ext/pb_ds/assoc_container.hpp>
  12.  
  13. using namespace __gnu_pbds;
  14. */
  15.  
  16. /*
  17.   two functions for policy based data structure. it is
  18.  
  19.   find_by_order() and order_of_key().
  20.  
  21.   The first returns an iterator to the k-th largest element (counting from zero),
  22.   the second returns the number of items in a set that are strictly smaller than our item
  23.  
  24. */
  25.  
  26. #define vi vector<int>
  27. #define si set<int>
  28. #define vs vector<string>
  29. #define pii pair<int,int>
  30. #define vpi vector<pii>
  31. #define pri priority_queue<int>
  32. #define rev_pri priority_queue<int,vector<int>,greater<int> >
  33. #define mpi map<int,int>
  34. #define i64 long long int
  35. #define endl '\n'
  36. #define pi acos(-1)
  37. #define all(v) v.begin(),v.end()
  38. #define pb push_back
  39. #define mp make_pair
  40. #define mod 1000000007
  41. #define For(i,n) for(int i=0;i<n;i++)
  42. #define Rep(i,x,y) for(int i=x;i<=y;i++)
  43. #define eps 1e-8
  44. #define ff first
  45. #define ss second
  46. #define mem(a,b) memset(a,b,sizeof(a))
  47. #define min3(a,b,c) min(a,min(b,c))
  48. #define max3(a,b,c) max(a,max(b,c))
  49. #define READ freopen("input.txt", "r", stdin)
  50. #define WRITE freopen("output.txt","w", stdout)
  51. #define sz size()
  52. #define dbg(x) printf("yo is %d!\n",x)
  53. #define dbg2(x,y) printf("yo is %d! and %d!\n",x,y)
  54. #define foreach(i,c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
  55. #define sqr(a) (a) * (a)
  56. #define clr clear()
  57. #define CASE(a) printf("Case %d:\n",a)
  58. #define sf(n) scanf("%d", &n)
  59. #define sff(a,b) scanf("%d %d", &a, &b)
  60. #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
  61.  
  62. //int dx[] = {0,1,0,-1};
  63. //int dy[] = {1,0,-1,0};
  64. //int dx[] = { -1, -1, 0, 1, 1, 1, 0, -1 };
  65. //int dy[] = { 0, -1, -1, -1, 0, 1, 1, 1 };
  66. //int dxK[] = { -2, -2, -1, 1, 2, 2, 1, -1 };
  67. //int dyK[] = { -1, 1, 2, 2, 1, -1, -2, -2 };
  68.  
  69. //functions
  70.  
  71. //i64 gcd(i64 a,i64 b){if(!b)return a;return gcd(b,a%b);}
  72.  
  73. //inline void fastRead(i64 *a){register char c=0;while(c<33)c=gc();*a=0;while(c>33){*a=*a*10+c-'0';c=gc();}}
  74.  
  75. //inline void fastWrite(int a){char snum[20];int i=0;do{snum[i++]=a%10+48;a=a/10;}while(a!=0);i=i-1;while(i>=0)pc(snum[i--]);pc('\n');}
  76.  
  77. //i64 bigmod(i64 num,i64 n){if(!n)return 1;i64 x=(bigmod(num,n/2)*bigmod(num,n/2))%mod;if(n%2)x=(x*num)%mod;return x;}
  78.  
  79. //i64 modinverse(i64 num){return bigmod(num,mod-2);}
  80.  
  81. //void combination(int pos,int last){if(pos==k+1){for(int i=1;i<=k;i++)cout << tem[i];cout << endl;return;}
  82. //for(int i=last+1;i<=n-k+pos;i++){tem[pos] = num[i-1];combination(pos+1,i);}}
  83. //i64 power(i64 value, i64 base){i64 result = 1;For(i,base)result *= value;return result;}
  84. //int Set(int N,int pos){return N = (1<<pos);}
  85. //int reset(int N,int pos){return N &= (~(1<<pos));}
  86. //bool check(int N,int pos){return (bool)(N & (1<<pos));}
  87.  
  88. //typedef tree< int, null_type, less < int >, rb_tree_tag, tree_order_statistics_node_update > Set;
  89.  
  90. const int N = 1e5;
  91. int n;
  92. int seg[2*N];
  93. int h;
  94. int d[N];
  95.  
  96. void apply(int p, int value)
  97. {
  98. seg[p] += value;
  99. if (p < n) d[p] += value;
  100. }
  101. void build(int p)
  102. {
  103. while (p > 1) p >>= 1, seg[p] = seg[p<<1] + seg[p<<1|1] + d[p];
  104. }
  105. void push(int p)
  106. {
  107. for (int s = h; s > 0; --s)
  108. {
  109. int i = p >> s;
  110. if (d[i] != 0)
  111. {
  112. apply(i<<1, d[i]);
  113. apply(i<<1|1, d[i]);
  114. d[i] = 0;
  115. }
  116. }
  117. }
  118. void inc(int l, int r, int value)
  119. {
  120. l += n, r += n;
  121. int l0 = l, r0 = r;
  122. for (; l < r; l >>= 1, r >>= 1)
  123. {
  124. if (l&1) apply(l++, value);
  125. if (r&1) apply(--r, value);
  126. }
  127. build(l0);
  128. build(r0 - 1);
  129. }
  130.  
  131. int query(int l, int r)
  132. {
  133. l += n, r += n;
  134. push(l);
  135. push(r - 1);
  136. int res = 0;
  137. for (; l < r; l >>= 1, r >>= 1)
  138. {
  139. if (l&1) res += seg[l++];
  140. if (r&1) res += seg[--r];
  141. }
  142. return res;
  143. }
  144.  
  145. int main()
  146. {
  147. int t;
  148. sf(t);
  149. Rep(tc,1,t)
  150. {
  151. mem(seg,0);
  152. mem(d,0);
  153. int q;
  154. sff(n,q);
  155. h = sizeof(int) * 8 - __builtin_clz(n);
  156. int x,y,v;
  157. CASE(tc);
  158. while(q--)
  159. {
  160. int a;
  161. sf(a);
  162. if(a==0)
  163. {
  164. sfff(x,y,v);
  165. inc(x,y+1,v);
  166. }
  167. else
  168. {
  169. sff(x,y);
  170. printf("%d\n", query(x,y+1));
  171. }
  172. }
  173. }
  174.  
  175. return 0;
  176. }
  177.  
Success #stdin #stdout 0s 16408KB
stdin
2

10 5

0 0 9 10

1 1 6

0 3 7 2

0 4 5 1

1 5 5

20 3

0 10 12 1

1 11 12

1 19 19
stdout
Case 1:
30
13
Case 2:
2
0