#include <bits/stdc++.h>
using namespace std;
typedef long long llong;
int n;
int A[200001];
vector<int> H[200001];
int I[200002];
int D[200001];
int V[200001];
vector<int> child[200001];
void add(int &ord, int p, int l, int r) {
if (r - l - 1 <= 0) return;
int i = ++ord;
child[p].push_back(i);
I[l] = i;
}
const int L = 18;
int st[200001], ed[200001], dep[200001], par[200001][L];
void dfs(int x, int p) {
static int ord = 0;
st[x] = ++ord;
par[x][0] = p;
for (int i = 1; i < L; ++i) par[x][i] = par[par[x][i - 1]][i - 1];
for (int i : child[x]) {
dfs(i, x);
}
ed[x] = ord;
}
vector<pair<int, int>> C[200001];
llong dp[200001], sum[200001];
llong fen[200001];
void update(int i, llong x) {
while (i <= n) {
fen[i] += x;
i += i & -i;
}
}
llong query(int i) {
llong ret = 0;
while (i) {
ret += fen[i];
i -= i & -i;
}
return ret;
}
void dfs2(int x, int p) {
for (int i : child[x]) {
dfs2(i, x);
sum[x] += dp[i];
}
for (auto [y, c] : C[x]) {
dp[x] = max(dp[x], query(st[y]) + c);
}
update(st[x], -dp[x]);
update(ed[x] + 1, dp[x]);
dp[x] += sum[x];
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> A[i];
H[A[i]].push_back(i);
}
set<int> cut;
cut.insert(0);
cut.insert(n + 1);
int ord = 0;
D[0] = n;
I[0] = ++ord;
for (int h = n; h > 0; --h) {
for (int i = 0; i < int(H[h].size()); ) {
int x = H[h][i];
auto it = cut.lower_bound(x);
int l = *prev(it), r = *it;
int p = I[l];
D[p] = h;
while (i < int(H[h].size()) && H[h][i] < r) {
add(ord, p, l, H[h][i]);
cut.insert(l = H[h][i]);
V[l] = p;
++i;
}
add(ord, p, l, r);
}
}
dfs(1, 0);
int q;
cin >> q;
llong sum = 0;
while (q--) {
int x, y, c;
cin >> x >> y >> c;
sum += c;
int dn = V[x];
int up = dn;
for (int i = L; i--; ) if (D[par[up][i]] < y) up = par[up][i];
C[up].emplace_back(dn, c);
}
dfs2(1, 0);
printf("%lld\n", sum - dp[1]);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14720 KB |
Output is correct |
2 |
Correct |
9 ms |
14592 KB |
Output is correct |
3 |
Correct |
9 ms |
14592 KB |
Output is correct |
4 |
Correct |
9 ms |
14592 KB |
Output is correct |
5 |
Correct |
10 ms |
14592 KB |
Output is correct |
6 |
Correct |
9 ms |
14592 KB |
Output is correct |
7 |
Correct |
9 ms |
14592 KB |
Output is correct |
8 |
Correct |
9 ms |
14592 KB |
Output is correct |
9 |
Correct |
9 ms |
14592 KB |
Output is correct |
10 |
Correct |
9 ms |
14592 KB |
Output is correct |
11 |
Correct |
9 ms |
14592 KB |
Output is correct |
12 |
Correct |
9 ms |
14592 KB |
Output is correct |
13 |
Correct |
12 ms |
14592 KB |
Output is correct |
14 |
Correct |
9 ms |
14592 KB |
Output is correct |
15 |
Correct |
9 ms |
14592 KB |
Output is correct |
16 |
Correct |
9 ms |
14592 KB |
Output is correct |
17 |
Correct |
9 ms |
14592 KB |
Output is correct |
18 |
Correct |
11 ms |
14592 KB |
Output is correct |
19 |
Correct |
10 ms |
14592 KB |
Output is correct |
20 |
Correct |
9 ms |
14592 KB |
Output is correct |
21 |
Correct |
10 ms |
14512 KB |
Output is correct |
22 |
Correct |
9 ms |
14592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14720 KB |
Output is correct |
2 |
Correct |
9 ms |
14592 KB |
Output is correct |
3 |
Correct |
9 ms |
14592 KB |
Output is correct |
4 |
Correct |
9 ms |
14592 KB |
Output is correct |
5 |
Correct |
10 ms |
14592 KB |
Output is correct |
6 |
Correct |
9 ms |
14592 KB |
Output is correct |
7 |
Correct |
9 ms |
14592 KB |
Output is correct |
8 |
Correct |
9 ms |
14592 KB |
Output is correct |
9 |
Correct |
9 ms |
14592 KB |
Output is correct |
10 |
Correct |
9 ms |
14592 KB |
Output is correct |
11 |
Correct |
9 ms |
14592 KB |
Output is correct |
12 |
Correct |
9 ms |
14592 KB |
Output is correct |
13 |
Correct |
12 ms |
14592 KB |
Output is correct |
14 |
Correct |
9 ms |
14592 KB |
Output is correct |
15 |
Correct |
9 ms |
14592 KB |
Output is correct |
16 |
Correct |
9 ms |
14592 KB |
Output is correct |
17 |
Correct |
9 ms |
14592 KB |
Output is correct |
18 |
Correct |
11 ms |
14592 KB |
Output is correct |
19 |
Correct |
10 ms |
14592 KB |
Output is correct |
20 |
Correct |
9 ms |
14592 KB |
Output is correct |
21 |
Correct |
10 ms |
14512 KB |
Output is correct |
22 |
Correct |
9 ms |
14592 KB |
Output is correct |
23 |
Correct |
11 ms |
14976 KB |
Output is correct |
24 |
Correct |
12 ms |
14976 KB |
Output is correct |
25 |
Correct |
12 ms |
14976 KB |
Output is correct |
26 |
Correct |
12 ms |
14976 KB |
Output is correct |
27 |
Correct |
12 ms |
14976 KB |
Output is correct |
28 |
Correct |
12 ms |
14976 KB |
Output is correct |
29 |
Correct |
12 ms |
15104 KB |
Output is correct |
30 |
Correct |
12 ms |
14976 KB |
Output is correct |
31 |
Correct |
11 ms |
14976 KB |
Output is correct |
32 |
Correct |
11 ms |
15104 KB |
Output is correct |
33 |
Correct |
11 ms |
15104 KB |
Output is correct |
34 |
Correct |
11 ms |
14976 KB |
Output is correct |
35 |
Correct |
11 ms |
14976 KB |
Output is correct |
36 |
Correct |
10 ms |
14720 KB |
Output is correct |
37 |
Correct |
10 ms |
14720 KB |
Output is correct |
38 |
Correct |
11 ms |
15104 KB |
Output is correct |
39 |
Correct |
11 ms |
14848 KB |
Output is correct |
40 |
Correct |
11 ms |
14976 KB |
Output is correct |
41 |
Correct |
11 ms |
14848 KB |
Output is correct |
42 |
Correct |
10 ms |
14848 KB |
Output is correct |
43 |
Correct |
11 ms |
14976 KB |
Output is correct |
44 |
Correct |
11 ms |
14848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14720 KB |
Output is correct |
2 |
Correct |
9 ms |
14592 KB |
Output is correct |
3 |
Correct |
9 ms |
14592 KB |
Output is correct |
4 |
Correct |
9 ms |
14592 KB |
Output is correct |
5 |
Correct |
10 ms |
14592 KB |
Output is correct |
6 |
Correct |
9 ms |
14592 KB |
Output is correct |
7 |
Correct |
9 ms |
14592 KB |
Output is correct |
8 |
Correct |
9 ms |
14592 KB |
Output is correct |
9 |
Correct |
9 ms |
14592 KB |
Output is correct |
10 |
Correct |
9 ms |
14592 KB |
Output is correct |
11 |
Correct |
9 ms |
14592 KB |
Output is correct |
12 |
Correct |
9 ms |
14592 KB |
Output is correct |
13 |
Correct |
12 ms |
14592 KB |
Output is correct |
14 |
Correct |
9 ms |
14592 KB |
Output is correct |
15 |
Correct |
9 ms |
14592 KB |
Output is correct |
16 |
Correct |
9 ms |
14592 KB |
Output is correct |
17 |
Correct |
9 ms |
14592 KB |
Output is correct |
18 |
Correct |
11 ms |
14592 KB |
Output is correct |
19 |
Correct |
10 ms |
14592 KB |
Output is correct |
20 |
Correct |
9 ms |
14592 KB |
Output is correct |
21 |
Correct |
10 ms |
14512 KB |
Output is correct |
22 |
Correct |
9 ms |
14592 KB |
Output is correct |
23 |
Correct |
11 ms |
14976 KB |
Output is correct |
24 |
Correct |
12 ms |
14976 KB |
Output is correct |
25 |
Correct |
12 ms |
14976 KB |
Output is correct |
26 |
Correct |
12 ms |
14976 KB |
Output is correct |
27 |
Correct |
12 ms |
14976 KB |
Output is correct |
28 |
Correct |
12 ms |
14976 KB |
Output is correct |
29 |
Correct |
12 ms |
15104 KB |
Output is correct |
30 |
Correct |
12 ms |
14976 KB |
Output is correct |
31 |
Correct |
11 ms |
14976 KB |
Output is correct |
32 |
Correct |
11 ms |
15104 KB |
Output is correct |
33 |
Correct |
11 ms |
15104 KB |
Output is correct |
34 |
Correct |
11 ms |
14976 KB |
Output is correct |
35 |
Correct |
11 ms |
14976 KB |
Output is correct |
36 |
Correct |
10 ms |
14720 KB |
Output is correct |
37 |
Correct |
10 ms |
14720 KB |
Output is correct |
38 |
Correct |
11 ms |
15104 KB |
Output is correct |
39 |
Correct |
11 ms |
14848 KB |
Output is correct |
40 |
Correct |
11 ms |
14976 KB |
Output is correct |
41 |
Correct |
11 ms |
14848 KB |
Output is correct |
42 |
Correct |
10 ms |
14848 KB |
Output is correct |
43 |
Correct |
11 ms |
14976 KB |
Output is correct |
44 |
Correct |
11 ms |
14848 KB |
Output is correct |
45 |
Correct |
633 ms |
64428 KB |
Output is correct |
46 |
Correct |
615 ms |
63904 KB |
Output is correct |
47 |
Correct |
618 ms |
63352 KB |
Output is correct |
48 |
Correct |
664 ms |
64248 KB |
Output is correct |
49 |
Correct |
604 ms |
62968 KB |
Output is correct |
50 |
Correct |
624 ms |
62968 KB |
Output is correct |
51 |
Correct |
640 ms |
62968 KB |
Output is correct |
52 |
Correct |
692 ms |
64120 KB |
Output is correct |
53 |
Correct |
612 ms |
64120 KB |
Output is correct |
54 |
Correct |
540 ms |
74872 KB |
Output is correct |
55 |
Correct |
513 ms |
71292 KB |
Output is correct |
56 |
Correct |
520 ms |
69668 KB |
Output is correct |
57 |
Correct |
496 ms |
68164 KB |
Output is correct |
58 |
Correct |
189 ms |
34936 KB |
Output is correct |
59 |
Correct |
181 ms |
34936 KB |
Output is correct |
60 |
Correct |
291 ms |
79224 KB |
Output is correct |
61 |
Correct |
325 ms |
52088 KB |
Output is correct |
62 |
Correct |
477 ms |
67448 KB |
Output is correct |
63 |
Correct |
288 ms |
47736 KB |
Output is correct |
64 |
Correct |
318 ms |
51004 KB |
Output is correct |
65 |
Correct |
537 ms |
67892 KB |
Output is correct |
66 |
Correct |
324 ms |
47352 KB |
Output is correct |