#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
#define rnd(l, r) uniform_int_distribution<int>(l, r)(rng)
using namespace std;
void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
int pow(int a,int b,int m){int ans=1;while(b){if(b&1){ans=(ans*a)%m;}b>>=1;a=(a*a)%m;}return ans;}
int binpow(int a,int b){int ans=1;while(b){if(b&1){ans=(ans*a);}b>>=1;a=(a*a);}return ans;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2e5 + 10, MX = 2e5 + 10, inf = 1e18;
int a[N], dp[N];
int n;
struct st{
int mx = -inf, mn = inf, dpcost = 0, ans = 0;
};
st t[N * 4];
int bmn[N * 4], bmx[N * 4], used[N * 4];
st merge(st a, st b){
st res;
res.ans = max(a.ans, b.ans);
res.ans = max(a.ans + a.mx - a.mn, b.ans + b.mx - b.mn);
return res;
}
void push(int v, int tl, int tr) {
if(!used[v]) return;
t[v].mn = min(t[v].mn, bmn[v]);
t[v].mx = max(t[v].mx, bmx[v]);
t[v].ans = max(t[v].ans, t[v].dpcost + t[v].mx - t[v].mn);
if (tl != tr) {
bmx[v * 2] = max(bmx[v * 2], bmx[v]);
bmx[v * 2 + 1] = max(bmx[v * 2 + 1], bmx[v]);
bmn[v * 2] = min(bmn[v * 2], bmn[v]);
bmn[v * 2 + 1] = min(bmn[v * 2 + 1], bmn[v]);
used[v * 2 + 1] = 1;
used[v * 2] = 1;
}
used[v]=0;
}
void update2(int pos, int x, int v = 1,int tl = 1, int tr = n){
push(v, tl, tr);
if(tl == tr){
t[v].ans = x;
// t[v].ans = t[v].dpcost + t[v].mx - t[v].mn;
}else{
int mid = (tl + tr) >> 1;
if(pos <= mid)
update2(pos, x, v * 2, tl, mid);
else
update2(pos, x, v * 2 + 1,mid + 1, tr);
t[v] = merge(t[v * 2], t[v * 2 + 1]);
}
}
void update(int l, int r, int x, int v = 1, int tl = 1, int tr = n){
push(v, tl, tr);
if (tr < l || r < tl) return;
if(l <= tl && tr <= r){
bmn[v] = min(bmn[v], x);
bmx[v] = max(bmx[v], x);
used[v]=1;
push(v, tl, tr);
return;
}else{
push(v, tl, tr);
int mid=(tl+tr)/2;
update(l, r, x, v * 2, tl, mid);
update(l, r, x, v * 2 + 1, mid + 1, tr);
}
}
int get(int l, int r, int v = 1, int tl = 1, int tr = n){
if(tl > r || tr < l)
return 0;
if(tl >= l && tr <= r){
return t[v].ans;
}
int mid = (tl + tr) >> 1;
return max(get(l, r, v * 2, tl, mid),get(l, r, v * 2 + 1, mid + 1, tr));
}
main(){
iostream::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int q;
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
while(q--){
int l, r, x;
cin >> l >> r >> x;
for(int i = l; i <= r; i++){
a[i] += x;
}
for(int i = 1; i <= n; i++)dp[i] = -inf;
int mn = -inf, mx = -inf;
for(int i = 1; i <= n; i++){
mn = max(dp[i - 1] - a[i], mn);
mx = max(dp[i - 1] + a[i], mx);
dp[i] = max(mx - a[i], mn + a[i]);
dp[i] = max(dp[i - 1], dp[i]);
//cout << dp[i] <<"-";
}
cout << dp[n] << endl;
}
//cout << ans << endl;
}
/*
* Before implementing the problem:
Do I understand the problem correctly?
Which places are tricky? What do I need to remember to implement them correctly?
Which place is the heaviest by implementation? Can I do it simpler?
Which place is the slowest? Where do I need to be careful about constant factors and where I can choose slower but simpler implementation?
----------------------------------
If not AC:
Did you remember to do everything to handle the tricky places you thought about before?
Is the solution correct?
Do I understand the problem correctly?
----------------------------------
If you have a test on which the solution gives wrong answer:
Is the solution doing what it was supposed to do?
Is the problem in the code or in the idea?
*/
Compilation message
Main.cpp:98:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
98 | main(){
| ^~~~
Main.cpp: In function 'void fp(std::string)':
Main.cpp:15:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:15:70: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
42 ms |
2648 KB |
Output is correct |
8 |
Correct |
42 ms |
2648 KB |
Output is correct |
9 |
Correct |
42 ms |
2652 KB |
Output is correct |
10 |
Correct |
42 ms |
2648 KB |
Output is correct |
11 |
Correct |
42 ms |
2648 KB |
Output is correct |
12 |
Correct |
43 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
42 ms |
2648 KB |
Output is correct |
8 |
Correct |
42 ms |
2648 KB |
Output is correct |
9 |
Correct |
42 ms |
2652 KB |
Output is correct |
10 |
Correct |
42 ms |
2648 KB |
Output is correct |
11 |
Correct |
42 ms |
2648 KB |
Output is correct |
12 |
Correct |
43 ms |
2652 KB |
Output is correct |
13 |
Execution timed out |
2035 ms |
6340 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |