Submission #824223

#TimeUsernameProblemLanguageResultExecution timeMemory
824223taherFoehn Phenomena (JOI17_foehn_phenomena)C++17
100 / 100
117 ms7228 KiB
#include <bits/stdc++.h>

using namespace std;

template <typename T>
class fenwick {
  public:
  vector<T> fenw;
  int n;

  fenwick(int _n) : n(_n) {
    fenw.resize(n);
  }

  void modify(int x, T v) {
    while (x < n) {
      fenw[x] += v;
      x |= (x + 1);
    }
  }

  T get(int x) {
    T v{};
    while (x >= 0) {
      v += fenw[x];
      x = (x & (x + 1)) - 1;
    }
    return v;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q, s, t;
  cin >> n >> q >> s >> t;
  ++n;

  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long res = 0;

  fenwick<long long> fenw(n + 1);
  auto Contribution = [&](int i) {
    long long la = a[i] + fenw.get(i);
    long long lb = a[i - 1] + fenw.get(i - 1);
    if (lb < la) {
      return -1LL * (s * (la - lb));
    } else {
      return +1LL * (t * (lb - la));
    }
  };

  for (int i = 1; i < n; i++) {
    res += Contribution(i);
  }

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;

    if (r + 1 < n) {
      res -= Contribution(r + 1);  
    }
    res -= Contribution(l);

    long long y;
    cin >> y;
    fenw.modify(l, +y);
    fenw.modify(r + 1, -y);

    if (r + 1 < n) {
      res += Contribution(r + 1);
    }
    res += Contribution(l);

    cout << res << '\n';
  }
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...