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;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
using ll = long long;
const int maxn = 2e5+10;
int n,q;
ll a[maxn];
//00
//01
//10
//11
ll sgn(ll x) {
if (x==0) return 0;
if (x>0) return 1;
if (x<0) return -1;
assert(false);
return 1;
}
struct node {
ll dx;
ll dp[3][3];
node() {
memset(dp,0,sizeof(dp));
dx = 0;
dp[sgn(0)+1][sgn(0)+1] = abs(0);
}
node(ll x) {
memset(dp,0,sizeof(dp));
dx = x;
dp[sgn(0)+1][sgn(0)+1] = abs(0);
dp[sgn(x)+1][sgn(x)+1] = abs(x);
}
};
node merge(node x, node y) {
node res = x;
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
res.dp[i][j] = max(res.dp[i][j], y.dp[i][j]);
}
}
for (int i1: {-1,0,1}) {
for (int j0: {-1,0,1}) {
if (i1*j0 >= 0) {
for (int i0: {-1,0,1}) {
for (int j1: {-1,0,1}) {
res.dp[i0+1][j1+1] = max(res.dp[i0+1][j1+1], x.dp[i0+1][i1+1]+y.dp[j0+1][j1+1]);
}
}
}
}
}
return res;
}
node t[4*maxn];
void upd(int v, int tl, int tr, int i, ll dx) {
if (tl==tr) {
ll prev = t[v].dx;
t[v] = node(prev+dx);
} else {
int tm=(tl+tr)/2;
if (i<=tm) {
upd(2*v,tl,tm,i,dx);
} else {
upd(2*v+1,tm+1,tr,i,dx);
}
t[v]=merge(t[2*v],t[2*v+1]);
}
}
void build(int v, int tl, int tr) {
if (tl==tr) {
if (tl>1) {
t[v]=node(a[tl]-a[tl-1]);
} else {
t[v]=node();
}
} else {
int tm = (tl+tr)/2;
build(2*v,tl,tm);
build(2*v+1,tm+1,tr);
t[v]=merge(t[2*v],t[2*v+1]);
}
}
void update(int l, int r, ll dx) {
if (l>1) {
upd(1,1,n,l,dx);
}
if (r<n) {
upd(1,1,n,r+1,-dx);
}
}
ll solve() {
ll res = 0;
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
res = max(res, t[1].dp[i][j]);
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>q;
for (int i=1; i<=n; i++) {
cin>>a[i];
}
build(1,1,n);
while (q--) {
int l,r,dx;
cin>>l>>r>>dx;
update(l,r,dx);
cout<<solve()<<"\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... |