Salvens

G

Aug 3rd, 2023
925
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <array>
  5. #include <set>
  6.  
  7. using namespace std;
  8.  
  9. #define int long long
  10.  
  11. const long long INF = 1e18 + 7;
  12. const int MAXN = 1e5 + 100;
  13. const int N = 1e5 + 10;
  14. const int MOD = 1e9 + 7;
  15.  
  16. array<int, 4 * MAXN> tree, upd, simple;
  17. array<int, MAXN> a;
  18.  
  19. void build(int x, int lx, int rx) {
  20.     if (lx + 1 == rx) {
  21.         tree[x] = a[lx] * (lx + 1);
  22.         simple[x] = a[lx];
  23.         return;
  24.     }
  25.     int mid = (rx + lx) / 2;
  26.     build(2 * x + 1, lx, mid);
  27.     build(2 * x + 2, mid, rx);
  28.     tree[x] = tree[2 * x + 1] + tree[2 * x + 2];
  29.     simple[x] = simple[2 * x + 1] + simple[2 * x + 2];
  30. }
  31.  
  32. void push(int x, int lx, int rx) {
  33.     if (lx + 1 != rx) {
  34.         upd[2 * x + 1] += upd[x];
  35.         upd[2 * x + 2] += upd[x];
  36.     }
  37.     int a1 = lx + 1, an = rx, n = rx - lx;
  38.     tree[x] += upd[x] * (((a1 + an) * n) / 2);
  39.     simple[x] += (rx - lx) * upd[x];
  40.     upd[x] = 0;
  41. }
  42.  
  43. void update(int x, int lx, int rx, int l, int r, int d) {
  44.     push(x, lx, rx);
  45.     if (lx >= r || rx <= l) {
  46.         return;
  47.     }
  48.     if (lx >= l && rx <= r) {
  49.         upd[x] += d;
  50.         push(x, lx, rx);
  51.         return;
  52.     }
  53.     int mid = (rx + lx) / 2;
  54.     update(2 * x + 1, lx, mid, l, r, d);
  55.     update(2 * x + 2, mid, rx, l, r, d);
  56.     tree[x] = tree[2 * x + 1] + tree[2 * x + 2];
  57.     simple[x] = simple[2 * x + 1] + simple[2 * x + 2];
  58. }
  59.  
  60. int sum(int x, int lx, int rx, int l, int r) {
  61.     push(x, lx, rx);
  62.     if (lx >= r || rx <= l) {
  63.         return 0;
  64.     }
  65.     if (lx >= l && rx <= r) {
  66.         return tree[x] - simple[x] * l;
  67.     }
  68.     int mid = (rx + lx) / 2;
  69.     return sum(2 * x + 1, lx, mid, l, r) + sum(2 * x + 2, mid, rx, l, r);
  70. }
  71.  
  72. signed main() {
  73.     ios_base::sync_with_stdio(false);
  74.     cin.tie(nullptr);
  75.     cout.tie(nullptr);
  76.     int n, m;
  77.     cin >> n >> m;
  78.     for (int i = 0; i < n; ++i) {
  79.         cin >> a[i];
  80.     }
  81.     build(0, 0, n);
  82.     for (int i = 0; i < m; ++i) {
  83.         int type, l, r;
  84.         cin >> type >> l >> r;
  85.         --l;
  86.         if (type == 1) {
  87.             int d;
  88.             cin >> d;
  89.             update(0, 0, n, l, r, d);
  90.         } else {
  91.             cout << sum(0, 0, n, l, r) << '\n';
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment