fork(1) download
  1. /*
  2. ID: allen.c1
  3. PROG: HORRIBLE
  4. LANG: C++
  5.  */
  6. #include<iostream>
  7. #include<cstdio>
  8. #include<algorithm>
  9. #include<vector>
  10. #include<set>
  11. #include<utility>
  12. #include<cstring>
  13. #include<stack>
  14. #include<queue>
  15. #include<map>
  16. #include<deque>
  17. #include<cmath>
  18. #include<map>
  19.  
  20. using namespace std;
  21.  
  22. #define rep(i, n) for(__typeof(n) i = 0; i < (n); i++)
  23. #define pi 3.1415926535897
  24.  
  25. typedef long long LL;
  26. typedef pair<int, int> pii;
  27. typedef pair<pii, int> edge;
  28.  
  29. const int maxn = 1 << 18;
  30. LL seg[2 * maxn];
  31. LL lazy[2 * maxn];
  32. LL mat[maxn];
  33. int qa, qb;
  34. LL qv;
  35. int t, n, m;
  36.  
  37. void down(int rt, int a, int b) {
  38. if (lazy[rt]) {
  39. if (a < b) {
  40. lazy[rt * 2] += lazy[rt];
  41. lazy[rt * 2 + 1] += lazy[rt];
  42.  
  43. int mid = (a + b) / 2;
  44. seg[rt * 2] += lazy[rt] * LL(mid - a + 1);
  45. seg[rt * 2 + 1] += lazy[rt] * LL(b - mid);
  46. }
  47. lazy[rt] = 0;
  48. }
  49.  
  50. }
  51.  
  52. void update(int rt, int a, int b) {
  53. if (a > qb || b < qa) return;
  54. if (qa <= a && b <= qb) {
  55. seg[rt] += qv * LL(b - a + 1);
  56. lazy[rt] += qv;
  57. return;
  58. }
  59. down(rt, a, b);
  60.  
  61. int mid = (a + b) / 2;
  62. update(rt * 2, a, mid);
  63. update(rt * 2 + 1, mid + 1, b);
  64. seg[rt] = seg[rt * 2] + seg[rt * 2 + 1];
  65. }
  66.  
  67. LL query(int rt, int a, int b) {
  68. if (a > qb || b < qa) return 0;
  69. if (qa <= a && b <= qb) return seg[rt];
  70. down(rt, a, b);
  71.  
  72. int mid = (a + b) / 2;
  73. return query(rt * 2, a, mid) + query(rt * 2 + 1, mid + 1, b);
  74. }
  75.  
  76. int main() {
  77. scanf("%d", &t);
  78. for (int tests = 0; tests < t; tests++) {
  79. memset(seg, 0, sizeof(seg));
  80. memset(lazy, 0, sizeof(lazy));
  81. scanf("%d%d", &n, &m);
  82. for (int i = 0; i < m; i++) {
  83. int q;
  84. scanf("%d", &q);
  85. if (q == 0) {
  86. scanf("%d%d%I64d", &qa, &qb, &qv);
  87. update(1, 1, n);
  88. }
  89. else if (q == 1) {
  90. scanf("%d%d", &qa, &qb);
  91. cout << query(1, 1, n) << endl;
  92. }
  93. }
  94. cout << endl;
  95. }
  96.  
  97. }
  98.  
Success #stdin #stdout 0s 13536KB
stdin
Standard input is empty
stdout
Standard output is empty