#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
}
void ran() {
srand(chrono::steady_clock::now().time_since_epoch().count());
}
long long get_rand() {
long long a = rand();
long long b = rand();
return a * (RAND_MAX + 1ll) + b;
}
void usaco() {
freopen("problem.in", "r", stdin);
freopen("problem.out", "w", stdout);
}
template<typename T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;
// #define endl '\n'
// #define double long double
#define int long long
// const int MOD = 1000 * 1000 * 1000 + 7;
// const int MOD = 998244353;
int n, q;
const int MXN = 200005;
int arr[MXN];
int df[MXN];
struct info {
int with[2][2] = {{0}}; // first and last, taken or not taken
info() {
with[0][0] = with[0][1] = with[1][0] = with[1][1] = 0;
}
} seg[MXN * 4];
info get(int x) {
info res;
res.with[1][1] = abs(x);
return res;
}
int sg(int x) {
if (x > 0) return 1;
if (x < 0) return -1;
return 0;
}
info comb(info l, info r, int m) {
info res;
bool cantog = (df[m] <= 0 && df[m+1] <= 0) || (df[m] >= 0 && df[m+1] >= 0);
if (cantog) {
for (int L=0; L<2; L++) {
for (int R=0; R<2; R++) {
for (int k=0; k<2; k++) for (int m=0; m<2; m++) {
res.with[L][R] = max(res.with[L][R], l.with[L][k] + r.with[m][R]);
}
}
}
}
else {
for (int L=0; L<2; L++) {
for (int R=0; R<2; R++) {
for (int k=0; k<2; k++) for (int m=0; m<2; m++) {
if (k == 1 && m == 1) continue;
else res.with[L][R] = max(res.with[L][R], l.with[L][k] + r.with[m][R]);
}
}
}
}
return res;
}
void build(int ind, int l, int r) {
if (l == r) {
seg[ind] = get(df[l]);
return;
}
int m = (l + r) / 2;
build(ind*2, l, m);
build(ind*2+1, m+1, r);
seg[ind] = comb(seg[ind*2], seg[ind*2+1], m);
}
void update(int ind, int l, int r, int pos) {
if (l == r) {
seg[ind] = get(df[l]);
return;
}
int m = (l + r) / 2;
if (pos <= m) update(ind*2, l, m, pos);
else update(ind*2+1, m+1, r, pos);
seg[ind] = comb(seg[ind*2], seg[ind*2+1], m);
}
int best() {
int a = 0;
for (int i=0; i<2; i++) for (int j=0; j<2; j++) a = max(a, seg[1].with[i][j]);
return a;
}
signed main() {
ran(); fast();
cin >> n >> q;
for (int i=1; i<=n; i++) cin >> arr[i];
if (n == 1) {
for (int i=0; i<q; i++) cout << 0 << endl;
exit(0);
}
for (int i=1; i<n; i++) df[i] = arr[i+1] - arr[i];
build(1, 1, n-1);
while (q--) {
int l, r, k;
cin >> l >> r >> k;
if (l > 1) {
df[l-1] += k;
update(1, 1, n-1, l-1);
}
if (r < n) {
df[r] -= k;
update(1, 1, n-1, r);
}
cout << best() << endl;
}
}
Compilation message
Main.cpp: In function 'void usaco()':
Main.cpp:15:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen("problem.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:16:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen("problem.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
25292 KB |
Output is correct |
2 |
Correct |
11 ms |
25336 KB |
Output is correct |
3 |
Correct |
11 ms |
25292 KB |
Output is correct |
4 |
Correct |
11 ms |
25380 KB |
Output is correct |
5 |
Correct |
11 ms |
25316 KB |
Output is correct |
6 |
Correct |
11 ms |
25292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
25292 KB |
Output is correct |
2 |
Correct |
11 ms |
25336 KB |
Output is correct |
3 |
Correct |
11 ms |
25292 KB |
Output is correct |
4 |
Correct |
11 ms |
25380 KB |
Output is correct |
5 |
Correct |
11 ms |
25316 KB |
Output is correct |
6 |
Correct |
11 ms |
25292 KB |
Output is correct |
7 |
Correct |
20 ms |
25548 KB |
Output is correct |
8 |
Correct |
20 ms |
25548 KB |
Output is correct |
9 |
Correct |
22 ms |
25548 KB |
Output is correct |
10 |
Correct |
21 ms |
25640 KB |
Output is correct |
11 |
Correct |
20 ms |
25548 KB |
Output is correct |
12 |
Correct |
19 ms |
25548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
25292 KB |
Output is correct |
2 |
Correct |
11 ms |
25336 KB |
Output is correct |
3 |
Correct |
11 ms |
25292 KB |
Output is correct |
4 |
Correct |
11 ms |
25380 KB |
Output is correct |
5 |
Correct |
11 ms |
25316 KB |
Output is correct |
6 |
Correct |
11 ms |
25292 KB |
Output is correct |
7 |
Correct |
20 ms |
25548 KB |
Output is correct |
8 |
Correct |
20 ms |
25548 KB |
Output is correct |
9 |
Correct |
22 ms |
25548 KB |
Output is correct |
10 |
Correct |
21 ms |
25640 KB |
Output is correct |
11 |
Correct |
20 ms |
25548 KB |
Output is correct |
12 |
Correct |
19 ms |
25548 KB |
Output is correct |
13 |
Correct |
862 ms |
37560 KB |
Output is correct |
14 |
Correct |
865 ms |
37588 KB |
Output is correct |
15 |
Correct |
903 ms |
37652 KB |
Output is correct |
16 |
Correct |
916 ms |
37640 KB |
Output is correct |
17 |
Correct |
886 ms |
37476 KB |
Output is correct |
18 |
Correct |
853 ms |
38204 KB |
Output is correct |