# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1118396 |
2024-11-25T12:26:55 Z |
rama_pang |
Tree (IOI24_tree) |
C++17 |
|
148 ms |
22948 KB |
#if not LOCAL
#define NDEBUG 1
#endif
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(auto i = a; i < (b); ++i)
#define down(x, a) for (auto x = a; x--;)
#define all(x) begin(x), end(x)
#define sz(x) int(size(x))
#define let auto const
using ll = long long;
using lint = ll;
using pii = pair<int, int>;
using vi = vector<int>;
vector<lint> answer;
struct DisjointSet {
DisjointSet(int n) : dsu(n, -1), cnt(n) {}
vector<int> dsu;
vector<int> cnt;
bool merge(int x, int y) {
if (find(x) == find(y)) {
return false;
}
x = find(x), y = find(y);
dsu[x] = y;
cnt[y] += cnt[x];
return true;
}
int updateCount(int x, int dif) {
return cnt[find(x)] += dif;
}
int getCount(int x) {
return cnt[find(x)];
}
int find(int x) {
return dsu[x] < 0 ? x : dsu[x] = find(dsu[x]);
}
};
void init(vector<int> P, vector<int> W) {
int N = sz(P) + 1;
P.insert(begin(P), -1);
W.insert(begin(W), 0);
rep(i, 1, N) P[i]++;
vector<int> cntLeaf(N, 0);
vector<vector<int>> adj(N);
rep(i,1,N) adj[P[i]].push_back(i);
lint baseAns = 0;
down(i, N) {
if (sz(adj[i]) == 0) {
cntLeaf[i] = 1;
baseAns += W[i];
} else {
cntLeaf[i] = 0;
for (auto j : adj[i]) {
cntLeaf[i] += cntLeaf[j];
}
}
}
vector<int> ord(N);
iota(all(ord), 0);
sort(all(ord), [&](int i, int j) {
return pair(W[i], i) > pair(W[j], j);
});
assert(end(ord)[-1] == 0);
DisjointSet dsu(N);
vector<int> done(N);
answer.resize(cntLeaf[0] + 1);
auto UpdateAns = [&](lint l, lint r, lint d) {
if (l < sz(answer)) answer[l] += d;
if (r < sz(answer)) answer[r] -= d;
};
for (auto u : ord) {
if (sz(adj[u]) == 0 || u == 0) {
continue;
}
done[u] = 1;
vector<int> ls;
for (auto v : adj[u]) {
if (done[v]) {
ls.push_back(dsu.getCount(v));
dsu.merge(v, u);
}
};
if (done[P[u]]) {
ls.push_back(dsu.getCount(P[u]));
dsu.merge(u, P[u]);
}
sort(all(ls));
lint ptr = 1;
lint total = 0;
rep(i, 0, sz(ls)) {
if (ptr <= ls[i]) {
UpdateAns(ptr, ls[i] + 1, -1ll * W[u] * (sz(ls) - i - 1));
total += 1ll * (ls[i] + 1 - ptr) * (sz(ls) - i - 1);
ptr = ls[i] + 1;
}
}
total += sz(adj[u]) - 1;
UpdateAns(ptr, ptr + total, W[u]);
dsu.updateCount(u, sz(adj[u]) - 1);
}
rep(i, 1, sz(answer)) {
answer[i] += answer[i - 1];
}
down(i, sz(answer)) {
if (i + 1 < sz(answer)) {
answer[i] += answer[i + 1];
} else {
answer[i] += baseAns;
}
}
}
long long query(int L, int R) {
if (L > 1) {
lint upper = query(1, (R + L - 1) / L);
lint lower = query(1, R / L);
return L * upper + ((L - R % L) % L) * (lower - upper);
}
assert(L == 1 && R >= 1);
return answer[min(R, sz(answer) - 1)];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
19480 KB |
Output is correct |
2 |
Correct |
51 ms |
18484 KB |
Output is correct |
3 |
Correct |
73 ms |
17972 KB |
Output is correct |
4 |
Correct |
104 ms |
19256 KB |
Output is correct |
5 |
Correct |
95 ms |
19256 KB |
Output is correct |
6 |
Correct |
56 ms |
18284 KB |
Output is correct |
7 |
Correct |
108 ms |
19220 KB |
Output is correct |
8 |
Correct |
93 ms |
19224 KB |
Output is correct |
9 |
Correct |
99 ms |
18604 KB |
Output is correct |
10 |
Correct |
57 ms |
16168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
632 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
632 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
23 ms |
6020 KB |
Output is correct |
10 |
Correct |
26 ms |
6008 KB |
Output is correct |
11 |
Correct |
28 ms |
6020 KB |
Output is correct |
12 |
Correct |
31 ms |
6028 KB |
Output is correct |
13 |
Correct |
25 ms |
5764 KB |
Output is correct |
14 |
Correct |
21 ms |
5764 KB |
Output is correct |
15 |
Correct |
25 ms |
5892 KB |
Output is correct |
16 |
Correct |
32 ms |
6020 KB |
Output is correct |
17 |
Correct |
26 ms |
6020 KB |
Output is correct |
18 |
Correct |
29 ms |
6012 KB |
Output is correct |
19 |
Correct |
32 ms |
6020 KB |
Output is correct |
20 |
Correct |
27 ms |
5752 KB |
Output is correct |
21 |
Correct |
23 ms |
5716 KB |
Output is correct |
22 |
Correct |
19 ms |
4996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
20532 KB |
Output is correct |
2 |
Correct |
81 ms |
20420 KB |
Output is correct |
3 |
Correct |
82 ms |
20236 KB |
Output is correct |
4 |
Correct |
86 ms |
20264 KB |
Output is correct |
5 |
Correct |
95 ms |
20520 KB |
Output is correct |
6 |
Correct |
85 ms |
22156 KB |
Output is correct |
7 |
Correct |
94 ms |
19592 KB |
Output is correct |
8 |
Correct |
63 ms |
17308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
20532 KB |
Output is correct |
2 |
Correct |
81 ms |
20420 KB |
Output is correct |
3 |
Correct |
82 ms |
20236 KB |
Output is correct |
4 |
Correct |
86 ms |
20264 KB |
Output is correct |
5 |
Correct |
95 ms |
20520 KB |
Output is correct |
6 |
Correct |
85 ms |
22156 KB |
Output is correct |
7 |
Correct |
94 ms |
19592 KB |
Output is correct |
8 |
Correct |
63 ms |
17308 KB |
Output is correct |
9 |
Correct |
105 ms |
19108 KB |
Output is correct |
10 |
Correct |
115 ms |
20388 KB |
Output is correct |
11 |
Correct |
110 ms |
20388 KB |
Output is correct |
12 |
Correct |
95 ms |
20388 KB |
Output is correct |
13 |
Correct |
102 ms |
22052 KB |
Output is correct |
14 |
Correct |
104 ms |
19608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
19496 KB |
Output is correct |
2 |
Correct |
99 ms |
19496 KB |
Output is correct |
3 |
Correct |
104 ms |
20776 KB |
Output is correct |
4 |
Correct |
129 ms |
20720 KB |
Output is correct |
5 |
Correct |
129 ms |
20776 KB |
Output is correct |
6 |
Correct |
116 ms |
20780 KB |
Output is correct |
7 |
Correct |
107 ms |
20772 KB |
Output is correct |
8 |
Correct |
125 ms |
22580 KB |
Output is correct |
9 |
Correct |
98 ms |
20268 KB |
Output is correct |
10 |
Correct |
100 ms |
20004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
78 ms |
19480 KB |
Output is correct |
3 |
Correct |
51 ms |
18484 KB |
Output is correct |
4 |
Correct |
73 ms |
17972 KB |
Output is correct |
5 |
Correct |
104 ms |
19256 KB |
Output is correct |
6 |
Correct |
95 ms |
19256 KB |
Output is correct |
7 |
Correct |
56 ms |
18284 KB |
Output is correct |
8 |
Correct |
108 ms |
19220 KB |
Output is correct |
9 |
Correct |
93 ms |
19224 KB |
Output is correct |
10 |
Correct |
99 ms |
18604 KB |
Output is correct |
11 |
Correct |
57 ms |
16168 KB |
Output is correct |
12 |
Correct |
1 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
2 ms |
484 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
2 ms |
632 KB |
Output is correct |
18 |
Correct |
2 ms |
592 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
23 ms |
6020 KB |
Output is correct |
21 |
Correct |
26 ms |
6008 KB |
Output is correct |
22 |
Correct |
28 ms |
6020 KB |
Output is correct |
23 |
Correct |
31 ms |
6028 KB |
Output is correct |
24 |
Correct |
25 ms |
5764 KB |
Output is correct |
25 |
Correct |
21 ms |
5764 KB |
Output is correct |
26 |
Correct |
25 ms |
5892 KB |
Output is correct |
27 |
Correct |
32 ms |
6020 KB |
Output is correct |
28 |
Correct |
26 ms |
6020 KB |
Output is correct |
29 |
Correct |
29 ms |
6012 KB |
Output is correct |
30 |
Correct |
32 ms |
6020 KB |
Output is correct |
31 |
Correct |
27 ms |
5752 KB |
Output is correct |
32 |
Correct |
23 ms |
5716 KB |
Output is correct |
33 |
Correct |
19 ms |
4996 KB |
Output is correct |
34 |
Correct |
71 ms |
20532 KB |
Output is correct |
35 |
Correct |
81 ms |
20420 KB |
Output is correct |
36 |
Correct |
82 ms |
20236 KB |
Output is correct |
37 |
Correct |
86 ms |
20264 KB |
Output is correct |
38 |
Correct |
95 ms |
20520 KB |
Output is correct |
39 |
Correct |
85 ms |
22156 KB |
Output is correct |
40 |
Correct |
94 ms |
19592 KB |
Output is correct |
41 |
Correct |
63 ms |
17308 KB |
Output is correct |
42 |
Correct |
105 ms |
19108 KB |
Output is correct |
43 |
Correct |
115 ms |
20388 KB |
Output is correct |
44 |
Correct |
110 ms |
20388 KB |
Output is correct |
45 |
Correct |
95 ms |
20388 KB |
Output is correct |
46 |
Correct |
102 ms |
22052 KB |
Output is correct |
47 |
Correct |
104 ms |
19608 KB |
Output is correct |
48 |
Correct |
66 ms |
19496 KB |
Output is correct |
49 |
Correct |
99 ms |
19496 KB |
Output is correct |
50 |
Correct |
104 ms |
20776 KB |
Output is correct |
51 |
Correct |
129 ms |
20720 KB |
Output is correct |
52 |
Correct |
129 ms |
20776 KB |
Output is correct |
53 |
Correct |
116 ms |
20780 KB |
Output is correct |
54 |
Correct |
107 ms |
20772 KB |
Output is correct |
55 |
Correct |
125 ms |
22580 KB |
Output is correct |
56 |
Correct |
98 ms |
20268 KB |
Output is correct |
57 |
Correct |
100 ms |
20004 KB |
Output is correct |
58 |
Correct |
89 ms |
21140 KB |
Output is correct |
59 |
Correct |
126 ms |
21288 KB |
Output is correct |
60 |
Correct |
96 ms |
19612 KB |
Output is correct |
61 |
Correct |
100 ms |
20008 KB |
Output is correct |
62 |
Correct |
114 ms |
21444 KB |
Output is correct |
63 |
Correct |
104 ms |
21288 KB |
Output is correct |
64 |
Correct |
122 ms |
21304 KB |
Output is correct |
65 |
Correct |
121 ms |
21284 KB |
Output is correct |
66 |
Correct |
123 ms |
21304 KB |
Output is correct |
67 |
Correct |
113 ms |
21288 KB |
Output is correct |
68 |
Correct |
148 ms |
22948 KB |
Output is correct |
69 |
Correct |
122 ms |
20776 KB |
Output is correct |
70 |
Correct |
102 ms |
19876 KB |
Output is correct |
71 |
Correct |
83 ms |
18228 KB |
Output is correct |