#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <cmath>
#include <map>
#include <random>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <limits.h>
using namespace std;
const int MOD = 1e9;
class Node {
public:
int dp[2][2] = {{0, 0}, {0, 0}};
int left;
int right;
int monotone;
int tl, tr;
};
Node merge (Node x, Node y) {
if (x.monotone == -1) return y;
if (y.monotone == -1) return x;
Node ans;
ans.left = x.left;
ans.right = y.right;
ans.tl = x.tl;
ans.tr = y.tr;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
ans.dp[i][j] = max(ans.dp[i][j], x.dp[i][1] + y.dp[1][j]);
ans.dp[i][j] = max(ans.dp[i][j], x.dp[i][0] + abs(x.right - y.left) + y.dp[0][j]);
}
}
if (x.tr - x.tl + y.tr - y.tl == 0) {
//cout << "YES\n";
ans.dp[0][1] = ans.dp[1][0] = ans.dp[0][0] = 0;
}
return ans;
}
void update(Node &x, int val) {
if (x.monotone == -1) {
x.left = val, x.right = val;
for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) x.dp[i][j] = 0;
x.monotone = true;
} else {
x.left += val, x.right += val;
}
}
Node INF;
struct segmentTree {
vector<Node> vec;
vector<int64_t> addLater;
void push(int v) {
addLater[2 * v + 1] += addLater[v];
addLater[2 * v] += addLater[v];
update(vec[2 * v + 1], addLater[v]);
update(vec[2 * v], addLater[v]);
addLater[v] = 0;
}
void upd(int dum, int tl, int tr, int l, int r, int64_t val) {
vec[dum].tl = tl, vec[dum].tr = tr;
if (tr < l || tl > r) {
return;
}
if (tl >= l && tr <= r) {
addLater[dum] += val;
update(vec[dum], val);
return;
}
push(dum);
int mid = (tl + tr) >> 1;
upd(2 * dum, tl, mid, l, r, val);
upd(2 * dum + 1, mid + 1, tr, l, r, val);
vec[dum] = merge(vec[2 * dum], vec[2 * dum + 1]);
}
void upd(int l, int r, int val) {
upd(1, 0, (int) vec.size() / 2 - 1, l, r, val);
}
void init (int dum, int tl, int tr) {
if (dum >= vec.size()) {
return;
}
vec[dum].tl = tl, vec[dum].tr = tr;
int mid = (tl + tr) >> 1;
upd(2 * dum, tl, mid);
upd(2 * dum + 1, mid + 1, tr);
}
Node get(int dum, int tl, int tr, int &l, int &r) {
vec[dum].tl = tl, vec[dum].tr = tr;
if (tl > r || tr < l) {
return INF;
}
if (tl >= l && tr <= r) {
return vec[dum];
}
push(dum);
int tm = (tl + tr) >> 1;
return merge(get(dum * 2, tl, tm, l, r), get(dum * 2 + 1, tm + 1, tr, l, r));
}
Node get(int l, int r) {
return get(1, 0, (int) vec.size() / 2 - 1, l, r);
}
void resz(int n) {
int sz = ((1 << (int) ceil(log2(n))));
vec.assign(sz * 2, INF);
addLater.resize(sz * 2);
init(1, 0, (int)vec.size()/2 - 1);
}
};
segmentTree st;
int N;
int64_t getter (int l, int r) {
int64_t a = max(st.get(l, r).dp[0][0], st.get(l, r).dp[0][1]);
int64_t b = max(st.get(l, r).dp[1][0], st.get(l, r).dp[1][1]);
//cout << a << " " << b << '\n';
return max(a, b);
}
int main() {
//freopen("inp.txt", "r", stdin);
//freopen("haybales.out", "w", stdout);
ios_base::sync_with_stdio(false);
INF.monotone = -1;
cin.tie(NULL);
int D;
cin >> N >> D;
st.resz(N);
for (int i = 0; i < N; i++) {
int x;
cin >> x;
st.upd(i, i, x);
}
//cout << st.get(2, 3).dp[0][1] << '\n';
while (D--) {
int l, r, x;
cin >> l >> r >> x;
l--, r--;
st.upd(l, r, x);
cout << getter(0, N - 1) << '\n';
}
}
Compilation message
Main.cpp: In member function 'void segmentTree::init(int, int, int)':
Main.cpp:92:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | if (dum >= vec.size()) {
| ~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |