#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q; cin >> n >> q;
ll smabs = 0, smc = 0;
vector<ll> D(n - 1);
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (i > 0) {
D[i - 1] = a[i] - a[i - 1];
smabs += abs(D[i - 1]);
}
}
auto junct = [&](int i) -> ll {
if (i >= n - 2 || i < 0) return 0;
if ((D[i] > 0 && D[i + 1] < 0) || (D[i] < 0 && D[i + 1] > 0)) return min(abs(D[i]), abs(D[i + 1]));
return 0;
};
for (int i = 0; i < n - 2; ++i) {
smc += junct(i);
}
auto add = [&](int i, int x) -> void {
if (i < 0 || i >= n - 1) return;
smc -= junct(i - 1);
smc -= junct(i);
smabs -= abs(D[i]);
D[i] += x;
smabs += abs(D[i]);
smc += junct(i - 1);
smc += junct(i);
};
while (q--) {
int l, r, x; cin >> l >> r >> x;
--l; --r;
add(l - 1, x);
add(r, -x);
cout << smabs - smc << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |