#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pld;
vector<pii> v1;
vector<pii> v2;
struct vertex{
ll left, right;
ll maxi = 0, lazy = 0, sz = 0;
vertex *lchild = nullptr, *rchild = nullptr;
vertex(){}
vertex(ll lb, ll rb){
left = lb;
right = rb;
}
void push(){
if(lchild){
lchild->lazy += lazy;
lchild->maxi += lazy;
}
if(rchild){
rchild->lazy += lazy;
rchild->maxi += lazy;
}
lazy = 0;
}
void pull(){
maxi = 0;
if(lchild)
maxi = max(maxi, lchild->maxi);
if(rchild)
maxi = max(maxi, rchild->maxi);
}
void add(ll idx, ll val){
++sz;
if(left == idx && right == idx){
maxi = max(maxi, val);
return;
}
push();
if(idx <= (left+right)/2){
if(!lchild)
lchild = new vertex(left, (left+right)/2);
lchild->add(idx, val);
}
else{
if(!rchild)
rchild = new vertex((left+right)/2+1, right);
rchild->add(idx, val);
}
pull();
}
void upd(ll l, ll r, ll val){
if(right < l || r < left)
return;
if(l <= left && right <= r){
maxi += val;
lazy += val;
return;
}
push();
if(lchild)
lchild->upd(l, r, val);
if(rchild)
rchild->upd(l, r, val);
pull();
}
ll get(ll l, ll r){
if(right < l || r < left)
return 0;
if(l <= left && right <= r){
return maxi;
}
push();
ll ret = 0;
if(lchild)
ret = max(ret, lchild->get(l, r));
if(rchild)
ret = max(ret, rchild->get(l, r));
return ret;
}
void dfs(){
if(left == right){
v1.pb({left, maxi});
if(v2.empty() || maxi > v2.back().ss)
v2.pb({left, maxi});
return;
}
push();
if(lchild)
lchild->dfs();
if(rchild)
rchild->dfs();
delete lchild;
delete rchild;
}
};
vector<ll> g[100009];
ll n, m, k;
pii val[100009];
void merge(vertex &a, vertex &b){
if(b.sz > a.sz)
swap(a, b);
v1.clear();
v2.clear();
b.dfs();
for(auto &u : v1){
u.ss += a.get(1, u.ff);
}
for(ll i = 0; i < v2.size(); ++i){
ll val;
if(i == 0)
val = v2[i].ss;
else
val = v2[i].ss-v2[i-1].ss;
a.upd(v2[i].ff, k, val);
}
for(auto u : v1){
a.add(u.ff, u.ss);
}
//cout << '\n';
}
vertex seg[100009];
void dfs(ll v){
seg[v] = vertex(1, k);
for(auto u : g[v]){
dfs(u);
merge(seg[v], seg[u]);
}
if(val[v].ff){
seg[v].add(val[v].ff, val[v].ss+seg[v].get(1, val[v].ff));
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n >> m >> k;
for(ll i = 2; i <= n; ++i){
ll p;
cin >> p;
g[p].pb(i);
}
for(ll i = 0; i < m; ++i){
ll v, d, w;
cin >> v >> d >> w;
val[v] = {d, w};
}
dfs(1);
cout << seg[1].maxi << '\n';
}
Compilation message
magictree.cpp: In function 'void merge(vertex&, vertex&)':
magictree.cpp:120:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for(ll i = 0; i < v2.size(); ++i){
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8192 KB |
Output is correct |
2 |
Correct |
5 ms |
8192 KB |
Output is correct |
3 |
Correct |
8 ms |
8192 KB |
Output is correct |
4 |
Correct |
5 ms |
8192 KB |
Output is correct |
5 |
Correct |
5 ms |
8192 KB |
Output is correct |
6 |
Correct |
5 ms |
8192 KB |
Output is correct |
7 |
Correct |
5 ms |
8192 KB |
Output is correct |
8 |
Correct |
5 ms |
8192 KB |
Output is correct |
9 |
Correct |
5 ms |
8192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
202 ms |
19060 KB |
Output is correct |
2 |
Correct |
104 ms |
21240 KB |
Output is correct |
3 |
Correct |
598 ms |
26352 KB |
Output is correct |
4 |
Correct |
246 ms |
20208 KB |
Output is correct |
5 |
Correct |
293 ms |
21368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8448 KB |
Output is correct |
2 |
Correct |
6 ms |
8448 KB |
Output is correct |
3 |
Correct |
5 ms |
8448 KB |
Output is correct |
4 |
Correct |
98 ms |
32248 KB |
Output is correct |
5 |
Correct |
101 ms |
32248 KB |
Output is correct |
6 |
Correct |
136 ms |
32248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
11768 KB |
Output is correct |
2 |
Correct |
74 ms |
11776 KB |
Output is correct |
3 |
Correct |
63 ms |
16248 KB |
Output is correct |
4 |
Correct |
48 ms |
10872 KB |
Output is correct |
5 |
Correct |
60 ms |
22392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8192 KB |
Output is correct |
2 |
Correct |
5 ms |
8192 KB |
Output is correct |
3 |
Correct |
8 ms |
8192 KB |
Output is correct |
4 |
Correct |
5 ms |
8192 KB |
Output is correct |
5 |
Correct |
5 ms |
8192 KB |
Output is correct |
6 |
Correct |
5 ms |
8192 KB |
Output is correct |
7 |
Correct |
5 ms |
8192 KB |
Output is correct |
8 |
Correct |
5 ms |
8192 KB |
Output is correct |
9 |
Correct |
5 ms |
8192 KB |
Output is correct |
10 |
Correct |
143 ms |
11768 KB |
Output is correct |
11 |
Correct |
149 ms |
11744 KB |
Output is correct |
12 |
Correct |
79 ms |
16248 KB |
Output is correct |
13 |
Correct |
72 ms |
10872 KB |
Output is correct |
14 |
Correct |
57 ms |
22392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
9344 KB |
Output is correct |
2 |
Correct |
49 ms |
12280 KB |
Output is correct |
3 |
Correct |
58 ms |
12408 KB |
Output is correct |
4 |
Correct |
62 ms |
12536 KB |
Output is correct |
5 |
Correct |
22 ms |
11128 KB |
Output is correct |
6 |
Correct |
42 ms |
14292 KB |
Output is correct |
7 |
Correct |
39 ms |
17016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8192 KB |
Output is correct |
2 |
Correct |
5 ms |
8192 KB |
Output is correct |
3 |
Correct |
8 ms |
8192 KB |
Output is correct |
4 |
Correct |
5 ms |
8192 KB |
Output is correct |
5 |
Correct |
5 ms |
8192 KB |
Output is correct |
6 |
Correct |
5 ms |
8192 KB |
Output is correct |
7 |
Correct |
5 ms |
8192 KB |
Output is correct |
8 |
Correct |
5 ms |
8192 KB |
Output is correct |
9 |
Correct |
5 ms |
8192 KB |
Output is correct |
10 |
Correct |
6 ms |
8448 KB |
Output is correct |
11 |
Correct |
6 ms |
8448 KB |
Output is correct |
12 |
Correct |
5 ms |
8448 KB |
Output is correct |
13 |
Correct |
98 ms |
32248 KB |
Output is correct |
14 |
Correct |
101 ms |
32248 KB |
Output is correct |
15 |
Correct |
136 ms |
32248 KB |
Output is correct |
16 |
Correct |
143 ms |
11768 KB |
Output is correct |
17 |
Correct |
149 ms |
11744 KB |
Output is correct |
18 |
Correct |
79 ms |
16248 KB |
Output is correct |
19 |
Correct |
72 ms |
10872 KB |
Output is correct |
20 |
Correct |
57 ms |
22392 KB |
Output is correct |
21 |
Correct |
69 ms |
9848 KB |
Output is correct |
22 |
Correct |
214 ms |
13944 KB |
Output is correct |
23 |
Correct |
345 ms |
23800 KB |
Output is correct |
24 |
Correct |
616 ms |
26736 KB |
Output is correct |
25 |
Correct |
227 ms |
21364 KB |
Output is correct |
26 |
Correct |
258 ms |
30780 KB |
Output is correct |
27 |
Correct |
204 ms |
33184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8192 KB |
Output is correct |
2 |
Correct |
5 ms |
8192 KB |
Output is correct |
3 |
Correct |
8 ms |
8192 KB |
Output is correct |
4 |
Correct |
5 ms |
8192 KB |
Output is correct |
5 |
Correct |
5 ms |
8192 KB |
Output is correct |
6 |
Correct |
5 ms |
8192 KB |
Output is correct |
7 |
Correct |
5 ms |
8192 KB |
Output is correct |
8 |
Correct |
5 ms |
8192 KB |
Output is correct |
9 |
Correct |
5 ms |
8192 KB |
Output is correct |
10 |
Correct |
202 ms |
19060 KB |
Output is correct |
11 |
Correct |
104 ms |
21240 KB |
Output is correct |
12 |
Correct |
598 ms |
26352 KB |
Output is correct |
13 |
Correct |
246 ms |
20208 KB |
Output is correct |
14 |
Correct |
293 ms |
21368 KB |
Output is correct |
15 |
Correct |
6 ms |
8448 KB |
Output is correct |
16 |
Correct |
6 ms |
8448 KB |
Output is correct |
17 |
Correct |
5 ms |
8448 KB |
Output is correct |
18 |
Correct |
98 ms |
32248 KB |
Output is correct |
19 |
Correct |
101 ms |
32248 KB |
Output is correct |
20 |
Correct |
136 ms |
32248 KB |
Output is correct |
21 |
Correct |
119 ms |
11768 KB |
Output is correct |
22 |
Correct |
74 ms |
11776 KB |
Output is correct |
23 |
Correct |
63 ms |
16248 KB |
Output is correct |
24 |
Correct |
48 ms |
10872 KB |
Output is correct |
25 |
Correct |
60 ms |
22392 KB |
Output is correct |
26 |
Correct |
143 ms |
11768 KB |
Output is correct |
27 |
Correct |
149 ms |
11744 KB |
Output is correct |
28 |
Correct |
79 ms |
16248 KB |
Output is correct |
29 |
Correct |
72 ms |
10872 KB |
Output is correct |
30 |
Correct |
57 ms |
22392 KB |
Output is correct |
31 |
Correct |
14 ms |
9344 KB |
Output is correct |
32 |
Correct |
49 ms |
12280 KB |
Output is correct |
33 |
Correct |
58 ms |
12408 KB |
Output is correct |
34 |
Correct |
62 ms |
12536 KB |
Output is correct |
35 |
Correct |
22 ms |
11128 KB |
Output is correct |
36 |
Correct |
42 ms |
14292 KB |
Output is correct |
37 |
Correct |
39 ms |
17016 KB |
Output is correct |
38 |
Correct |
69 ms |
9848 KB |
Output is correct |
39 |
Correct |
214 ms |
13944 KB |
Output is correct |
40 |
Correct |
345 ms |
23800 KB |
Output is correct |
41 |
Correct |
616 ms |
26736 KB |
Output is correct |
42 |
Correct |
227 ms |
21364 KB |
Output is correct |
43 |
Correct |
258 ms |
30780 KB |
Output is correct |
44 |
Correct |
204 ms |
33184 KB |
Output is correct |
45 |
Correct |
65 ms |
10104 KB |
Output is correct |
46 |
Correct |
207 ms |
14072 KB |
Output is correct |
47 |
Correct |
322 ms |
22260 KB |
Output is correct |
48 |
Correct |
576 ms |
29296 KB |
Output is correct |
49 |
Correct |
262 ms |
22032 KB |
Output is correct |
50 |
Correct |
289 ms |
31408 KB |
Output is correct |
51 |
Correct |
204 ms |
34036 KB |
Output is correct |