// model_solution/endagorion-full.cpp
#include "tree.h"
#include <bits/stdc++.h>
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
const int maxn = 310000;
i64 ans[maxn];
int n;
int p[maxn];
i64 w[maxn];
vi e[maxn];
int par[maxn], nleaves[maxn], tcr[maxn];
i64 sl[maxn];
int root(int v) {
return v == par[v] ? v : par[v] = root(par[v]);
}
void init(vi P, vi W) {
n = sz(P);
copy(all(P), p);
copy(all(W), w);
forn(i, n) e[i].clear();
for1(i, n - 1) e[P[i]].pb(i);
vi ord(n);
iota(all(ord), 0);
sort(rall(ord), [&](int i, int j){return w[i] < w[j];});
iota(par, par + n, 0);
fill(nleaves, nleaves + n, 1);
fill(tcr, tcr + n, n + 1);
fill(sl, sl + n + 1, 0);
i64 v0 = 0;
for (int v: ord) {
if (e[v].empty()) {
v0 += w[v];
continue;
}
int r = root(v);
sl[nleaves[r]] += tcr[r] - w[v];
--nleaves[r];
tcr[r] = w[v];
for (int u: e[v]) {
sl[nleaves[u]] += tcr[u] - w[v];
par[u] = r;
nleaves[r] += nleaves[u];
}
}
sl[nleaves[0]] += tcr[0];
// for1(i, n) if (sl[i]) cerr << i << ' ' << sl[i] << '\n';
i64 slope = 0, v = v0;
ford(i, n + 1) {
v += slope;
ans[i] = v;
slope += sl[i];
}
}
i64 query(int L, int R) {
if (R / L >= n) return ans[n] * L;
i64 qans = (L - R % L) * ans[R / L];
if (R % L) qans += (R % L) * ans[R / L + 1];
return qans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
23504 KB |
Output is correct |
2 |
Correct |
38 ms |
23404 KB |
Output is correct |
3 |
Correct |
58 ms |
21332 KB |
Output is correct |
4 |
Correct |
58 ms |
23380 KB |
Output is correct |
5 |
Correct |
57 ms |
23384 KB |
Output is correct |
6 |
Correct |
54 ms |
23376 KB |
Output is correct |
7 |
Correct |
59 ms |
23384 KB |
Output is correct |
8 |
Correct |
74 ms |
23124 KB |
Output is correct |
9 |
Correct |
64 ms |
22592 KB |
Output is correct |
10 |
Correct |
58 ms |
20164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7772 KB |
Output is correct |
2 |
Correct |
4 ms |
7772 KB |
Output is correct |
3 |
Correct |
3 ms |
7772 KB |
Output is correct |
4 |
Correct |
3 ms |
7772 KB |
Output is correct |
5 |
Correct |
3 ms |
7772 KB |
Output is correct |
6 |
Correct |
3 ms |
7772 KB |
Output is correct |
7 |
Correct |
3 ms |
7772 KB |
Output is correct |
8 |
Correct |
3 ms |
7772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7772 KB |
Output is correct |
2 |
Correct |
4 ms |
7772 KB |
Output is correct |
3 |
Correct |
3 ms |
7772 KB |
Output is correct |
4 |
Correct |
3 ms |
7772 KB |
Output is correct |
5 |
Correct |
3 ms |
7772 KB |
Output is correct |
6 |
Correct |
3 ms |
7772 KB |
Output is correct |
7 |
Correct |
3 ms |
7772 KB |
Output is correct |
8 |
Correct |
3 ms |
7772 KB |
Output is correct |
9 |
Correct |
17 ms |
12380 KB |
Output is correct |
10 |
Correct |
16 ms |
12376 KB |
Output is correct |
11 |
Correct |
17 ms |
12376 KB |
Output is correct |
12 |
Correct |
20 ms |
12332 KB |
Output is correct |
13 |
Correct |
19 ms |
12120 KB |
Output is correct |
14 |
Correct |
18 ms |
12128 KB |
Output is correct |
15 |
Correct |
19 ms |
12380 KB |
Output is correct |
16 |
Correct |
19 ms |
12384 KB |
Output is correct |
17 |
Correct |
18 ms |
12376 KB |
Output is correct |
18 |
Correct |
20 ms |
12380 KB |
Output is correct |
19 |
Correct |
23 ms |
12332 KB |
Output is correct |
20 |
Correct |
20 ms |
12120 KB |
Output is correct |
21 |
Correct |
18 ms |
12112 KB |
Output is correct |
22 |
Correct |
16 ms |
11480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
24272 KB |
Output is correct |
2 |
Correct |
60 ms |
24144 KB |
Output is correct |
3 |
Correct |
59 ms |
24132 KB |
Output is correct |
4 |
Correct |
60 ms |
24072 KB |
Output is correct |
5 |
Correct |
61 ms |
23884 KB |
Output is correct |
6 |
Correct |
84 ms |
26432 KB |
Output is correct |
7 |
Correct |
61 ms |
23368 KB |
Output is correct |
8 |
Correct |
76 ms |
21172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
24272 KB |
Output is correct |
2 |
Correct |
60 ms |
24144 KB |
Output is correct |
3 |
Correct |
59 ms |
24132 KB |
Output is correct |
4 |
Correct |
60 ms |
24072 KB |
Output is correct |
5 |
Correct |
61 ms |
23884 KB |
Output is correct |
6 |
Correct |
84 ms |
26432 KB |
Output is correct |
7 |
Correct |
61 ms |
23368 KB |
Output is correct |
8 |
Correct |
76 ms |
21172 KB |
Output is correct |
9 |
Correct |
66 ms |
22356 KB |
Output is correct |
10 |
Correct |
70 ms |
24144 KB |
Output is correct |
11 |
Correct |
68 ms |
24132 KB |
Output is correct |
12 |
Correct |
69 ms |
24136 KB |
Output is correct |
13 |
Correct |
69 ms |
26492 KB |
Output is correct |
14 |
Correct |
66 ms |
23372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
23508 KB |
Output is correct |
2 |
Correct |
81 ms |
22356 KB |
Output is correct |
3 |
Correct |
84 ms |
24152 KB |
Output is correct |
4 |
Correct |
90 ms |
24104 KB |
Output is correct |
5 |
Correct |
82 ms |
24148 KB |
Output is correct |
6 |
Correct |
88 ms |
24140 KB |
Output is correct |
7 |
Correct |
85 ms |
23880 KB |
Output is correct |
8 |
Correct |
90 ms |
26432 KB |
Output is correct |
9 |
Correct |
87 ms |
23372 KB |
Output is correct |
10 |
Correct |
84 ms |
23376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7512 KB |
Output is correct |
2 |
Correct |
43 ms |
23504 KB |
Output is correct |
3 |
Correct |
38 ms |
23404 KB |
Output is correct |
4 |
Correct |
58 ms |
21332 KB |
Output is correct |
5 |
Correct |
58 ms |
23380 KB |
Output is correct |
6 |
Correct |
57 ms |
23384 KB |
Output is correct |
7 |
Correct |
54 ms |
23376 KB |
Output is correct |
8 |
Correct |
59 ms |
23384 KB |
Output is correct |
9 |
Correct |
74 ms |
23124 KB |
Output is correct |
10 |
Correct |
64 ms |
22592 KB |
Output is correct |
11 |
Correct |
58 ms |
20164 KB |
Output is correct |
12 |
Correct |
4 ms |
7772 KB |
Output is correct |
13 |
Correct |
4 ms |
7772 KB |
Output is correct |
14 |
Correct |
3 ms |
7772 KB |
Output is correct |
15 |
Correct |
3 ms |
7772 KB |
Output is correct |
16 |
Correct |
3 ms |
7772 KB |
Output is correct |
17 |
Correct |
3 ms |
7772 KB |
Output is correct |
18 |
Correct |
3 ms |
7772 KB |
Output is correct |
19 |
Correct |
3 ms |
7772 KB |
Output is correct |
20 |
Correct |
17 ms |
12380 KB |
Output is correct |
21 |
Correct |
16 ms |
12376 KB |
Output is correct |
22 |
Correct |
17 ms |
12376 KB |
Output is correct |
23 |
Correct |
20 ms |
12332 KB |
Output is correct |
24 |
Correct |
19 ms |
12120 KB |
Output is correct |
25 |
Correct |
18 ms |
12128 KB |
Output is correct |
26 |
Correct |
19 ms |
12380 KB |
Output is correct |
27 |
Correct |
19 ms |
12384 KB |
Output is correct |
28 |
Correct |
18 ms |
12376 KB |
Output is correct |
29 |
Correct |
20 ms |
12380 KB |
Output is correct |
30 |
Correct |
23 ms |
12332 KB |
Output is correct |
31 |
Correct |
20 ms |
12120 KB |
Output is correct |
32 |
Correct |
18 ms |
12112 KB |
Output is correct |
33 |
Correct |
16 ms |
11480 KB |
Output is correct |
34 |
Correct |
59 ms |
24272 KB |
Output is correct |
35 |
Correct |
60 ms |
24144 KB |
Output is correct |
36 |
Correct |
59 ms |
24132 KB |
Output is correct |
37 |
Correct |
60 ms |
24072 KB |
Output is correct |
38 |
Correct |
61 ms |
23884 KB |
Output is correct |
39 |
Correct |
84 ms |
26432 KB |
Output is correct |
40 |
Correct |
61 ms |
23368 KB |
Output is correct |
41 |
Correct |
76 ms |
21172 KB |
Output is correct |
42 |
Correct |
66 ms |
22356 KB |
Output is correct |
43 |
Correct |
70 ms |
24144 KB |
Output is correct |
44 |
Correct |
68 ms |
24132 KB |
Output is correct |
45 |
Correct |
69 ms |
24136 KB |
Output is correct |
46 |
Correct |
69 ms |
26492 KB |
Output is correct |
47 |
Correct |
66 ms |
23372 KB |
Output is correct |
48 |
Correct |
57 ms |
23508 KB |
Output is correct |
49 |
Correct |
81 ms |
22356 KB |
Output is correct |
50 |
Correct |
84 ms |
24152 KB |
Output is correct |
51 |
Correct |
90 ms |
24104 KB |
Output is correct |
52 |
Correct |
82 ms |
24148 KB |
Output is correct |
53 |
Correct |
88 ms |
24140 KB |
Output is correct |
54 |
Correct |
85 ms |
23880 KB |
Output is correct |
55 |
Correct |
90 ms |
26432 KB |
Output is correct |
56 |
Correct |
87 ms |
23372 KB |
Output is correct |
57 |
Correct |
84 ms |
23376 KB |
Output is correct |
58 |
Correct |
69 ms |
24268 KB |
Output is correct |
59 |
Correct |
89 ms |
24216 KB |
Output is correct |
60 |
Correct |
89 ms |
21896 KB |
Output is correct |
61 |
Correct |
100 ms |
22568 KB |
Output is correct |
62 |
Correct |
87 ms |
24348 KB |
Output is correct |
63 |
Correct |
119 ms |
24352 KB |
Output is correct |
64 |
Correct |
107 ms |
24348 KB |
Output is correct |
65 |
Correct |
84 ms |
24344 KB |
Output is correct |
66 |
Correct |
87 ms |
24384 KB |
Output is correct |
67 |
Correct |
94 ms |
24096 KB |
Output is correct |
68 |
Correct |
83 ms |
26448 KB |
Output is correct |
69 |
Correct |
88 ms |
23588 KB |
Output is correct |
70 |
Correct |
82 ms |
22572 KB |
Output is correct |
71 |
Correct |
101 ms |
21292 KB |
Output is correct |