# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
670279 | chanhchuong123 | Sjeckanje (COCI21_sjeckanje) | C++17 | 464 ms | 26316 KiB |
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>
using namespace std;
#define task "C"
#define fi first
#define se second
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int N = 2e5 + 3;
int n, q, a[N]; ll d[N];
struct node {
int L, R;
ll dp[2][2];
} st[N << 2];
void combine(int id, bool ok) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
st[id].dp[i][j] = 0;
for (int _i = 0; _i < 2; _i++) {
for (int _j = 0; _j < 2; _j++) {
if (_i && _j && ok == false) continue;
maxi(st[id].dp[i][j], st[id << 1].dp[i][_i] + st[id << 1 | 1].dp[_j][j]);
}
}
}
}
}
void build(int id, int l, int r) {
if (l == r) {
st[id].L = st[id].R = l;
st[id].dp[0][0] = 0;
st[id].dp[0][1] = 0;
st[id].dp[1][0] = 0;
st[id].dp[1][1] = abs(d[l]);
} else {
int mid = l + r >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
combine(id, 1LL * d[mid] * d[mid + 1] >= 0);
}
}
void update(int id, int l, int r, int pos, int delta) {
if (l == r) {
d[l] += delta; //cout << d[l] << '\n';
st[id].dp[1][1] = abs(d[l]);
} else {
int mid = l + r >> 1;
if (pos <= mid) update(id << 1, l, mid, pos, delta);
else update(id << 1 | 1, mid + 1, r, pos, delta);
combine(id, 1LL * d[mid] * d[mid + 1] >= 0);
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++) d[i] = a[i + 1] - a[i];
build(1, 1, n - 1);
while (q--) {
int l, r, x; cin >> l >> r >> x;
if (l > 1) update(1, 1, n - 1, l - 1, +x); if (r < n) update(1, 1, n - 1, r, -x);
cout << max({st[1].dp[0][0], st[1].dp[0][1], st[1].dp[1][0], st[1].dp[1][1]}) << '\n';
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |