#include<bits/stdc++.h>
#define fo(i,d,c) for(int i=d;i<=c;i++)
#define fod(i,c,d) for(int i=c;i>=d;i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout<<"\n";
#define int long long
#define inf (int)1e18
#define pii pair<int,int>
#define vii vector<pii>
#define lb(x) x&-x
#define bit(i,j) ((i>>j)&1)
#define offbit(i,j) (i^(1<<j))
#define onbit(i,j) (i|(1<<j))
#define vi vector<int>
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
struct node
{
array<int,2> a;
int f[2][2];
node(int x = 0)
{
a[0] = a[1] = x;
f[1][1] = abs(x);
f[0][0] = f[1][0] = f[0][1] = 0;
}
};
node operator + (node a,node b)
{
node ans;
ans.a[0] = a.a[0];
ans.a[1] = b.a[1];
fo(l,0,1) fo(i,0,1) fo(j,0,1) fo(r,0,1)
{
if(i && j)
{
if((a.a[1] < 0) == (b.a[0] < 0))
ans.f[l][r] = max(ans.f[l][r],a.f[l][i] + b.f[j][r]);
}
else ans.f[l][r] = max(ans.f[l][r],a.f[l][i] + b.f[j][r]);
}
return ans;
}
node st[maxn];
int n,q;
int a[maxn];
int b[maxn];
void build(int id,int l,int r)
{
if(l == r)
{
st[id] = node(b[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 up(int id,int l,int r,int x,int val)
{
if(l > x or r < x) return ;
if(l == r)
{
st[id].a[0] += val;
st[id].a[1] += val;
st[id].f[1][1] = abs(st[id].a[0]);
return ;
}
int mid = (l + r) >> 1;
up(id << 1,l,mid,x,val);
up(id << 1 | 1,mid + 1,r,x,val);
st[id] = st[id << 1] + st[id << 1 | 1];
// cout << l << ' ' << r << st[id].f[1][1];en;
}
void up(int x,int val)
{
up(1,1,n - 1,x,val);
}
main()
{
#define name "TASK"
if(fopen(name".inp","r"))
{
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n >> q;
fo(i,1,n)
{
cin >> a[i];
b[i - 1] = a[i - 1] - a[i];
}
build(1,1,n - 1);
while(q--)
{
int l,r,val;
cin >> l >> r >> val;
if(l > 1) up(1,1,n - 1,l - 1,-val);
if(r < n) up(1,1,n - 1,r,val);
cout << max({st[1].f[0][0],st[1].f[1][1],st[1].f[0][1],st[1].f[1][0]});en;
}
}
Compilation message
Main.cpp:87:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
87 | main()
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:92:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:93:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
50520 KB |
Output is correct |
2 |
Correct |
8 ms |
50380 KB |
Output is correct |
3 |
Correct |
8 ms |
50524 KB |
Output is correct |
4 |
Correct |
8 ms |
50588 KB |
Output is correct |
5 |
Correct |
8 ms |
50592 KB |
Output is correct |
6 |
Correct |
8 ms |
50524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
50520 KB |
Output is correct |
2 |
Correct |
8 ms |
50380 KB |
Output is correct |
3 |
Correct |
8 ms |
50524 KB |
Output is correct |
4 |
Correct |
8 ms |
50588 KB |
Output is correct |
5 |
Correct |
8 ms |
50592 KB |
Output is correct |
6 |
Correct |
8 ms |
50524 KB |
Output is correct |
7 |
Correct |
12 ms |
50524 KB |
Output is correct |
8 |
Correct |
12 ms |
50688 KB |
Output is correct |
9 |
Correct |
12 ms |
50524 KB |
Output is correct |
10 |
Correct |
12 ms |
50524 KB |
Output is correct |
11 |
Correct |
12 ms |
50524 KB |
Output is correct |
12 |
Correct |
12 ms |
50524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
50520 KB |
Output is correct |
2 |
Correct |
8 ms |
50380 KB |
Output is correct |
3 |
Correct |
8 ms |
50524 KB |
Output is correct |
4 |
Correct |
8 ms |
50588 KB |
Output is correct |
5 |
Correct |
8 ms |
50592 KB |
Output is correct |
6 |
Correct |
8 ms |
50524 KB |
Output is correct |
7 |
Correct |
12 ms |
50524 KB |
Output is correct |
8 |
Correct |
12 ms |
50688 KB |
Output is correct |
9 |
Correct |
12 ms |
50524 KB |
Output is correct |
10 |
Correct |
12 ms |
50524 KB |
Output is correct |
11 |
Correct |
12 ms |
50524 KB |
Output is correct |
12 |
Correct |
12 ms |
50524 KB |
Output is correct |
13 |
Correct |
503 ms |
64200 KB |
Output is correct |
14 |
Correct |
484 ms |
63928 KB |
Output is correct |
15 |
Correct |
528 ms |
63936 KB |
Output is correct |
16 |
Correct |
484 ms |
63700 KB |
Output is correct |
17 |
Correct |
505 ms |
63804 KB |
Output is correct |
18 |
Correct |
454 ms |
64564 KB |
Output is correct |