This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 5e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
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;
}
int n;
int q;
ll a[mxN];
ll dp[mxN][3];
struct IT{
struct Node{
ll f[2][2];
ll lef;
ll rig;
};
int n;
vector<Node> t;
vector<ll> lz;
IT(){}
IT(int _n) {
n = _n;
t.resize(n * 4 + 7);
lz.resize(n * 4 + 7, 0);
}
Node merge_node(Node &x, Node &y)
{
Node res;
res.lef = x.lef;
res.rig = y.rig;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++) res.f[i][j] = -infll;
for(int a = 0; a < 2; a++)
for(int b = 0; b < 2; b++) {
if(x.f[a][b] == -infll) continue;
for(int c = 0; c < 2; c++)
for(int d = 0; d < 2; d++) {
if(y.f[c][d] == -infll) continue;
ll val = x.f[a][b] + y.f[c][d];
if(b == c) {
if(b == 0) {
if(x.rig < y.lef) val += y.lef - x.rig;
}
else {
if(x.rig > y.lef) val += x.rig - y.lef;
}
}
maximize(res.f[a][d], val);
}
}
return res;
}
void build(int v, int tl, int tr)
{
if(tl == tr) {
t[v].lef = t[v].rig = a[tl];
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
if(i != j) t[v].f[i][j] = -infll;
else t[v].f[i][j] = 0;
}
else {
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm + 1, tr);
t[v] = merge_node(t[v * 2], t[v * 2 + 1]);
}
}
void push_down(int v)
{
t[v * 2].lef += lz[v];
t[v * 2].rig += lz[v];
t[v * 2 + 1].lef += lz[v];
t[v * 2 + 1].rig += lz[v];
lz[v * 2] += lz[v];
lz[v * 2 + 1] += lz[v];
lz[v] = 0;
}
void update(int v, int tl, int tr, int l, int r, int val)
{
if(l > r) return;
if(tl == l && tr == r) {
t[v].lef += val;
t[v].rig += val;
lz[v] += val;
}
else {
push_down(v);
int tm = (tl + tr) / 2;
update(v * 2, tl, tm, l, min(r, tm), val);
update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
t[v] = merge_node(t[v * 2], t[v * 2 + 1]);
}
}
void update(int l, int r, int val)
{
update(1, 1, n, l, r, val);
}
} seg;
void solve()
{
cin >> n >> q;
for(int i = 1; i <= n; i++) cin >> a[i];
seg = IT(n);
seg.build(1, 1, n);
while(q--) {
int l, r, x;
cin >> l >> r >> x;
seg.update(l, r, x);
ll res = -infll;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++) maximize(res, seg.t[1].f[i][j]);
cout << res << endl;
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int tc = 1;
//cin >> tc;
while(tc--) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |