Submission #1354586

#TimeUsernameProblemLanguageResultExecution timeMemory
1354586cpismayilmmdv985Foehn Phenomena (JOI17_foehn_phenomena)C++20
30 / 100
391 ms16196 KiB
#ifdef LOCAL
#include ".debug.hpp"
#else
#define debug(...) 42
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

struct Lazy_Segment_Tree {
   // update range [l; r] +x, and find sum in range [a; b]
   int64_t n;
   vector<int64_t> tree, lazy;
   Lazy_Segment_Tree(int64_t _n) {
      n = _n + 5, tree.assign(n << 2, 0), lazy.assign(n << 2, 0); // init tree and lazy vectors are filled with 0
   }
   void relax(const int64_t &v, const int64_t &tl, const int64_t &tr) {
      if (!lazy[v])  return;
      tree[v] += (tr - tl + 1) * lazy[v];
      if (tl != tr) lazy[v << 1] += lazy[v], lazy[v << 1 | 1] += lazy[v];
      lazy[v] = 0;
   }
   void update(const int64_t &v, const int64_t &tl, const int64_t &tr, const int64_t &l, const int64_t &r, const int64_t &add) {
      relax(v, tl, tr);
      if (l > r || tl > r || l > tr)   return;
      if (l <= tl && tr <= r) {
         lazy[v] += add; relax(v, tl, tr);
         return;
      }
      int64_t tm = (tl + tr) >> 1;
      update(v << 1, tl, tm, l, min(r, tm), add); update(v << 1 | 1, tm + 1, tr, max(l, tm + 1), r, add);
      tree[v] = tree[v << 1] + tree[v << 1 | 1];
   }
   int64_t getans(const int64_t &v, const int64_t &tl, const int64_t &tr, const int64_t &l, const int64_t &r) {
      relax(v, tl, tr);
      if (l > r || tl > r || l > tr)   return 0;
      if (l <= tl && tr <= r)          return tree[v];
      int64_t tm = (tl + tr) >> 1;
      return getans(v << 1, tl, tm, l, min(r, tm)) + getans(v << 1 | 1, tm + 1, tr, max(l, tm + 1), r);
   }
};

signed main() {
#ifndef VOID
   cin.tie(nullptr)->sync_with_stdio(false);
#endif

   int64_t N, Q, S, T, curr = 0;   cin >> N >> Q >> S >> T;
   Lazy_Segment_Tree segtree(N + 5);
   auto cal = [&](const int64_t &val1, const int64_t &val2) -> int64_t {
      return (val1 - val2) * (val2 > val1 ? S : T);
   };
   vector<int> A(N + 1);   for (int i = 0; i <= N; i++) {
      cin >> A[i]; 
      if (i)   segtree.update(1, 1, N, i, i, A[i]), curr += cal(A[i - 1], A[i]);
   }
   while (Q-- > 0) {
      int64_t L, R, X;   cin >> L >> R >> X;
      int el1 = segtree.getans(1, 1, N, L - 1, L - 1), el2 = segtree.getans(1, 1, N, L, L);
      if (L - 1 >= 0)   curr -= cal(el1, el2);
      el1 = segtree.getans(1, 1, N, R, R), el2 = segtree.getans(1, 1, N, R + 1, R + 1);
      if (R + 1 <= N)   curr -= cal(el1, el2);
      segtree.update(1, 1, N, L, R, X);
      el1 = segtree.getans(1, 1, N, L - 1, L - 1), el2 = segtree.getans(1, 1, N, L, L);
      if (L - 1 >= 0)   curr += cal(el1, el2);
      el1 = segtree.getans(1, 1, N, R, R), el2 = segtree.getans(1, 1, N, R + 1, R + 1);
      if (R + 1 <= N)   curr += cal(el1, el2);
      cout << curr << '\n';
   }
      
   return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...