#include <bits/stdc++.h>
using namespace std;
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define rf if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
//#define in ({int x = 0; int c = getchar(), n = 0; for(; !isdigit(c); c = getchar()) n = (c == '-'); for(; isdigit(c); c = getchar()) x = x * 10 + c - '0'; n ? -x : x;})
#define bit(i, mask) (((mask) >> (i)) & 1)
#define on(i, mask) ((mask) | (1LL << (i)))
#define off(i, mask) ((mask) & (~(1LL << (i))))
#define ll long long
#define fi first
#define se second
#define pii pair<int, int>
#define plx pair<ll, int>
#define vi vector<int>
#define vvi vector<vi>
#define all(a) (a).begin(), (a).end()
#define len(x) ((int)(x).size())
#define pb push_back
#define endl '\n'
#define ub(x, i) upper_bound(all(x), i) - x.begin()
#define lb(x, i) lower_bound(all(x), i) - x.begin()
#define name ""
template<typename T1, typename T2> bool mini(T1 &a, T2 b)
{if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b)
{if(a < b) a = b; else return 0; return 1;}
const int mod = 1e9+7;
const int inf = 1e9+9;
const ll oo = 1e18l+7;
const int M = 5e5+6;
const int N = 2e5+6;
const int LOG = 31 - __builtin_clz(N);
int n, q, a[N], d[N];
struct Segment{
struct Node{
ll d[2][2];
int head, tail;
Node(){
d[0][0] = d[0][1] = d[1][0] = d[1][1] = 0;
}
Node(ll x, ll y, ll z, ll t, int _head, int _tail){
d[0][0] = x, d[0][1] = y, d[1][0] = z, d[1][1] = t;
head = _head, tail = _tail;
}
friend Node operator + (const Node &a, const Node &b){
Node res;
if(1LL * a.tail * b.head < 0LL){
res.d[0][0] = max(a.d[0][1] + b.d[0][0], a.d[0][0] + b.d[1][0]);
res.d[1][0] = max(a.d[1][1] + b.d[0][0], a.d[1][0] + b.d[1][0]);
res.d[0][1] = max(a.d[0][1] + b.d[0][1], a.d[0][0] + b.d[1][1]);
res.d[1][1] = max(a.d[1][1] + b.d[0][1], a.d[1][0] + b.d[1][1]);
}
else{
res.d[0][0] = a.d[0][1] + b.d[1][0];
res.d[1][0] = a.d[1][1] + b.d[1][0];
res.d[0][1] = a.d[0][1] + b.d[1][1];
res.d[1][1] = a.d[1][1] + b.d[1][1];
}
res.head = a.head;
res.tail = b.tail;
return res;
}
};
Node st[N << 2];
void build(int id, int l, int r){
if(l == r){
st[id] = Node(0, 0, 0, abs(d[l]), d[l], d[l]);
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
st[id] = st[id << 1] + st[id << 1 | 1];
}
void update(int id, int l, int r, int i, int val){
if(l > i || r < i) return;
if(l == r){
d[l] += val;
st[id] = Node(0, 0, 0, abs(d[l]), d[l], d[l]);
return;
}
int mid = (l + r) >> 1;
if(l <= i && i <= mid) update(id << 1, l, mid, i, val);
else update(id << 1 | 1, mid + 1, r, i, val);
st[id] = st[id << 1] + st[id << 1 | 1];
}
Node get(int id, int l, int r, int u, int v){
if(l > v || r < u) return Node();
if(l >= u && r <= v) return st[id];
int mid = (l + r) >> 1;
return get(id << 1, l, mid, u, v) + get(id << 1 | 1, mid + 1, r, u, v);
}
} it;
void inp(){
cin >> n >> q;
for(int i = 1; i <= n; i++) cin >> a[i];
}
void proc(){
for(int i = 2; i <= n; i++) d[i] = a[i] - a[i - 1];
it.build(1, 1, n);
while(q--){
int l, r, x; cin >> l >> r >> x;
if(l > 1) it.update(1, 1, n, l, x);
if(r < n) it.update(1, 1, n, r + 1, -x);
cout << it.get(1, 1, n, 1, n).d[1][1] << endl;
}
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
rf
int test = 1;
//cin >> test;
while(test--){
inp();
proc();
}
cerr << "Time elapsed: " << TIME << "s" << endl;
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:8:47: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
8 | #define rf if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:131:5: note: in expansion of macro 'rf'
131 | rf
| ^~
Main.cpp:8:80: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
8 | #define rf if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:131:5: note: in expansion of macro 'rf'
131 | rf
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |