#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 200005;
int n, m, arr[N], st[N][20], lg[N];
vector<int> pos[N];
int vv[N], dep[N], top, up[N][20], rt;
ll dp[N], bit[N], sum[N];
int tin[N], tout[N], tid;
vector<int> adj[N];
vector<pair<int, int>> stuff[N];
void update(int i, ll x) {
while (i <= n) {
bit[i] += x;
i += i & -i;
}
}
ll query(int i) {
ll ret = 0;
while (i) {
ret += bit[i];
i -= i & -i;
}
return ret;
}
int build(int l, int r){
if(r<l) return -1;
int tmp = lg[r-l+1];
int mx = max(st[l][tmp], st[r-(1<<tmp)+1][tmp]);
vector<int> cur_pos = {l-1};
for(auto i = lower_bound(pos[mx].begin(), pos[mx].end(), l)-pos[mx].begin(); i<(int)pos[mx].size() && pos[mx][i]<=r; ++i)
cur_pos.push_back(pos[mx][i]);
cur_pos.push_back(r+1);
vector<int> cur_child;
for(int i = 0; i<(int)cur_pos.size()-1; ++i){
cur_child.push_back(build(cur_pos[i]+1, cur_pos[i+1]-1));
if(cur_child.back()==-1) cur_child.pop_back();
}
for(auto x : cur_child)
adj[top].push_back(x);
for(auto x : cur_pos)
if(x!=l-1 && x!=r+1)
vv[x] = top;
dep[top] = mx;
++top; return top-1;
}
void dfs1(int v, int p){
up[v][0] = p;
for(int i = 1; i<20; ++i)
up[v][i] = up[up[v][i-1]][i-1];
tin[v] = ++tid;
for(auto u : adj[v])
dfs1(u, v);
tout[v] = tid;
}
void dfs2(int x) {
for (int i : adj[x]) {
dfs2(i);
sum[x] += dp[i];
}
for (auto [y, c] : stuff[x]) {
dp[x] = max(dp[x], query(tin[y]) + c);
}
update(tin[x], -dp[x]);
update(tout[x] + 1, dp[x]);
dp[x] += sum[x];
}
int main(){
// freopen("a.in", "r", stdin);
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 1; i<=n; ++i)
cin >> arr[i], pos[arr[i]].push_back(i);
for(int i = 1; i<=n; ++i)
st[i][0] = arr[i];
for(int i = n; i>=1; --i)
for(int j = 1; j<20; ++j)
if(i+(1<<j)-1<=n) st[i][j] = max(st[i][j-1], st[i+(1<<(j-1))][j-1]);
for(int i = 1; i<=n/2; ++i)
lg[i<<1] = lg[i<<1|1] = lg[i]+1;
rt = build(1, n); cin >> m; ll sum = 0;
dfs1(rt, rt);
while(m--){
int x, y, c; cin >> x >> y >> c;
sum += c;
int tmp = vv[x];
int cur = tmp;
for(int j = 19; j>=0; --j)
if(dep[up[cur][j]]<y) cur = up[cur][j];
stuff[cur].push_back({tmp, c});
}
dfs2(rt);
cout << sum-dp[rt] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14552 KB |
Output is correct |
2 |
Correct |
8 ms |
14540 KB |
Output is correct |
3 |
Correct |
8 ms |
14424 KB |
Output is correct |
4 |
Correct |
8 ms |
14556 KB |
Output is correct |
5 |
Correct |
11 ms |
14428 KB |
Output is correct |
6 |
Correct |
8 ms |
14432 KB |
Output is correct |
7 |
Correct |
8 ms |
14540 KB |
Output is correct |
8 |
Correct |
8 ms |
14540 KB |
Output is correct |
9 |
Correct |
7 ms |
14540 KB |
Output is correct |
10 |
Correct |
9 ms |
14556 KB |
Output is correct |
11 |
Correct |
8 ms |
14540 KB |
Output is correct |
12 |
Correct |
7 ms |
14520 KB |
Output is correct |
13 |
Correct |
8 ms |
14540 KB |
Output is correct |
14 |
Correct |
7 ms |
14412 KB |
Output is correct |
15 |
Correct |
10 ms |
14424 KB |
Output is correct |
16 |
Correct |
9 ms |
14552 KB |
Output is correct |
17 |
Correct |
9 ms |
14472 KB |
Output is correct |
18 |
Correct |
10 ms |
14540 KB |
Output is correct |
19 |
Correct |
10 ms |
14424 KB |
Output is correct |
20 |
Correct |
7 ms |
14428 KB |
Output is correct |
21 |
Correct |
8 ms |
14540 KB |
Output is correct |
22 |
Correct |
10 ms |
14544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14552 KB |
Output is correct |
2 |
Correct |
8 ms |
14540 KB |
Output is correct |
3 |
Correct |
8 ms |
14424 KB |
Output is correct |
4 |
Correct |
8 ms |
14556 KB |
Output is correct |
5 |
Correct |
11 ms |
14428 KB |
Output is correct |
6 |
Correct |
8 ms |
14432 KB |
Output is correct |
7 |
Correct |
8 ms |
14540 KB |
Output is correct |
8 |
Correct |
8 ms |
14540 KB |
Output is correct |
9 |
Correct |
7 ms |
14540 KB |
Output is correct |
10 |
Correct |
9 ms |
14556 KB |
Output is correct |
11 |
Correct |
8 ms |
14540 KB |
Output is correct |
12 |
Correct |
7 ms |
14520 KB |
Output is correct |
13 |
Correct |
8 ms |
14540 KB |
Output is correct |
14 |
Correct |
7 ms |
14412 KB |
Output is correct |
15 |
Correct |
10 ms |
14424 KB |
Output is correct |
16 |
Correct |
9 ms |
14552 KB |
Output is correct |
17 |
Correct |
9 ms |
14472 KB |
Output is correct |
18 |
Correct |
10 ms |
14540 KB |
Output is correct |
19 |
Correct |
10 ms |
14424 KB |
Output is correct |
20 |
Correct |
7 ms |
14428 KB |
Output is correct |
21 |
Correct |
8 ms |
14540 KB |
Output is correct |
22 |
Correct |
10 ms |
14544 KB |
Output is correct |
23 |
Correct |
11 ms |
14976 KB |
Output is correct |
24 |
Correct |
10 ms |
14900 KB |
Output is correct |
25 |
Correct |
12 ms |
14904 KB |
Output is correct |
26 |
Correct |
9 ms |
14952 KB |
Output is correct |
27 |
Correct |
12 ms |
14924 KB |
Output is correct |
28 |
Correct |
9 ms |
14924 KB |
Output is correct |
29 |
Correct |
9 ms |
14948 KB |
Output is correct |
30 |
Correct |
11 ms |
14924 KB |
Output is correct |
31 |
Correct |
12 ms |
14948 KB |
Output is correct |
32 |
Correct |
11 ms |
15200 KB |
Output is correct |
33 |
Correct |
10 ms |
15180 KB |
Output is correct |
34 |
Correct |
10 ms |
15072 KB |
Output is correct |
35 |
Correct |
10 ms |
15076 KB |
Output is correct |
36 |
Correct |
8 ms |
14668 KB |
Output is correct |
37 |
Correct |
8 ms |
14692 KB |
Output is correct |
38 |
Correct |
9 ms |
15332 KB |
Output is correct |
39 |
Correct |
9 ms |
14824 KB |
Output is correct |
40 |
Correct |
9 ms |
15180 KB |
Output is correct |
41 |
Correct |
9 ms |
14820 KB |
Output is correct |
42 |
Correct |
9 ms |
14796 KB |
Output is correct |
43 |
Correct |
9 ms |
15180 KB |
Output is correct |
44 |
Correct |
10 ms |
14796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14552 KB |
Output is correct |
2 |
Correct |
8 ms |
14540 KB |
Output is correct |
3 |
Correct |
8 ms |
14424 KB |
Output is correct |
4 |
Correct |
8 ms |
14556 KB |
Output is correct |
5 |
Correct |
11 ms |
14428 KB |
Output is correct |
6 |
Correct |
8 ms |
14432 KB |
Output is correct |
7 |
Correct |
8 ms |
14540 KB |
Output is correct |
8 |
Correct |
8 ms |
14540 KB |
Output is correct |
9 |
Correct |
7 ms |
14540 KB |
Output is correct |
10 |
Correct |
9 ms |
14556 KB |
Output is correct |
11 |
Correct |
8 ms |
14540 KB |
Output is correct |
12 |
Correct |
7 ms |
14520 KB |
Output is correct |
13 |
Correct |
8 ms |
14540 KB |
Output is correct |
14 |
Correct |
7 ms |
14412 KB |
Output is correct |
15 |
Correct |
10 ms |
14424 KB |
Output is correct |
16 |
Correct |
9 ms |
14552 KB |
Output is correct |
17 |
Correct |
9 ms |
14472 KB |
Output is correct |
18 |
Correct |
10 ms |
14540 KB |
Output is correct |
19 |
Correct |
10 ms |
14424 KB |
Output is correct |
20 |
Correct |
7 ms |
14428 KB |
Output is correct |
21 |
Correct |
8 ms |
14540 KB |
Output is correct |
22 |
Correct |
10 ms |
14544 KB |
Output is correct |
23 |
Correct |
11 ms |
14976 KB |
Output is correct |
24 |
Correct |
10 ms |
14900 KB |
Output is correct |
25 |
Correct |
12 ms |
14904 KB |
Output is correct |
26 |
Correct |
9 ms |
14952 KB |
Output is correct |
27 |
Correct |
12 ms |
14924 KB |
Output is correct |
28 |
Correct |
9 ms |
14924 KB |
Output is correct |
29 |
Correct |
9 ms |
14948 KB |
Output is correct |
30 |
Correct |
11 ms |
14924 KB |
Output is correct |
31 |
Correct |
12 ms |
14948 KB |
Output is correct |
32 |
Correct |
11 ms |
15200 KB |
Output is correct |
33 |
Correct |
10 ms |
15180 KB |
Output is correct |
34 |
Correct |
10 ms |
15072 KB |
Output is correct |
35 |
Correct |
10 ms |
15076 KB |
Output is correct |
36 |
Correct |
8 ms |
14668 KB |
Output is correct |
37 |
Correct |
8 ms |
14692 KB |
Output is correct |
38 |
Correct |
9 ms |
15332 KB |
Output is correct |
39 |
Correct |
9 ms |
14824 KB |
Output is correct |
40 |
Correct |
9 ms |
15180 KB |
Output is correct |
41 |
Correct |
9 ms |
14820 KB |
Output is correct |
42 |
Correct |
9 ms |
14796 KB |
Output is correct |
43 |
Correct |
9 ms |
15180 KB |
Output is correct |
44 |
Correct |
10 ms |
14796 KB |
Output is correct |
45 |
Correct |
370 ms |
72020 KB |
Output is correct |
46 |
Correct |
328 ms |
71520 KB |
Output is correct |
47 |
Correct |
326 ms |
70908 KB |
Output is correct |
48 |
Correct |
346 ms |
72260 KB |
Output is correct |
49 |
Correct |
327 ms |
70368 KB |
Output is correct |
50 |
Correct |
386 ms |
70228 KB |
Output is correct |
51 |
Correct |
339 ms |
70392 KB |
Output is correct |
52 |
Correct |
376 ms |
71640 KB |
Output is correct |
53 |
Correct |
336 ms |
71620 KB |
Output is correct |
54 |
Correct |
446 ms |
95116 KB |
Output is correct |
55 |
Correct |
460 ms |
87784 KB |
Output is correct |
56 |
Correct |
426 ms |
84188 KB |
Output is correct |
57 |
Correct |
372 ms |
81484 KB |
Output is correct |
58 |
Correct |
162 ms |
41084 KB |
Output is correct |
59 |
Correct |
130 ms |
41100 KB |
Output is correct |
60 |
Correct |
230 ms |
106584 KB |
Output is correct |
61 |
Correct |
247 ms |
59332 KB |
Output is correct |
62 |
Correct |
439 ms |
87396 KB |
Output is correct |
63 |
Correct |
226 ms |
54792 KB |
Output is correct |
64 |
Correct |
246 ms |
57752 KB |
Output is correct |
65 |
Correct |
458 ms |
88148 KB |
Output is correct |
66 |
Correct |
258 ms |
54112 KB |
Output is correct |