#include<bits/stdc++.h>
using namespace std;
// define
#define execute cerr << " Time: " << fixed << setprecision(6) << (1.0 * clock() / CLOCKS_PER_SEC) << "s\n";
#define ll long long
#define int long long
#define ld double
#define ii pair<int,int>
#define se second
#define fi first
#define iii pair<int,ii>
#define all(v) v.begin(),v.end()
#define bit(x,i) (((x)>>(i))&1LL)
#define flip(x,i) ((x)^(1LL<<(i)))
#define ms(d,x) memset(d,x,sizeof(d))
#define sitingfake 1
#define orz 1
#define exist __exist
#define ends __ends
#define visit visited
#define left __left
#define right __right
//constant
const ll mod = 1e9 + 7;
const long long linf = 4557430888798830399;
const int inf = 1061109567;
const int maxarr = 1e6 + 5;
int dx[] = {0 , -1 , 0 , 1};
int dy[] = {-1 , 0 , 1 , 0};
template<typename T> bool maximize(T &a, const T &b)
{
if(a < b) {a = b; return 1;}
return 0;
}
template<typename T> bool minimize(T &a, const T &b)
{
if(a > b) {a = b; return 1;}
return 0;
}
inline void Plus(ll & a ,ll b)
{
b %= mod;
a += b;
if(a >= mod) a -= mod;
if(a < 0) a += mod;
return;
}
inline void Mul(ll & a, ll b)
{
a %= mod; b %= mod;
a *= b;
a %= mod;
return;
}
//code
const int maxn = 2e5 + 7;
int a[maxn];
int n , q;
struct Node
{
ll res;
ll suf;
ll pre;
bool typepref , typesuf;
Node ()
{
res = -linf;
}
Node operator + (Node & other)
{
Node ans;
ans.pre = pre;
ans.suf = other.suf;
ans.typepref = typepref;
ans.typesuf = other.typesuf;
if(typesuf == other.typepref)
{
ans.res = res + other.res;
}
else
{
ans.res = max(res + other.res - other.pre , res - suf + other.res);
}
return ans;
}
}st[4 * maxn];
void Build(int i , int l ,int r)
{
if(l == r)
{
if(a[l] < a[l + 1])
{
st[i].typepref = st[i].typesuf = 0;
}
else
{
st[i].typepref = st[i].typesuf = 1;
}
st[i].pre = st[i].suf = st[i].res = abs(a[l + 1] - a[l]);
return;
}
int mid = (r + l) >> 1;
Build(i * 2 , l , mid);
Build(i * 2 + 1, mid + 1 , r);
st[i] = st[i * 2] + st[i * 2 + 1];
}
void UpLeft(int i , int l ,int r ,int pos , ll val)
{
if(pos > r || pos < l) return;
if(l == r)
{
if(st[i].typepref == 0)
{
st[i].pre += val;
st[i].suf += val;
st[i].res += val;
}
else
{
st[i].pre -= val;
st[i].suf -= val;
st[i].res -= val;
if(st[i].res < 0)
{
st[i].typepref = st[i].typesuf = 0;
st[i].pre = -st[i].pre;
st[i].suf = -st[i].suf;
st[i].res = -st[i].res;
}
}
return;
}
int mid = (r + l) >> 1;
UpLeft(i * 2 , l , mid , pos , val);
UpLeft(i * 2 + 1, mid + 1 , r , pos , val);
st[i] = st[i * 2] + st[i * 2 + 1];
}
void UpRight(int i , int l , int r, int pos , int val)
{
if(pos > r || pos < l) return;
if(l == r)
{
if(st[i].typepref == 1)
{
st[i].pre += val;
st[i].suf += val;
st[i].res += val;
}
else
{
st[i].pre -= val;
st[i].suf -= val;
st[i].res -= val;
if(st[i].res < 0)
{
st[i].typepref = st[i].typesuf = 1;
st[i].pre = -st[i].pre;
st[i].suf = -st[i].suf;
st[i].res = -st[i].res;
}
}
return;
}
int mid = (r + l) >> 1;
UpRight(i * 2 , l , mid , pos , val);
UpRight(i * 2 + 1, mid + 1 , r , pos , val);
st[i] = st[i * 2] + st[i * 2 + 1];
}
Node get(int i , int l , int r , int u, int v)
{
if(u <= l && r <= v) return st[i];
int mid = (r + l) >> 1;
if(v <= mid) return get(i * 2 , l , mid , u , v);
if(u > mid) return get(i * 2 + 1 , mid + 1, r , u , v);
Node node1 = get(i * 2 , l , mid , u, v);
Node node2 = get(i * 2 + 1, mid + 1 , r, u , v);
return node1 + node2;
}
void solve(void)
{
cin >> n >> q;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
Build(1 , 1 , n - 1);
while(q--)
{
//int t; cin >> t;
// if(t == 1)
// {
int l , r , x;
cin >> l >> r >> x;
if(l != 1) UpLeft(1 , 1 , n - 1 , l - 1 , x);
if(r != n) UpRight(1 , 1 , n - 1 , r , x);
cout << st[1].res << "\n";
// }
// else
// {
// int l , r;
// cin >> l >> r;
// if(l == r)
// {
// cout << "0\n";
// }
// else
// {
// cout << get(1 , 1 , n - 1, l , r - 1).res << "\n";
// }
// }
}
}
/**
**/
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define task ""
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
int tc; tc = 1;
while(tc--) solve();
//execute;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:255:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
255 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:256:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
256 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |