#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+10;
// lazy1 -> set
// lazy2 -> sum
struct Node
{
ll mx, lazy1, lazy2;
Node *l, *r;
Node()
{
mx = lazy1 = lazy2 = 0;
l = r = NULL;
}
} *tree[maxn];
int n, m, k;
int d[maxn], w[maxn];
vector<int> grafo[maxn];
set<int> st[maxn];
void flush(Node *node, int l, int r)
{
if (!node->l) node->l = new Node();
if (!node->r) node->r = new Node();
if (!node->lazy1 && !node->lazy2) return;
if (l != r)
{
if (node->lazy1)
{
node->l->lazy1 = node->r->lazy1 = node->lazy1;
node->l->lazy2 = node->r->lazy2 = 0;
}
else
{
if (node->l->lazy1) node->l->lazy1 += node->lazy2;
else node->l->lazy2 += node->lazy2;
if (node->r->lazy1) node->r->lazy1 += node->lazy2;
else node->r->lazy2 += node->lazy2;
}
}
if (node->lazy1) node->mx = node->lazy1;
else node->mx += node->lazy2;
node->lazy1 = node->lazy2 = 0;
}
void upd(Node *node, int l, int r, int a, int b, ll v, int type)
{
flush(node, l, r);
if (l > b || r < a) return;
if (l >= a && r <= b)
{
if (type == 1)
{
node->lazy1 = v;
node->lazy2 = 0;
}
else
{
if (node->lazy1) node->lazy1 += v;
else node->lazy2 += v;
}
flush(node, l, r);
return;
}
int mid = (l+r)>>1;
upd(node->l, l, mid, a, b, v, type);
upd(node->r, mid+1, r, a, b, v, type);
node->mx = max(node->l->mx, node->r->mx);
}
ll query(Node *node, int l, int r, int a, int b)
{
flush(node, l, r);
if (l > b || r < a) return 0;
if (l >= a && r <= b) return node->mx;
int mid = (l+r)>>1;
return max(query(node->l, l, mid, a, b), query(node->r, mid+1, r, a, b));
}
int busca(Node *node, int l, int r, ll v)
{
flush(node, l, r);
if (l == r)
{
if (node->mx > v) return l;
return k+1;
}
int mid = (l+r)>>1;
flush(node->l, l, mid); flush(node->r, mid+1, r);
if (node->l->mx > v) return busca(node->l, l, mid, v);
return busca(node->r, mid+1, r, v);
}
void merge(int u, int v)
{
bool rev = 0;
if (st[u].size() < st[v].size()) swap(u, v), rev = 1;
for (auto it = st[v].begin(); it != st[v].end(); ++it)
{
int x = *it, lim = k;
if (next(it) != st[v].end()) lim = *next(it)-1;
if (lim >= x) upd(tree[u], 1, k, x, lim, query(tree[v], 1, k, x, x), 2);
st[u].insert(x);
}
if (rev)
{
swap(st[u], st[v]);
swap(tree[u], tree[v]);
}
}
void solve(int u, int p)
{
tree[u] = new Node();
for (auto v: grafo[u])
{
if (v == p) continue;
solve(v, u);
merge(u, v);
}
if (d[u])
{
ll x = 1ll*w[u] + 1ll*query(tree[u], 1, k, d[u], d[u]);
int pos = busca(tree[u], 1, k, x);
if (pos > d[u])
{
upd(tree[u], 1, k, d[u], pos-1, x, 1);
st[u].insert(d[u]);
}
}
}
int main(void)
{
scanf("%d %d %d", &n, &m, &k);
for (int i = 2; i <= n; i++)
{
int p;
scanf("%d", &p);
grafo[i].push_back(p);
grafo[p].push_back(i);
}
for (int i = 1; i <= m; i++)
{
int v;
scanf("%d", &v);
scanf("%d %d", &d[v], &w[v]);
}
solve(1, 0);
printf("%lld\n", query(tree[1], 1, k, 1, k));
}
Compilation message
magictree.cpp: In function 'int main()':
magictree.cpp:172:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &n, &m, &k);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:177:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &p);
~~~~~^~~~~~~~~~
magictree.cpp:186:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &v);
~~~~~^~~~~~~~~~
magictree.cpp:187:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &d[v], &w[v]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14464 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14592 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
529 ms |
317560 KB |
Output is correct |
2 |
Correct |
257 ms |
161144 KB |
Output is correct |
3 |
Correct |
1683 ms |
961020 KB |
Output is correct |
4 |
Correct |
878 ms |
575728 KB |
Output is correct |
5 |
Correct |
921 ms |
576760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14720 KB |
Output is correct |
2 |
Correct |
14 ms |
14848 KB |
Output is correct |
3 |
Correct |
14 ms |
14720 KB |
Output is correct |
4 |
Correct |
202 ms |
52600 KB |
Output is correct |
5 |
Correct |
199 ms |
52856 KB |
Output is correct |
6 |
Correct |
325 ms |
52584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
42104 KB |
Output is correct |
2 |
Correct |
136 ms |
32632 KB |
Output is correct |
3 |
Correct |
111 ms |
34424 KB |
Output is correct |
4 |
Correct |
117 ms |
55660 KB |
Output is correct |
5 |
Correct |
91 ms |
32248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14464 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14592 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
283 ms |
112380 KB |
Output is correct |
11 |
Correct |
237 ms |
84856 KB |
Output is correct |
12 |
Correct |
144 ms |
52472 KB |
Output is correct |
13 |
Correct |
232 ms |
148848 KB |
Output is correct |
14 |
Correct |
106 ms |
31608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
24568 KB |
Output is correct |
2 |
Correct |
78 ms |
32504 KB |
Output is correct |
3 |
Correct |
81 ms |
32888 KB |
Output is correct |
4 |
Correct |
83 ms |
35320 KB |
Output is correct |
5 |
Correct |
44 ms |
31472 KB |
Output is correct |
6 |
Correct |
71 ms |
31480 KB |
Output is correct |
7 |
Correct |
61 ms |
30204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14464 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14592 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14720 KB |
Output is correct |
11 |
Correct |
14 ms |
14848 KB |
Output is correct |
12 |
Correct |
14 ms |
14720 KB |
Output is correct |
13 |
Correct |
202 ms |
52600 KB |
Output is correct |
14 |
Correct |
199 ms |
52856 KB |
Output is correct |
15 |
Correct |
325 ms |
52584 KB |
Output is correct |
16 |
Correct |
283 ms |
112380 KB |
Output is correct |
17 |
Correct |
237 ms |
84856 KB |
Output is correct |
18 |
Correct |
144 ms |
52472 KB |
Output is correct |
19 |
Correct |
232 ms |
148848 KB |
Output is correct |
20 |
Correct |
106 ms |
31608 KB |
Output is correct |
21 |
Correct |
164 ms |
92796 KB |
Output is correct |
22 |
Correct |
467 ms |
247032 KB |
Output is correct |
23 |
Correct |
828 ms |
475256 KB |
Output is correct |
24 |
Correct |
1435 ms |
817272 KB |
Output is correct |
25 |
Correct |
857 ms |
575212 KB |
Output is correct |
26 |
Correct |
743 ms |
380448 KB |
Output is correct |
27 |
Correct |
500 ms |
169880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14464 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14592 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
529 ms |
317560 KB |
Output is correct |
11 |
Correct |
257 ms |
161144 KB |
Output is correct |
12 |
Correct |
1683 ms |
961020 KB |
Output is correct |
13 |
Correct |
878 ms |
575728 KB |
Output is correct |
14 |
Correct |
921 ms |
576760 KB |
Output is correct |
15 |
Correct |
13 ms |
14720 KB |
Output is correct |
16 |
Correct |
14 ms |
14848 KB |
Output is correct |
17 |
Correct |
14 ms |
14720 KB |
Output is correct |
18 |
Correct |
202 ms |
52600 KB |
Output is correct |
19 |
Correct |
199 ms |
52856 KB |
Output is correct |
20 |
Correct |
325 ms |
52584 KB |
Output is correct |
21 |
Correct |
153 ms |
42104 KB |
Output is correct |
22 |
Correct |
136 ms |
32632 KB |
Output is correct |
23 |
Correct |
111 ms |
34424 KB |
Output is correct |
24 |
Correct |
117 ms |
55660 KB |
Output is correct |
25 |
Correct |
91 ms |
32248 KB |
Output is correct |
26 |
Correct |
283 ms |
112380 KB |
Output is correct |
27 |
Correct |
237 ms |
84856 KB |
Output is correct |
28 |
Correct |
144 ms |
52472 KB |
Output is correct |
29 |
Correct |
232 ms |
148848 KB |
Output is correct |
30 |
Correct |
106 ms |
31608 KB |
Output is correct |
31 |
Correct |
34 ms |
24568 KB |
Output is correct |
32 |
Correct |
78 ms |
32504 KB |
Output is correct |
33 |
Correct |
81 ms |
32888 KB |
Output is correct |
34 |
Correct |
83 ms |
35320 KB |
Output is correct |
35 |
Correct |
44 ms |
31472 KB |
Output is correct |
36 |
Correct |
71 ms |
31480 KB |
Output is correct |
37 |
Correct |
61 ms |
30204 KB |
Output is correct |
38 |
Correct |
164 ms |
92796 KB |
Output is correct |
39 |
Correct |
467 ms |
247032 KB |
Output is correct |
40 |
Correct |
828 ms |
475256 KB |
Output is correct |
41 |
Correct |
1435 ms |
817272 KB |
Output is correct |
42 |
Correct |
857 ms |
575212 KB |
Output is correct |
43 |
Correct |
743 ms |
380448 KB |
Output is correct |
44 |
Correct |
500 ms |
169880 KB |
Output is correct |
45 |
Correct |
161 ms |
92792 KB |
Output is correct |
46 |
Correct |
487 ms |
247832 KB |
Output is correct |
47 |
Correct |
837 ms |
474744 KB |
Output is correct |
48 |
Correct |
1442 ms |
818808 KB |
Output is correct |
49 |
Correct |
873 ms |
575856 KB |
Output is correct |
50 |
Correct |
731 ms |
381760 KB |
Output is correct |
51 |
Correct |
506 ms |
170488 KB |
Output is correct |