This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
template<typename T>
struct fenwick {
vector<T> fenw;
int n;
fenwick(int n_) : n(n_) {
fenw.resize(n);
}
void upd(int id, int x) {
while (id < n) {
fenw[id] += x;
id |= id + 1;
}
}
T qry(int id) {
T s = 0;
while (id >= 0) {
s += fenw[id];
id = (id & (id + 1)) - 1;
}
return s;
}
void upd(int l, int r, int x) {
upd(l, x);
upd(r + 1, -x);
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q, s, t, p = 0;
cin >> n >> q >> s >> t;
fenwick<long long> bit(n + 1);
long long ans = 0;
for (int i = 0; i <= n; i++) {
int x;
cin >> x;
bit.upd(i, i, x);
ans += (x > p ? -s * (x - p): t * (p - x));
p = x;
}
while (q--) {
int l, r, x;
cin >> l >> r >> x;
long long a1 = bit.qry(l), a2 = (l == r ? a1 : bit.qry(r));
bit.upd(l, r, x);
long long p1 = bit.qry(l - 1), p2 = (r == n ? -1 : bit.qry(r + 1));
if (p1 < a1 && p1 >= a1 + x) ans = ans + (a1 - p1) * s + t * (p1 - a1 - x);
else if (p1 >= a1 + x) ans = ans - (p1 - a1) * t + (p1 - a1 - x) * t;
if (p1 >= a1 && p1 < a1 + x) ans = ans - (a1 + x - p1) * s - (p1 - a1) * t;
else if (p1 < a1 + x) ans = ans + (a1 - p1) * s - (a1 + x - p1) * s;
if (r != n) {
if (a2 < p2 && a2 + x >= p2) ans = ans + (p2 - a2) * s + t * (a2 + x - p2);
else if (a2 + x >= p2) ans = ans - (a2 - p2) * t + (a2 + x - p2) * t;
if (a2 >= p2 && a2 + x < p2) ans = ans - (a2 - p2) * t - (p2 - a2 - x) * s;
else if (a2 + x < p2) ans = ans + (p2 - a2) * s - (p2 - a2 - x) * s;
}
cout << ans << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |