#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cstdio>
#include <algorithm>
#include <functional>
using namespace std;
#define int long long
const int maxn = 300'000;
struct func {
int k;
int b;
func(int k, int b): k(k), b(b) {}
func(){}
int get(int x) {
return k * x + b;
}
};
struct node {
int x, y, add, sz;
node* l = nullptr;
node* r = nullptr;
node(int x): x(x) {
y = rand();
add = 0;
sz = 1;
}
node() {}
};
void push(node* v) {
if (!v) return;
if (v->l) {
v->l->add += v->add;
}
if (v->r) {
v->r->add += v->add;
}
v->x += v->add;
v->add = 0;
}
int s(node* v) {
if (!v) return 0;
return v->sz;
}
void upd(node* v) {
if (!v) return;
v->sz = 1 + s(v->l) + s(v->r);
}
pair<node*, func> root[maxn];
node* merge(node* l, node* r) {
if (!l) return r;
if (!r) return l;
if (l->y > r->y) {
push(l);
l->r = merge(l->r, r);
upd(l);
return l;
}
push(r);
r->l = merge(l, r->l);
upd(r);
return r;
}
pair<node*, node*> split(node* v, int k) {
if (!v) return { nullptr, nullptr };
push(v);
if (s(v->l) + 1 <= k) {
pair<node*, node*> q = split(v->r, k - s(v->l) - 1);
v->r = q.first;
upd(v);
return { v, q.second };
}
pair<node*, node*> q = split(v->l, k);
v->l = q.second;
upd(v);
return { q.first, v };
}
pair<node*, node*> split1(node* v, int k) {
if (!v) return { nullptr, nullptr };
push(v);
if (v->x <= k) {
pair<node*, node*> q = split1(v->r, k);
v->r = q.first;
upd(v);
return { v, q.second };
}
pair<node*, node*> q = split1(v->l, k);
v->l = q.second;
upd(v);
return { q.first, v };
}
void add(node*& root, int x) {
pair<node*, node*> q = split1(root, x);
node* t = new node(x);
root = merge(q.first, merge(t, q.second));
}
int back(node* v) {
push(v);
if (v->r) {
return back(v->r);
}
return v->x;
}
void walk(node* v, vector<int> &x) {
if (!v) return;
push(v);
walk(v->l, x);
x.push_back(v->x);
walk(v->r, x);
}
void dfs(int v, vector<vector<pair<int, int>>> &g, int l) {
sort(g[v].begin(), g[v].end(), [&] (pair<int, int> u, pair<int, int> v) {
return s(root[u.first].first) > s(root[v.first].first);
});
root[v] = { nullptr, func(0, 0) };
if (g[v].size() == 0) {
root[v] = { nullptr, func(1, -l) };
add(root[v].first, l);
add(root[v].first, l);
} else {
for (auto [u, c] : g[v]) {
dfs(u, g, c);
}
root[v] = root[g[v][0].first];
for (int i = 1; i < g[v].size(); ++i) {
root[v].second.k += root[g[v][i].first].second.k;
root[v].second.b += root[g[v][i].first].second.b;
vector<int> x;
walk(root[g[v][i].first].first, x);
for (int c : x) {
add(root[v].first, c);
}
x.clear();
}
if (1) {
while (root[v].second.k > 1) {
int x = back(root[v].first);
int val = root[v].second.k * x + root[v].second.b;
--root[v].second.k;
root[v].second.b = val - root[v].second.k * x;
pair<node*, node*> q = split(root[v].first, s(root[v].first) - 1);
root[v].first = q.first;
}
}
if (1) {
pair<node*, node*> q = split(root[v].first, s(root[v].first) - 2);
if (q.second) q.second->add += l;
root[v].second.b -= l;
root[v].first = merge(q.first, q.second);
}
}
return;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<vector<pair<int, int>>> g(n + m);
for (int i = 1; i < n + m; ++i) {
int p, c;
cin >> p >> c;
--p;
g[p].push_back({ i, c });
}
dfs(0, g, 0);
int curr = root[0].second.k;
vector<int> x;
walk(root[0].first, x);
auto pt = x.rbegin();
while (curr--) {
int x = *pt;
int val = root[0].second.k * x + root[0].second.b;
--root[0].second.k;
root[0].second.b = val - root[0].second.k * x;
++pt;
}
cout << root[0].second.b;
}
/*
4 6
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
*/
Compilation message
fireworks.cpp: In function 'void dfs(long long int, std::vector<std::vector<std::pair<long long int, long long int> > >&, long long int)':
fireworks.cpp:144:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
144 | for (int i = 1; i < g[v].size(); ++i) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7252 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7372 KB |
Output is correct |
6 |
Correct |
3 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
3 ms |
7372 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7368 KB |
Output is correct |
2 |
Correct |
3 ms |
7368 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7328 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
3 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7368 KB |
Output is correct |
8 |
Correct |
3 ms |
7360 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7368 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7380 KB |
Output is correct |
14 |
Correct |
3 ms |
7364 KB |
Output is correct |
15 |
Correct |
3 ms |
7368 KB |
Output is correct |
16 |
Correct |
3 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7364 KB |
Output is correct |
19 |
Correct |
3 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7252 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7372 KB |
Output is correct |
6 |
Correct |
3 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
3 ms |
7372 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7380 KB |
Output is correct |
11 |
Correct |
3 ms |
7368 KB |
Output is correct |
12 |
Correct |
3 ms |
7368 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7328 KB |
Output is correct |
15 |
Correct |
3 ms |
7380 KB |
Output is correct |
16 |
Correct |
3 ms |
7380 KB |
Output is correct |
17 |
Correct |
3 ms |
7368 KB |
Output is correct |
18 |
Correct |
3 ms |
7360 KB |
Output is correct |
19 |
Correct |
3 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7368 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
3 ms |
7380 KB |
Output is correct |
24 |
Correct |
3 ms |
7364 KB |
Output is correct |
25 |
Correct |
3 ms |
7368 KB |
Output is correct |
26 |
Correct |
3 ms |
7380 KB |
Output is correct |
27 |
Correct |
4 ms |
7380 KB |
Output is correct |
28 |
Correct |
4 ms |
7364 KB |
Output is correct |
29 |
Correct |
3 ms |
7380 KB |
Output is correct |
30 |
Correct |
4 ms |
7380 KB |
Output is correct |
31 |
Correct |
4 ms |
7508 KB |
Output is correct |
32 |
Correct |
5 ms |
7704 KB |
Output is correct |
33 |
Correct |
5 ms |
7764 KB |
Output is correct |
34 |
Correct |
8 ms |
7884 KB |
Output is correct |
35 |
Correct |
8 ms |
8008 KB |
Output is correct |
36 |
Correct |
7 ms |
8148 KB |
Output is correct |
37 |
Correct |
8 ms |
8276 KB |
Output is correct |
38 |
Correct |
8 ms |
8404 KB |
Output is correct |
39 |
Correct |
9 ms |
8396 KB |
Output is correct |
40 |
Correct |
6 ms |
8404 KB |
Output is correct |
41 |
Correct |
5 ms |
8404 KB |
Output is correct |
42 |
Correct |
5 ms |
7636 KB |
Output is correct |
43 |
Correct |
8 ms |
8660 KB |
Output is correct |
44 |
Correct |
9 ms |
8628 KB |
Output is correct |
45 |
Correct |
8 ms |
8532 KB |
Output is correct |
46 |
Correct |
12 ms |
9300 KB |
Output is correct |
47 |
Correct |
13 ms |
9428 KB |
Output is correct |
48 |
Correct |
11 ms |
8964 KB |
Output is correct |
49 |
Correct |
12 ms |
9172 KB |
Output is correct |
50 |
Correct |
11 ms |
8820 KB |
Output is correct |
51 |
Correct |
11 ms |
8784 KB |
Output is correct |
52 |
Correct |
13 ms |
8960 KB |
Output is correct |
53 |
Correct |
13 ms |
9300 KB |
Output is correct |
54 |
Correct |
11 ms |
8916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7252 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7372 KB |
Output is correct |
6 |
Correct |
3 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
3 ms |
7372 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7380 KB |
Output is correct |
11 |
Correct |
3 ms |
7368 KB |
Output is correct |
12 |
Correct |
3 ms |
7368 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7328 KB |
Output is correct |
15 |
Correct |
3 ms |
7380 KB |
Output is correct |
16 |
Correct |
3 ms |
7380 KB |
Output is correct |
17 |
Correct |
3 ms |
7368 KB |
Output is correct |
18 |
Correct |
3 ms |
7360 KB |
Output is correct |
19 |
Correct |
3 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7368 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
3 ms |
7380 KB |
Output is correct |
24 |
Correct |
3 ms |
7364 KB |
Output is correct |
25 |
Correct |
3 ms |
7368 KB |
Output is correct |
26 |
Correct |
3 ms |
7380 KB |
Output is correct |
27 |
Correct |
4 ms |
7380 KB |
Output is correct |
28 |
Correct |
4 ms |
7364 KB |
Output is correct |
29 |
Correct |
3 ms |
7380 KB |
Output is correct |
30 |
Correct |
4 ms |
7380 KB |
Output is correct |
31 |
Correct |
4 ms |
7508 KB |
Output is correct |
32 |
Correct |
5 ms |
7704 KB |
Output is correct |
33 |
Correct |
5 ms |
7764 KB |
Output is correct |
34 |
Correct |
8 ms |
7884 KB |
Output is correct |
35 |
Correct |
8 ms |
8008 KB |
Output is correct |
36 |
Correct |
7 ms |
8148 KB |
Output is correct |
37 |
Correct |
8 ms |
8276 KB |
Output is correct |
38 |
Correct |
8 ms |
8404 KB |
Output is correct |
39 |
Correct |
9 ms |
8396 KB |
Output is correct |
40 |
Correct |
6 ms |
8404 KB |
Output is correct |
41 |
Correct |
5 ms |
8404 KB |
Output is correct |
42 |
Correct |
5 ms |
7636 KB |
Output is correct |
43 |
Correct |
8 ms |
8660 KB |
Output is correct |
44 |
Correct |
9 ms |
8628 KB |
Output is correct |
45 |
Correct |
8 ms |
8532 KB |
Output is correct |
46 |
Correct |
12 ms |
9300 KB |
Output is correct |
47 |
Correct |
13 ms |
9428 KB |
Output is correct |
48 |
Correct |
11 ms |
8964 KB |
Output is correct |
49 |
Correct |
12 ms |
9172 KB |
Output is correct |
50 |
Correct |
11 ms |
8820 KB |
Output is correct |
51 |
Correct |
11 ms |
8784 KB |
Output is correct |
52 |
Correct |
13 ms |
8960 KB |
Output is correct |
53 |
Correct |
13 ms |
9300 KB |
Output is correct |
54 |
Correct |
11 ms |
8916 KB |
Output is correct |
55 |
Correct |
15 ms |
10140 KB |
Output is correct |
56 |
Correct |
57 ms |
18388 KB |
Output is correct |
57 |
Correct |
129 ms |
26700 KB |
Output is correct |
58 |
Correct |
153 ms |
32388 KB |
Output is correct |
59 |
Correct |
191 ms |
40732 KB |
Output is correct |
60 |
Correct |
246 ms |
49324 KB |
Output is correct |
61 |
Correct |
278 ms |
55488 KB |
Output is correct |
62 |
Correct |
304 ms |
60616 KB |
Output is correct |
63 |
Correct |
390 ms |
73232 KB |
Output is correct |
64 |
Correct |
395 ms |
73268 KB |
Output is correct |
65 |
Correct |
110 ms |
70900 KB |
Output is correct |
66 |
Correct |
105 ms |
70828 KB |
Output is correct |
67 |
Correct |
107 ms |
28976 KB |
Output is correct |
68 |
Correct |
407 ms |
87004 KB |
Output is correct |
69 |
Correct |
452 ms |
84848 KB |
Output is correct |
70 |
Correct |
442 ms |
84944 KB |
Output is correct |
71 |
Correct |
741 ms |
140752 KB |
Output is correct |
72 |
Correct |
748 ms |
140828 KB |
Output is correct |
73 |
Correct |
695 ms |
122332 KB |
Output is correct |
74 |
Correct |
834 ms |
139700 KB |
Output is correct |
75 |
Correct |
710 ms |
120900 KB |
Output is correct |
76 |
Correct |
727 ms |
121068 KB |
Output is correct |
77 |
Correct |
758 ms |
118132 KB |
Output is correct |
78 |
Correct |
857 ms |
126784 KB |
Output is correct |
79 |
Correct |
986 ms |
104204 KB |
Output is correct |