#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 2e5 + 5;
const int lg = 20;
pair<int, int> h[mxN][lg];
int cur, up[mxN], lc[mxN], rc[mxN], low[mxN], out[mxN], par[mxN][lg];
vector<pair<int, int>> points[mxN];
long long dp[mxN][2];
long long bit[mxN];
int down[mxN], ll[mxN], rr[mxN];
pair<int, int> query(int l, int r) {
int x = 31 - __builtin_clz(r - l + 1);
return max(h[l][x], h[r - (1 << x) + 1][x]);
}
int build(int l, int r, int H, int p) {
if (l > r) return -1;
int x = ++cur;
ll[x] = l, rr[x] = r, down[x] = H;
auto [mx, idx] = query(l, r);
par[x][0] = p;
for (int j = 1; j < lg; j++) par[x][j] = par[par[x][j - 1]][j - 1];
lc[x] = build(l, idx - 1, mx, x);
rc[x] = build(idx + 1, r, mx, x);
low[idx] = x;
up[x] = mx + 1;
out[x] = cur;
return x;
}
void update(int i, long long v) {
for (; i < mxN; i += i & (-i)) bit[i] += v;
}
long long query(int i) {
long long r = 0;
for (; i > 0; i -= i & (-i)) r += bit[i];
return r;
}
void dfs(int u) {
if (lc[u] != -1) {
dfs(lc[u]);
dp[u][0] += dp[lc[u]][1];
}
if (rc[u] != -1) {
dfs(rc[u]);
dp[u][0] += dp[rc[u]][1];
}
dp[u][1] = dp[u][0];
for (auto [x, c] : points[u]) {
dp[u][1] = max(dp[u][1], dp[u][0] + query(low[x]) + c);
}
update(u, dp[u][0] - dp[u][1]);
update(out[u] + 1, dp[u][1] - dp[u][0]);
}
void testCase() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> h[i][0].first;
h[i][0].second = i;
}
for (int j = 1; j < lg; j++) {
for (int i = 1; i <= n - (1 << j) + 1; i++) {
h[i][j] = max(h[i][j - 1], h[i + (1 << (j - 1))][j - 1]);
}
}
par[1][0] = 1;
build(1, n, n, 1);
int m;
cin >> m;
long long sum = 0;
while (m--) {
int x, y, c;
cin >> x >> y >> c;
sum += c;
int _x = x;
x = low[x];
for (int j = lg - 1; j >= 0; j--) {
if (up[par[x][j]] <= y) x = par[x][j];
}
points[x].push_back({_x, c});
}
dfs(1);
cout << sum - dp[1][1] << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5128 KB |
Output is correct |
4 |
Correct |
3 ms |
5168 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5152 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5164 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5168 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5152 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
3 ms |
5204 KB |
Output is correct |
22 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5128 KB |
Output is correct |
4 |
Correct |
3 ms |
5168 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5152 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5164 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5168 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5152 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
3 ms |
5204 KB |
Output is correct |
22 |
Correct |
3 ms |
5204 KB |
Output is correct |
23 |
Correct |
4 ms |
5716 KB |
Output is correct |
24 |
Correct |
4 ms |
5716 KB |
Output is correct |
25 |
Correct |
4 ms |
5692 KB |
Output is correct |
26 |
Correct |
4 ms |
5684 KB |
Output is correct |
27 |
Correct |
5 ms |
5688 KB |
Output is correct |
28 |
Correct |
5 ms |
5688 KB |
Output is correct |
29 |
Correct |
5 ms |
5760 KB |
Output is correct |
30 |
Correct |
6 ms |
5684 KB |
Output is correct |
31 |
Correct |
5 ms |
5636 KB |
Output is correct |
32 |
Correct |
5 ms |
5688 KB |
Output is correct |
33 |
Correct |
5 ms |
5716 KB |
Output is correct |
34 |
Correct |
5 ms |
5716 KB |
Output is correct |
35 |
Correct |
6 ms |
5764 KB |
Output is correct |
36 |
Correct |
5 ms |
5716 KB |
Output is correct |
37 |
Correct |
5 ms |
5776 KB |
Output is correct |
38 |
Correct |
5 ms |
5756 KB |
Output is correct |
39 |
Correct |
5 ms |
5688 KB |
Output is correct |
40 |
Correct |
5 ms |
5716 KB |
Output is correct |
41 |
Correct |
5 ms |
5688 KB |
Output is correct |
42 |
Correct |
4 ms |
5724 KB |
Output is correct |
43 |
Correct |
5 ms |
5716 KB |
Output is correct |
44 |
Correct |
5 ms |
5716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5128 KB |
Output is correct |
4 |
Correct |
3 ms |
5168 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5152 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5164 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5168 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5152 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
3 ms |
5204 KB |
Output is correct |
22 |
Correct |
3 ms |
5204 KB |
Output is correct |
23 |
Correct |
4 ms |
5716 KB |
Output is correct |
24 |
Correct |
4 ms |
5716 KB |
Output is correct |
25 |
Correct |
4 ms |
5692 KB |
Output is correct |
26 |
Correct |
4 ms |
5684 KB |
Output is correct |
27 |
Correct |
5 ms |
5688 KB |
Output is correct |
28 |
Correct |
5 ms |
5688 KB |
Output is correct |
29 |
Correct |
5 ms |
5760 KB |
Output is correct |
30 |
Correct |
6 ms |
5684 KB |
Output is correct |
31 |
Correct |
5 ms |
5636 KB |
Output is correct |
32 |
Correct |
5 ms |
5688 KB |
Output is correct |
33 |
Correct |
5 ms |
5716 KB |
Output is correct |
34 |
Correct |
5 ms |
5716 KB |
Output is correct |
35 |
Correct |
6 ms |
5764 KB |
Output is correct |
36 |
Correct |
5 ms |
5716 KB |
Output is correct |
37 |
Correct |
5 ms |
5776 KB |
Output is correct |
38 |
Correct |
5 ms |
5756 KB |
Output is correct |
39 |
Correct |
5 ms |
5688 KB |
Output is correct |
40 |
Correct |
5 ms |
5716 KB |
Output is correct |
41 |
Correct |
5 ms |
5688 KB |
Output is correct |
42 |
Correct |
4 ms |
5724 KB |
Output is correct |
43 |
Correct |
5 ms |
5716 KB |
Output is correct |
44 |
Correct |
5 ms |
5716 KB |
Output is correct |
45 |
Correct |
296 ms |
71688 KB |
Output is correct |
46 |
Correct |
283 ms |
70988 KB |
Output is correct |
47 |
Correct |
261 ms |
70160 KB |
Output is correct |
48 |
Correct |
300 ms |
71656 KB |
Output is correct |
49 |
Correct |
288 ms |
69608 KB |
Output is correct |
50 |
Correct |
292 ms |
69704 KB |
Output is correct |
51 |
Correct |
274 ms |
69708 KB |
Output is correct |
52 |
Correct |
311 ms |
71216 KB |
Output is correct |
53 |
Correct |
268 ms |
71176 KB |
Output is correct |
54 |
Correct |
404 ms |
77744 KB |
Output is correct |
55 |
Correct |
402 ms |
74392 KB |
Output is correct |
56 |
Correct |
429 ms |
72804 KB |
Output is correct |
57 |
Correct |
425 ms |
71516 KB |
Output is correct |
58 |
Correct |
319 ms |
72808 KB |
Output is correct |
59 |
Correct |
376 ms |
72564 KB |
Output is correct |
60 |
Correct |
188 ms |
79984 KB |
Output is correct |
61 |
Correct |
269 ms |
70580 KB |
Output is correct |
62 |
Correct |
460 ms |
76388 KB |
Output is correct |
63 |
Correct |
245 ms |
69544 KB |
Output is correct |
64 |
Correct |
264 ms |
68340 KB |
Output is correct |
65 |
Correct |
422 ms |
77052 KB |
Output is correct |
66 |
Correct |
262 ms |
68848 KB |
Output is correct |