#include <bits/stdc++.h>
using namespace std;
const int mxN = 4e5 + 100;
#define int long long
struct state{
int b[2] = {0, 0};
int dp[2][2] = {0, 0, 0, 0};
state combine(const state &other){
state cur;
cur.b[0] = b[0], cur.b[1] = other.b[1];
for(int l = 0; l <= 1; ++l){
for(int x = 0; x <= 1; ++x){
for(int y = 0; y <= 1; ++y){
for(int r = 0; r <= 1; ++r){
if(x && y){
if((b[1] < 0)== (other.b[0] < 0)){
cur.dp[l][r] = max(cur.dp[l][r], dp[l][1] + other.dp[1][r]);
}
}else{
cur.dp[l][r] = max(cur.dp[l][r], dp[l][x] + other.dp[y][r]);
}
}
}
}
}
return cur;
}
void upd(int x){
b[0] += x, b[1] += x;
dp[1][1] = abs(b[0]);
}
};
vector<state> seg;
int N;
void init(int n){
N = n;
seg.resize(mxN * 4);
}
void update(int node, int start, int end, int idx, int val){
if(start == end){
seg[node].upd(val);
return;
}
int mid = start + (end - start) / 2;
if(idx <= mid) update(node * 2 + 1, start, mid, idx, val);
else update(node * 2 + 2, mid + 1, end, idx, val);
seg[node] = seg[node * 2 + 1].combine(seg[node * 2 + 2]);
}
void update(int idx, int x){
update(0, 0, N - 1, idx, x);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(nullptr); cout.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> a(n + 1, 0), d;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i <= n - 1; ++i){
d.push_back(a[i + 1] - a[i]);
}
init((int) d.size());
for(int i = 0; i < (int) d.size(); ++i) update(i, d[i]);
while(q--){
int l, r, x;
cin >> l >> r >> x; --l, --r;
if(l - 1 >= 0) update(l - 1, x);
if(r < n - 1) update(r, -x);
cout << seg[0].dp[1][1] << "\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... |