#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sz(v) int(v.size())
#define ar array
const int N = 1e5+10, MOD = 1e9+7;
const ll INF = 1e18+10;
struct T {
T *l, *r;
ll sum;
T() {
l = r = nullptr;
sum = 0;
}
void upd(int tl, int tr, int pos, ll x) {
sum += x;
if (tl == tr) {
return;
}
int tm = (tl + tr) / 2;
if (pos <= tm) {
if (!l) l = new T();
l->upd(tl, tm, pos, x);
} else {
if (!r) r = new T();
r->upd(tm+1, tr, pos, x);
}
}
ll qry(int tl, int tr, int ql, int qr) {
if (qr < tl || ql > tr) return 0;
if (ql <= tl && tr <= qr) {
return sum;
}
int tm = (tl + tr) / 2;
return (l ? l->qry(tl, tm, ql, qr) : 0) + (r ? r->qry(tm+1, tr, ql, qr) : 0);
}
};
const int BASE = 1e9+20;
void upd(T* tree, int l, int r, ll x) {
tree->upd(0, BASE, l, +x);
tree->upd(0, BASE, r+1, -x);
}
ll qry(T* tree, int pos) {
return tree->qry(0, BASE, 0, pos);
}
struct S {
// i must be >= the max
map<int, ll> mp;
T* tree;
S() {
mp[0] = 0;
tree = new T();
}
void add_range(int l, int r, ll x) {
upd(tree, l, r, x);
/*
for (auto& [k, v] : mp) {
if (l <= k && k <= r) {
v += x;
}
}
*/
}
void merge(S& nxt) {
if (sz(mp) < sz(nxt.mp)) {
swap(mp, nxt.mp);
swap(tree, nxt.tree);
}
for (auto [k, v] : nxt.mp) {
auto it = --mp.upper_bound(k);
ll use = it->second + qry(tree, it->first);
mp[k] = use - qry(tree, k);
}
for (auto one = nxt.mp.begin(); one != nxt.mp.end(); one++) {
auto two = next(one);
if (two == nxt.mp.end()) {
// one->first, to the end
add_range(one->first, MOD, one->second + qry(nxt.tree, one->first));
} else {
// one->first, two->first-1
add_range(one->first, two->first - 1, one->second + qry(nxt.tree, one->first));
}
}
}
void add(int t, int x) {
auto base = --mp.upper_bound(t);
ll cur = base->second + qry(tree, base->first) + x;
auto it = mp.lower_bound(t);
while (it != mp.end() && it->second + qry(tree, it->first) <= cur) {
mp.erase(it);
it = mp.lower_bound(t);
}
mp[t] = cur - qry(tree, t);
}
ll get_ans() {
auto it = prev(mp.end());
return it->second + qry(tree, it->first);
}
} s[N];
int n, m, k;
vector<int> adj[N];
int t[N], a[N];
// value of ancestor has to be >= value of any thing in the subtree
void dfs(int c, int p) {
s[c] = S();
for (int nxt : adj[c]) if (nxt != p) {
dfs(nxt, c);
s[c].merge(s[nxt]);
}
if (t[c] != -1) s[c].add(t[c], a[c]);
}
void solve() {
cin >> n >> m >> k;
for (int i = 1; i < n; i++) {
int p; cin >> p, --p;
adj[p].push_back(i);
}
memset(t, -1, sizeof(t));
memset(a, -1, sizeof(a));
while (m--) {
int c, i, x; cin >> c >> i >> x, --c;
t[c] = i, a[c] = x;
}
dfs(0, -1);
cout << s[0].get_ans() << '\n';
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T = 1;
// cin >> T;
while (T--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
18260 KB |
Output is correct |
2 |
Correct |
14 ms |
18320 KB |
Output is correct |
3 |
Correct |
11 ms |
18260 KB |
Output is correct |
4 |
Correct |
11 ms |
18328 KB |
Output is correct |
5 |
Correct |
14 ms |
18328 KB |
Output is correct |
6 |
Correct |
13 ms |
18260 KB |
Output is correct |
7 |
Correct |
11 ms |
18332 KB |
Output is correct |
8 |
Correct |
11 ms |
18260 KB |
Output is correct |
9 |
Correct |
16 ms |
18260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
343 ms |
122088 KB |
Output is correct |
2 |
Correct |
115 ms |
37196 KB |
Output is correct |
3 |
Correct |
614 ms |
141132 KB |
Output is correct |
4 |
Correct |
222 ms |
37920 KB |
Output is correct |
5 |
Correct |
285 ms |
40028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
18520 KB |
Output is correct |
2 |
Correct |
16 ms |
18516 KB |
Output is correct |
3 |
Correct |
13 ms |
18444 KB |
Output is correct |
4 |
Correct |
125 ms |
38660 KB |
Output is correct |
5 |
Correct |
143 ms |
42540 KB |
Output is correct |
6 |
Correct |
141 ms |
38684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
261 ms |
75608 KB |
Output is correct |
2 |
Correct |
203 ms |
76780 KB |
Output is correct |
3 |
Correct |
136 ms |
32716 KB |
Output is correct |
4 |
Correct |
109 ms |
29172 KB |
Output is correct |
5 |
Correct |
134 ms |
40704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
18260 KB |
Output is correct |
2 |
Correct |
14 ms |
18320 KB |
Output is correct |
3 |
Correct |
11 ms |
18260 KB |
Output is correct |
4 |
Correct |
11 ms |
18328 KB |
Output is correct |
5 |
Correct |
14 ms |
18328 KB |
Output is correct |
6 |
Correct |
13 ms |
18260 KB |
Output is correct |
7 |
Correct |
11 ms |
18332 KB |
Output is correct |
8 |
Correct |
11 ms |
18260 KB |
Output is correct |
9 |
Correct |
16 ms |
18260 KB |
Output is correct |
10 |
Correct |
259 ms |
83280 KB |
Output is correct |
11 |
Correct |
227 ms |
80728 KB |
Output is correct |
12 |
Correct |
147 ms |
32108 KB |
Output is correct |
13 |
Correct |
105 ms |
28572 KB |
Output is correct |
14 |
Correct |
126 ms |
40064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
37112 KB |
Output is correct |
2 |
Correct |
173 ms |
115800 KB |
Output is correct |
3 |
Correct |
171 ms |
115520 KB |
Output is correct |
4 |
Correct |
178 ms |
115080 KB |
Output is correct |
5 |
Correct |
63 ms |
22404 KB |
Output is correct |
6 |
Correct |
94 ms |
26828 KB |
Output is correct |
7 |
Correct |
80 ms |
30784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
18260 KB |
Output is correct |
2 |
Correct |
14 ms |
18320 KB |
Output is correct |
3 |
Correct |
11 ms |
18260 KB |
Output is correct |
4 |
Correct |
11 ms |
18328 KB |
Output is correct |
5 |
Correct |
14 ms |
18328 KB |
Output is correct |
6 |
Correct |
13 ms |
18260 KB |
Output is correct |
7 |
Correct |
11 ms |
18332 KB |
Output is correct |
8 |
Correct |
11 ms |
18260 KB |
Output is correct |
9 |
Correct |
16 ms |
18260 KB |
Output is correct |
10 |
Correct |
13 ms |
18520 KB |
Output is correct |
11 |
Correct |
16 ms |
18516 KB |
Output is correct |
12 |
Correct |
13 ms |
18444 KB |
Output is correct |
13 |
Correct |
125 ms |
38660 KB |
Output is correct |
14 |
Correct |
143 ms |
42540 KB |
Output is correct |
15 |
Correct |
141 ms |
38684 KB |
Output is correct |
16 |
Correct |
259 ms |
83280 KB |
Output is correct |
17 |
Correct |
227 ms |
80728 KB |
Output is correct |
18 |
Correct |
147 ms |
32108 KB |
Output is correct |
19 |
Correct |
105 ms |
28572 KB |
Output is correct |
20 |
Correct |
126 ms |
40064 KB |
Output is correct |
21 |
Correct |
85 ms |
36844 KB |
Output is correct |
22 |
Correct |
283 ms |
104800 KB |
Output is correct |
23 |
Correct |
338 ms |
129152 KB |
Output is correct |
24 |
Correct |
468 ms |
152764 KB |
Output is correct |
25 |
Correct |
198 ms |
37372 KB |
Output is correct |
26 |
Correct |
254 ms |
41356 KB |
Output is correct |
27 |
Correct |
205 ms |
37324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
18260 KB |
Output is correct |
2 |
Correct |
14 ms |
18320 KB |
Output is correct |
3 |
Correct |
11 ms |
18260 KB |
Output is correct |
4 |
Correct |
11 ms |
18328 KB |
Output is correct |
5 |
Correct |
14 ms |
18328 KB |
Output is correct |
6 |
Correct |
13 ms |
18260 KB |
Output is correct |
7 |
Correct |
11 ms |
18332 KB |
Output is correct |
8 |
Correct |
11 ms |
18260 KB |
Output is correct |
9 |
Correct |
16 ms |
18260 KB |
Output is correct |
10 |
Correct |
343 ms |
122088 KB |
Output is correct |
11 |
Correct |
115 ms |
37196 KB |
Output is correct |
12 |
Correct |
614 ms |
141132 KB |
Output is correct |
13 |
Correct |
222 ms |
37920 KB |
Output is correct |
14 |
Correct |
285 ms |
40028 KB |
Output is correct |
15 |
Correct |
13 ms |
18520 KB |
Output is correct |
16 |
Correct |
16 ms |
18516 KB |
Output is correct |
17 |
Correct |
13 ms |
18444 KB |
Output is correct |
18 |
Correct |
125 ms |
38660 KB |
Output is correct |
19 |
Correct |
143 ms |
42540 KB |
Output is correct |
20 |
Correct |
141 ms |
38684 KB |
Output is correct |
21 |
Correct |
261 ms |
75608 KB |
Output is correct |
22 |
Correct |
203 ms |
76780 KB |
Output is correct |
23 |
Correct |
136 ms |
32716 KB |
Output is correct |
24 |
Correct |
109 ms |
29172 KB |
Output is correct |
25 |
Correct |
134 ms |
40704 KB |
Output is correct |
26 |
Correct |
259 ms |
83280 KB |
Output is correct |
27 |
Correct |
227 ms |
80728 KB |
Output is correct |
28 |
Correct |
147 ms |
32108 KB |
Output is correct |
29 |
Correct |
105 ms |
28572 KB |
Output is correct |
30 |
Correct |
126 ms |
40064 KB |
Output is correct |
31 |
Correct |
44 ms |
37112 KB |
Output is correct |
32 |
Correct |
173 ms |
115800 KB |
Output is correct |
33 |
Correct |
171 ms |
115520 KB |
Output is correct |
34 |
Correct |
178 ms |
115080 KB |
Output is correct |
35 |
Correct |
63 ms |
22404 KB |
Output is correct |
36 |
Correct |
94 ms |
26828 KB |
Output is correct |
37 |
Correct |
80 ms |
30784 KB |
Output is correct |
38 |
Correct |
85 ms |
36844 KB |
Output is correct |
39 |
Correct |
283 ms |
104800 KB |
Output is correct |
40 |
Correct |
338 ms |
129152 KB |
Output is correct |
41 |
Correct |
468 ms |
152764 KB |
Output is correct |
42 |
Correct |
198 ms |
37372 KB |
Output is correct |
43 |
Correct |
254 ms |
41356 KB |
Output is correct |
44 |
Correct |
205 ms |
37324 KB |
Output is correct |
45 |
Correct |
97 ms |
37608 KB |
Output is correct |
46 |
Correct |
302 ms |
107024 KB |
Output is correct |
47 |
Correct |
357 ms |
133212 KB |
Output is correct |
48 |
Correct |
514 ms |
161440 KB |
Output is correct |
49 |
Correct |
191 ms |
37908 KB |
Output is correct |
50 |
Correct |
258 ms |
42256 KB |
Output is correct |
51 |
Correct |
210 ms |
38336 KB |
Output is correct |