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
struct SegTree {
int n;
vector<int> st, add;
void push(int v, int vl, int vr) {
st[v] += add[v] * (vr - vl + 1);
if (vl != vr) {
add[v * 2] += add[v];
add[v * 2 + 1] += add[v];
}
add[v] = 0;
}
void update(int l, int r, int x, int v, int vl, int vr) {
push(v, vl, vr);
if (r < vl || vr < l) return;
if (l <= vl && vr <= r) {
add[v] += x;
push(v, vl, vr);
return;
}
int vm = (vl + vr) / 2;
update(l, r, x, v * 2, vl, vm);
update(l, r, x, v * 2 + 1, vm + 1, vr);
st[v] = st[v * 2] + st[v * 2 + 1];
}
SegTree(int n, vector<int> &a) {
this->n = n;
st.resize(4 * n);
add.resize(4 * n);
for (int i = 0; i < n; i++) {
update(i, i, a[i], 1, 0, n - 1);
}
}
int get(int l, int r, int v, int vl, int vr) {
push(v, vl, vr);
if (r < vl || vr < l) return 0;
if (l <= vl && vr <= r) return st[v];
int vm = (vl + vr) / 2;
return get(l, r, v * 2, vl, vm) + get(l, r, v * 2 + 1, vm + 1, vr);
}
int get(int pos) {
return get(pos, pos, 1, 0, n - 1);
}
};
int f(int x, int y, int s, int t) {
return (x < y ? s * (x - y) : t * (x - y));
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q, s, t;
cin >> n >> q >> s >> t;
n++;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
SegTree st(n, a);
int ans = 0;
for (int i = 0; i < n - 1; i++) {
ans += f(a[i], a[i + 1], s, t);
}
while (q--) {
int l, r, x;
cin >> l >> r >> x;
ans -= f(st.get(l - 1), st.get(l), s, t);
if (r < n - 1) ans -= f(st.get(r), st.get(r + 1), s, t);
st.update(l, r, x, 1, 0, n - 1);
ans += f(st.get(l - 1), st.get(l), s, t);
if (r < n - 1) ans += f(st.get(r), st.get(r + 1), s, t);
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... |