#include <cstdio>
#include <stdio.h>
#include <stdbool.h>
#include <iostream>
#include <map>
#include <vector>
#include <climits>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <cctype>
#include <bitset>
#include <iomanip>
#include <cstring>
#include <numeric>
#include <cassert>
using namespace std;
#define int long long
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
struct node{
int s, e, m, val[2][2];
node *l, *r;
node(int S, int E){
s = S, e = E, m = (s+e)/2;
val[0][0]=val[0][1]=val[1][0]=val[1][1]=0;
if (s!=e){
l = new node(s, m);
r = new node(m+1, e);
}
}
void up(int ind, int nv, bool ta, bool tb){
if (s==e)val[0][0]=val[0][1]=val[1][0]=val[1][1]=0, val[ta][tb]=nv;
else{
if (ind<=m)l->up(ind, nv, ta, tb);
else r->up(ind, nv, ta, tb);
int a[2][2], b[2][2];
a[0][0]=l->val[0][0];
a[0][1]=l->val[0][1];
a[1][0]=l->val[1][0];
a[1][1]=l->val[1][1];
b[0][0]=r->val[0][0];
b[0][1]=r->val[0][1];
b[1][0]=r->val[1][0];
b[1][1]=r->val[1][1];
val[0][0]=val[0][1]=val[1][0]=val[1][1]=0;
val[0][0]=max({a[0][0]+max(b[0][0], b[1][0]), max(a[0][1], a[0][0])+b[0][0]});
val[1][1]=max({a[1][0]+max(b[0][1], b[1][1]), max(a[1][1], a[1][0])+b[0][1]});
val[0][1]=max({a[0][0]+max(b[0][1], b[1][1]), max(a[0][1], a[0][0])+b[0][1]});
val[1][0]=max({a[1][0]+max(b[0][0], b[1][0]), max(a[1][1], a[1][0])+b[0][0]});
}
}
int query(int i){
if (s==e)return (val[1][1])>0;
if (i<=m)return l->query(i);
else return r->query(i);
}
int query2(int i){
if (s==e)return max({val[0][0], val[0][1], val[1][0], val[1][1]});
if (i<=m)return l->query2(i);
else return r->query2(i);
}
}*st;
struct node2{
int s, e, m, val, lazy;
node2 *l, *r;
void create(){
if (l==nullptr){
l = new node2(s, m);
r = new node2(m+1, e);
}
}
void propagate(){
val+=lazy*(e-s+1);
if (s!=e){
create();
l->lazy+=lazy;
r->lazy+=lazy;
}
lazy=0;
}
node2(int S, int E){
s = S, e = E, m = (s+e)/2;
val=lazy=0;
l=r=nullptr;
}
void up(int left, int right, int v){
propagate();
if (s==left && e==right)lazy+=v;
else{
create();
if (right<=m)l->up(left, right, v);
else if (left>m)r->up(left, right, v);
else l->up(left, m, v), r->up(m+1, right, v);
r->propagate(), l->propagate();
val=l->val+r->val;
}
}
int query(int left, int right){
propagate();
if (s==left && e==right)return val;
create();
if (right<=m)return l->query(left, right);
else if (left>m)return r->query(left, right);
else return l->query(left, m)+r->query(m+1, right);
}
}*st2;
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q, aa, bb, cc;
cin>>n>>q;
if (n==1){
while (q--)cout<<"0\n";
}
st = new node(2, n);
st2 = new node2(1, n);
vector<int> vect(n+1);
for (int i=1; i<=n; ++i)cin>>vect[i], st2->up(i, i, vect[i]);
for (int i=2; i<=n; ++i){
bool ta=0, tb=0;
if (1<i&&i<n)tb=((vect[i-1]<vect[i]&&vect[i+1]<vect[i])||(vect[i-1]>vect[i]&&vect[i+1]>vect[i]));
if (1<=i-2&&i<=n)ta=((vect[i-2]<vect[i-1]&&vect[i]<vect[i-1])||(vect[i-2]>vect[i-1]&&vect[i]>vect[i-1]));
st->up(i, abs(vect[i]-vect[i-1]), ta, tb);
}
while (q--){
cin>>aa>>bb>>cc;
st2->up(aa, bb, cc);
for (int a=aa-1; a<=aa+1; ++a){
bool ta=0, tb=0;
if (1<a&&a<n)ta=((st2->query(a-1, a-1)<st2->query(a, a)&&st2->query(a+1, a+1)<st2->query(a, a))||(st2->query(a-1, a-1)>st2->query(a, a)&&st2->query(a+1, a+1)>st2->query(a, a)));
if (1<=a-2&&a<=n)tb=((st2->query(a-2, a-2)<st2->query(a-1, a-1)&&st2->query(a, a)<st2->query(a-1, a-1))||(st2->query(a-2, a-2)>st2->query(a-1, a-1)&&st2->query(a, a)>st2->query(a-1, a-1)));
if (a>1&&a<=n)st->up(a, abs(st2->query(a, a)-st2->query(a-1, a-1)), tb, ta);
}
for (int b=bb-1; b<=bb+1; ++b){
bool ta=0, tb=0;
if (1<b&&b<n)ta=((st2->query(b-1, b-1)<st2->query(b, b)&&st2->query(b+1, b+1)<st2->query(b, b))||(st2->query(b-1, b-1)>st2->query(b, b)&&st2->query(b+1, b+1)>st2->query(b, b)));
if (!tb&&1<=b&&b+2<=n)tb=((st2->query(b, b)<st2->query(b+1, b+1)&&st2->query(b+2, b+2)<st2->query(b+1, b+1))||(st2->query(b, b)>st2->query(b+1, b+1)&&st2->query(b+2, b+2)>st2->query(b+1, b+1)));
if (1<=b&&b<n)st->up(b+1, abs(st2->query(b, b)-st2->query(b+1, b+1)), ta, tb);
}
cout<<max({st->val[0][0], st->val[0][1], st->val[1][0], st->val[1][1]})<<"\n";
//cout<<"{";
//for (int i=1; i<=n; ++i)cout<<st2->query(i, i)<<" ";
//cout<<"}\n";
//cout<<"[";
//for (int i=2; i<=n; ++i)cout<<st->query(i)<<" ";
//cout<<"]\n";
//cout<<"[";
//for (int i=2; i<=n; ++i)cout<<st->query2(i)<<" ";
//cout<<"]\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... |