Advertisement
haufont

Untitled

Jul 23rd, 2016
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<iostream>
  3. #include<set>
  4. #include<vector>
  5. #include<algorithm>
  6. #include<set>
  7. #include<vector>
  8. #include<string>
  9. #include<stdlib.h>
  10. #include<math.h>
  11. #include<stdio.h>
  12. using namespace std;
  13. long long U[500013];
  14. long long tr[1000013];
  15. long long up[1000013];
  16. long long sumq[500013];
  17. void push(long long v,long long l, long long r)
  18. {
  19. if (l > r)
  20. {
  21. return;
  22. }
  23. if (l == r)
  24. {
  25. tr[v] += up[v];
  26. up[v] = 0;
  27. return;
  28. }
  29. up[2 * v] += up[v];
  30. up[2 * v + 1] += up[v];
  31. tr[v] += up[v] * (r - l + 1);
  32. up[v] = 0;
  33. }
  34. void build(long long v, long long l, long long r)
  35. {
  36. if (l > r)
  37. {
  38. return;
  39. }
  40. if (l == r)
  41. {
  42. tr[v] = U[l];
  43. return;
  44. }
  45. long long m = (l + r) / 2;
  46. build(2 * v, l, m);
  47. build(2 * v + 1, m + 1, r);
  48. tr[v] = tr[2 * v] + tr[2 * v + 1];
  49. }
  50. long long sum(long long v, long long l, long long r, long long l1, long long r1)
  51. {
  52. push(v, l, r);
  53. if (l > r)
  54. {
  55. return 0;
  56. }
  57. if (l1 > r)
  58. {
  59. return 0;
  60. }
  61. if (r1 < l)
  62. {
  63. return 0;
  64. }
  65. if ((l1 == l) && (r1 == r))
  66. {
  67. return tr[v];
  68. }
  69.  
  70. long long x, y;
  71. long long m = (l + r) / 2;
  72. x = sum(2 * v, l, m, l1, min(m, r1));
  73. y = sum(2 * v + 1, m + 1, r, max(m+1,l1), r1);
  74. push(v, l, r);
  75. return x + y;
  76. }
  77. void upd_slo(long long v, long long l, long long r, long long l1, long long r1,long long x)
  78. {
  79. push(v, l, r);
  80. if (l > r)
  81. {
  82. return;
  83. }
  84. if (l1 > r)
  85. {
  86. return;
  87. }
  88. if (r1 < l)
  89. {
  90. return;
  91. }
  92. if ((l1 == l) && (r1 == r))
  93. {
  94. up[v] += x;
  95. push(v, l1, r1);
  96. return;
  97. }
  98. long long m = (l + r) / 2;
  99. upd_slo(2 * v, l, m, l1, min(m, r1), x);
  100. upd_slo(2 * v + 1, m + 1, r, max(m + 1, l1), r1, x);
  101. push(v, l, r);
  102. tr[v] = tr[2 * v] + tr[2 * v + 1];
  103. return;
  104. }
  105. int main()
  106. {
  107. int a,b;
  108. cin >> a >> b;
  109. for (int i = 1; i <= a; i++)
  110. {
  111. cin >> U[i];
  112. }
  113. build(1, 1, a);
  114. char y;
  115. long long l;
  116. long long r;
  117. long long x;
  118. long long f = 0;
  119. for (int i = 0; i < b; i++)
  120. {
  121. cin >> y;
  122. if (y == 'U')
  123. {
  124. cin >> l;
  125. cin >> r;
  126. cin >> x;
  127. upd_slo(1, 1, a, l, r, x);
  128. }
  129. if (y == 'S')
  130. {
  131. cin >> l;
  132. cin >> r;
  133. sumq[f]=sum(1, 1, a, l, r);
  134. f++;
  135. }
  136. }
  137. for (int i = 0; i < f; i++)
  138. {
  139. cout << sumq[i] << endl;
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement