#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int n; cin >> n;
int q; cin >> q;
int a[n]; for (int i = 0; i < n; i++ )cin >> a[i];
int d[n]; for (int i = 0; i < n-1; i++) d[i] = a[i+1]-a[i];
d[n-1] = 0;
while (q--) {
int s, e, c; cin >> s >> e >> c;
if (s >= 2) d[s-2]+=c;
d[e-1]-= c;
int ans = 0;
int cur = d[0];
//two cases: take first, dont take first
for (int i = 1; i < n-1; i++) {
if (d[i]*d[i-1] > 0 or cur == 0) {
//take it
cur += d[i];
}
else {
ans += abs(cur);
cur = 0;
}
}
ans += abs(cur);
//case 2: dont take first
int ans2 = 0;
cur = 0;
for (int i = 1; i < n-1; i++) {
if (d[i]*d[i-1] > 0 or cur == 0) {
cur += d[i];
}
else {
ans2 += abs(cur);
cur = 0;
}
}ans2 += abs(cur);
cout << max(ans, ans2) << '\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... |