#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> order_set;
//mt19937 mt_rand(chrono::high_resolution_clock::now().time_since_epoch().count());
//uniform_int_distribution<int> gen; ///(min, max)
//int random() {return gen(mt_rand);}
const int mxN = 5e5+25000;
const int mod = 1e9+9;
const int mxlogN = 20;
const int mxK = 26;
const ll inf = 1e18;
const int K=600;
///left, right 00, 1+, 2-
ll v[mxN][3][3], a[mxN], lz[mxN];
int sz;
void recalc(int i)
{
for(int j=0; j<3; j++) for(int k=0; k<3; k++) v[i][j][k]=max(v[i*2+1][j][k], v[i*2+2][j][k]);
for(int j=0; j<3; j++) for(int k=0; k<3; k++) for(int l=0; l<3; l++) for(int m=0; m<3; m++)
{
if(k&&l)
{
if(k==l) continue;
}
else if(k||l) continue;
v[i][j][m]=max(v[i][j][m],v[i*2+1][j][k]+v[i*2+2][l][m]);
}
}
void build2(int n, int i=0, int Lx=0, int Rx=sz)
{
if(Rx-Lx==1)
{
for(int j=0; j<3; j++) for(int k=0; k<3; k++) v[i][j][k]=-inf;
if(Lx<n)
{
v[i][0][1]=a[Lx];
v[i][1][0]=a[Lx];
v[i][0][2]=-a[Lx];
v[i][2][0]=-a[Lx];
v[i][0][0]=0;
}
return;
}
int m = (Lx+Rx)/2;
build2(n, i*2+1, Lx, m);
build2(n, i*2+2, m, Rx);
recalc(i);
}
void build(int n)
{
sz=1;
while(sz < n) sz <<= 1;
build2(n);
}
void lazy(int i)
{
if(lz[i])
{
lz[i*2+1]+=lz[i];
for(int j=0; j<3; j++) for(int k=0; k<3; k++)
{
ll add=0;
if(j==1) add++;
if(k==1) add++;
if(j==2) add--;
if(k==2) add--;
v[i*2+1][j][k]+=lz[i]*add;
}
for(int j=0; j<3; j++) for(int k=0; k<3; k++)
{
ll add=0;
if(j==1) add++;
if(k==1) add++;
if(j==2) add--;
if(k==2) add--;
v[i*2+2][j][k]+=lz[i]*add;
}
lz[i*2+2]+=lz[i];
lz[i]=0;
}
}
void ADD(int l, int r, ll x, int i=0, int Lx=0, int Rx=sz)
{
if(Rx <= l || Lx >= r) return;
if(Lx >= l && Rx <= r)
{
for(int j=0; j<3; j++) for(int k=0; k<3; k++)
{
ll add=0;
if(j==1) add++;
if(k==1) add++;
if(j==2) add--;
if(k==2) add--;
v[i][j][k]+=x*add;
}
lz[i]+=x;
return;
}
int m = (Rx+Lx)/2;
lazy(i);
ADD(l, r, x, i*2+1, Lx, m);
ADD(l, r, x, i*2+2, m, Rx);
recalc(i);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q; cin >> n >> q;
for(int i=0; i<n; i++) cin >> a[i];
build(n);
while(q--)
{
int l, r, x; cin >> l >> r >> x;
ADD(l-1,r,x);
cout << v[0][0][0] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
15 ms |
1108 KB |
Output is correct |
8 |
Correct |
17 ms |
1108 KB |
Output is correct |
9 |
Correct |
16 ms |
1112 KB |
Output is correct |
10 |
Correct |
20 ms |
1096 KB |
Output is correct |
11 |
Correct |
16 ms |
1112 KB |
Output is correct |
12 |
Correct |
15 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
15 ms |
1108 KB |
Output is correct |
8 |
Correct |
17 ms |
1108 KB |
Output is correct |
9 |
Correct |
16 ms |
1112 KB |
Output is correct |
10 |
Correct |
20 ms |
1096 KB |
Output is correct |
11 |
Correct |
16 ms |
1112 KB |
Output is correct |
12 |
Correct |
15 ms |
1108 KB |
Output is correct |
13 |
Correct |
1700 ms |
51148 KB |
Output is correct |
14 |
Correct |
1774 ms |
51300 KB |
Output is correct |
15 |
Correct |
1603 ms |
51208 KB |
Output is correct |
16 |
Correct |
1613 ms |
51024 KB |
Output is correct |
17 |
Correct |
1627 ms |
50996 KB |
Output is correct |
18 |
Correct |
1654 ms |
51656 KB |
Output is correct |