#include <bits/stdc++.h>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
using ull = unsigned ll;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000009;
//const ll Mod2 = 999999999989;
//only use when required
const ll inf = -(1e18);
const int maxN = 1e5 + 1;
int n, q;
int a[maxN];
struct TNode{
ll dp[2][2];
ll l, r;
ll lazy;
bool okay;
void sex(){
dp[0][0] = dp[0][1] = dp[1][0] = dp[1][1] = inf;
l = r = lazy = okay = 0;
}
TNode operator + (const TNode &other){
TNode tem;
tem.sex();
if (okay) return other;
if (other.okay){
tem.dp[0][0] = dp[0][0];
tem.dp[0][1] = dp[0][1];
tem.dp[1][0] = dp[1][0];
tem.dp[1][1] = dp[1][1];
tem.l = l;
tem.r = r;
return tem;
}
tem.l = l; tem.r = other.r;
for (int i = 0; i < 2; ++i){
for (int j = 0; j < 2; ++j){
tem.dp[i][j] = max(tem.dp[i][j], dp[i][0] + other.dp[0][j]);
tem.dp[i][j] = max(tem.dp[i][j], dp[i][0] + other.dp[1][j]);
tem.dp[i][j] = max(tem.dp[i][j], dp[i][1] + other.dp[0][j]);
tem.dp[i][j] = max(tem.dp[i][j], dp[i][1] + other.dp[1][j]);
if (r <= other.l){
tem.dp[i][j] = max(tem.dp[i][j], dp[i][0] + other.l - r + other.dp[0][j]);
}
else{
tem.dp[i][j] = max(tem.dp[i][j], dp[i][1] + r - other.l + other.dp[1][j]);
}
}
}
return tem;
}
};
TNode st[maxN * 4];
void pull(int id, bool t){
if (st[id].lazy){
if (!t){
st[id * 2].l += st[id].lazy;
st[id * 2].r += st[id].lazy;
st[id * 2].lazy += st[id].lazy;
st[id * 2 + 1].l += st[id].lazy;
st[id * 2 + 1].r += st[id].lazy;
st[id * 2 + 1].lazy += st[id].lazy;
}
st[id].lazy = 0;
}
}
void update(int id, int i, int j, int x, int l = 1, int r = n){
if (r < i || l > j) return;
if (i <= l && r <= j){
//cerr << id << " " << st[id].l << " " << st[id].r << "\n";
st[id].lazy += x;
st[id].l += x;
st[id].r += x;
return;
}
pull(id, l == r);
int mid = (l + r) / 2;
update(id * 2, i, j, x, l, mid);
update(id * 2 + 1, i, j, x, mid + 1, r);
st[id] = st[id * 2] + st[id * 2 + 1];
}
void build(int id = 1, int l = 1, int r = n){
st[id].sex();
if (l == r){
st[id].dp[0][0] = st[id].dp[1][1] = 0;
st[id].l = st[id].r = a[l];
//cerr << id << " " << st[id].l << " " << st[id].r << "\n";
return;
}
int mid = (l + r) / 2;
build(id * 2, l, mid);
build(id * 2 + 1, mid + 1, r);
st[id] = st[id * 2] + st[id * 2 + 1];
//cerr << l << " " << r << " " << st[id * 2].dp[1][1] << " " << st[id * 2 + 1].dp[1][1] << " " << st[id].dp[1][1] << "\n";
}
TNode get(int id, int i, int j, int l = 1, int r = n){
if (r < i || l > j){
TNode incest;
incest.okay = 1;
return incest;
};
if (i <= l && r <= j){
return st[id];
}
int mid = (l + r) / 2;
TNode x = get(id * 2, i, j, l, mid);
TNode y = get(id * 2 + 1, i, j, mid + 1, r);
TNode z = x + y;
return x + y;
}
ll query(int l, int r, int x){
update(1, l, r, x);
return max(max(st[1].dp[0][0], st[1].dp[0][1]), max(st[1].dp[1][0], st[1].dp[1][1]));
}
void Init(){
cin >> n >> q;
for (int i = 1; i <= n; ++i) cin >> a[i];
build();
//cout << st[1].dp[0][0] << " " << st[1].dp[0][1] << " " << st[1].dp[1][0] << " " << st[1].dp[1][1] << "\n";
while (q--){
int l, r, x;
cin >> l >> r >> x;
cout << query(l, r, x) << "\n";
}
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}
Compilation message
Main.cpp: In function 'TNode get(int, int, int, int, int)':
Main.cpp:133:11: warning: variable 'z' set but not used [-Wunused-but-set-variable]
133 | TNode z = x + y;
| ^
Main.cpp: In function 'int main()':
Main.cpp:156:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
156 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 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 |
340 KB |
Output is correct |
4 |
Correct |
1 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 |
5 ms |
980 KB |
Output is correct |
8 |
Correct |
5 ms |
988 KB |
Output is correct |
9 |
Correct |
5 ms |
980 KB |
Output is correct |
10 |
Correct |
5 ms |
964 KB |
Output is correct |
11 |
Correct |
5 ms |
940 KB |
Output is correct |
12 |
Correct |
5 ms |
1024 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 |
340 KB |
Output is correct |
4 |
Correct |
1 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 |
5 ms |
980 KB |
Output is correct |
8 |
Correct |
5 ms |
988 KB |
Output is correct |
9 |
Correct |
5 ms |
980 KB |
Output is correct |
10 |
Correct |
5 ms |
964 KB |
Output is correct |
11 |
Correct |
5 ms |
940 KB |
Output is correct |
12 |
Correct |
5 ms |
1024 KB |
Output is correct |
13 |
Runtime error |
11 ms |
2520 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |