#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = (int) 1e9 + 1e6 + 123;
const ll LINF = (ll) 1e18 + 1e9 + 123;
#define rep(i, s, t) for (auto i = (s); i < (t); ++(i))
#define per(i, s, t) for (auto i = (s); i >= (t); --(i))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
bool mini(auto &x, const auto &y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
bool maxi(auto &x, const auto &y) {
if (y > x) {
x = y;
return 1;
}
return 0;
}
void run();
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
run();
return 0;
}
const int N = (int) 1e5 + 123;
struct F {
int f[N];
void add(int x, int y) {
x++;
for (; x < N; x += x & (-x)) f[x] += y;
}
int get(int i) {
i++;
int res = 0;
for (; i > 0; i -= i & (-i)) res += f[i];
return res;
}
};
int n;
int c[N];
map<int, int> comp;
vector<int> g[N];
struct HLD {
int up, down;
vector<pair<int, int>> st;
void set(int len, int c) {
int alr = 0;
while (sz(st) && st.back().second + alr <= len) {
alr += st.back().second;
st.pop_back();
}
assert(alr <= len);
if (alr < len) {
st.back().second -= (len - alr);
}
st.pb({c, len});
}
};
int ptr;
HLD hld[N];
int id[N];
int depth[N];
int cnt[N];
int p[N];
pair<int, int> q[N];
void precalc(int v) {
cnt[v] = 1;
for (int u : g[v]) {
precalc(u);
cnt[v] += cnt[u];
}
}
void build_hld(int v, int cur = -1) {
if (cur == -1) {
cur = ptr++;
hld[cur].up = v;
}
id[v] = cur;
hld[cur].down = v;
int to = -1;
for (int u : g[v]) {
if (to == -1 || cnt[u] > cnt[to]) {
to = u;
}
}
if (to != -1) {
build_hld(to, cur);
}
for (int u : g[v]) {
if (u != to) {
build_hld(u, -1);
}
}
}
F f;
ll get(int v) {
static vector<pair<int, int>> cool;
cool.clear();
while (v > 0) {
int w = id[v];
int len = depth[v] - depth[hld[w].up] + 1;
int alr = 0;
int ptr = sz(hld[w].st) - 1;
while (ptr >= 0 && hld[w].st[ptr].second + alr <= len) {
alr += hld[w].st[ptr].second;
ptr--;
}
if (alr < len) {
assert(ptr >= 0);
cool.pb({hld[w].st[ptr].first, len - alr});
}
rep(i, ptr + 1, sz(hld[w].st)) {
cool.pb(hld[w].st[i]);
}
v = p[hld[w].up];
}
ll ans = 0;
for (auto &it : cool) {
ans += it.second * f.get(it.first - 1);
f.add(it.first, it.second);
}
for (auto &it : cool) {
f.add(it.first, -it.second);
}
return ans;
}
void set_color(int v, int c) {
while (v > 0) {
int w = id[v];
int len = depth[v] - depth[hld[w].up] + 1;
hld[w].set(len, c);
v = p[hld[w].up];
}
}
void run() {
cin >> n;
rep(i, 1, n + 1) {
cin >> c[i];
comp[c[i]] = 1;
}
{
int ptr = 0;
for (auto &it : comp) {
it.second = ptr++;
}
}
rep(i, 1, n + 1) {
c[i] = comp[c[i]];
}
rep(i, 1, n) {
int a, b;
cin >> a >> b;
g[a].pb(b);
p[b] = a;
depth[b] = depth[a] + 1;
q[i] = {a, b};
}
precalc(1);
build_hld(1);
rep(i, 0, ptr) {
hld[i].st.pb({-1, depth[hld[i].down] - depth[hld[i].up] + 1});
}
set_color(1, c[1]);
rep(i, 1, n) {
cout << get(q[i].first) << "\n";
set_color(q[i].second, c[q[i].second]);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5880 KB |
Output is correct |
2 |
Correct |
6 ms |
5988 KB |
Output is correct |
3 |
Correct |
6 ms |
5988 KB |
Output is correct |
4 |
Correct |
6 ms |
5988 KB |
Output is correct |
5 |
Correct |
6 ms |
6228 KB |
Output is correct |
6 |
Correct |
6 ms |
6228 KB |
Output is correct |
7 |
Correct |
6 ms |
6228 KB |
Output is correct |
8 |
Correct |
6 ms |
6228 KB |
Output is correct |
9 |
Correct |
6 ms |
6244 KB |
Output is correct |
10 |
Correct |
6 ms |
6244 KB |
Output is correct |
11 |
Correct |
6 ms |
6244 KB |
Output is correct |
12 |
Correct |
6 ms |
6244 KB |
Output is correct |
13 |
Correct |
6 ms |
6244 KB |
Output is correct |
14 |
Correct |
6 ms |
6244 KB |
Output is correct |
15 |
Correct |
7 ms |
6252 KB |
Output is correct |
16 |
Correct |
6 ms |
6252 KB |
Output is correct |
17 |
Correct |
6 ms |
6252 KB |
Output is correct |
18 |
Correct |
6 ms |
6252 KB |
Output is correct |
19 |
Correct |
6 ms |
6252 KB |
Output is correct |
20 |
Correct |
6 ms |
6252 KB |
Output is correct |
21 |
Correct |
6 ms |
6252 KB |
Output is correct |
22 |
Correct |
6 ms |
6252 KB |
Output is correct |
23 |
Correct |
6 ms |
6252 KB |
Output is correct |
24 |
Correct |
6 ms |
6252 KB |
Output is correct |
25 |
Correct |
6 ms |
6252 KB |
Output is correct |
26 |
Correct |
6 ms |
6252 KB |
Output is correct |
27 |
Correct |
6 ms |
6252 KB |
Output is correct |
28 |
Correct |
6 ms |
6252 KB |
Output is correct |
29 |
Correct |
6 ms |
6252 KB |
Output is correct |
30 |
Correct |
6 ms |
6252 KB |
Output is correct |
31 |
Correct |
6 ms |
6252 KB |
Output is correct |
32 |
Correct |
6 ms |
6252 KB |
Output is correct |
33 |
Correct |
6 ms |
6252 KB |
Output is correct |
34 |
Correct |
6 ms |
6252 KB |
Output is correct |
35 |
Correct |
6 ms |
6252 KB |
Output is correct |
36 |
Correct |
6 ms |
6260 KB |
Output is correct |
37 |
Correct |
6 ms |
6260 KB |
Output is correct |
38 |
Correct |
6 ms |
6260 KB |
Output is correct |
39 |
Correct |
6 ms |
6260 KB |
Output is correct |
40 |
Correct |
6 ms |
6260 KB |
Output is correct |
41 |
Correct |
6 ms |
6260 KB |
Output is correct |
42 |
Correct |
6 ms |
6260 KB |
Output is correct |
43 |
Correct |
6 ms |
6260 KB |
Output is correct |
44 |
Correct |
6 ms |
6260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5880 KB |
Output is correct |
2 |
Correct |
6 ms |
5988 KB |
Output is correct |
3 |
Correct |
6 ms |
5988 KB |
Output is correct |
4 |
Correct |
6 ms |
5988 KB |
Output is correct |
5 |
Correct |
6 ms |
6228 KB |
Output is correct |
6 |
Correct |
6 ms |
6228 KB |
Output is correct |
7 |
Correct |
6 ms |
6228 KB |
Output is correct |
8 |
Correct |
6 ms |
6228 KB |
Output is correct |
9 |
Correct |
6 ms |
6244 KB |
Output is correct |
10 |
Correct |
6 ms |
6244 KB |
Output is correct |
11 |
Correct |
6 ms |
6244 KB |
Output is correct |
12 |
Correct |
6 ms |
6244 KB |
Output is correct |
13 |
Correct |
6 ms |
6244 KB |
Output is correct |
14 |
Correct |
6 ms |
6244 KB |
Output is correct |
15 |
Correct |
7 ms |
6252 KB |
Output is correct |
16 |
Correct |
6 ms |
6252 KB |
Output is correct |
17 |
Correct |
6 ms |
6252 KB |
Output is correct |
18 |
Correct |
6 ms |
6252 KB |
Output is correct |
19 |
Correct |
6 ms |
6252 KB |
Output is correct |
20 |
Correct |
6 ms |
6252 KB |
Output is correct |
21 |
Correct |
6 ms |
6252 KB |
Output is correct |
22 |
Correct |
6 ms |
6252 KB |
Output is correct |
23 |
Correct |
6 ms |
6252 KB |
Output is correct |
24 |
Correct |
6 ms |
6252 KB |
Output is correct |
25 |
Correct |
6 ms |
6252 KB |
Output is correct |
26 |
Correct |
6 ms |
6252 KB |
Output is correct |
27 |
Correct |
6 ms |
6252 KB |
Output is correct |
28 |
Correct |
6 ms |
6252 KB |
Output is correct |
29 |
Correct |
6 ms |
6252 KB |
Output is correct |
30 |
Correct |
6 ms |
6252 KB |
Output is correct |
31 |
Correct |
6 ms |
6252 KB |
Output is correct |
32 |
Correct |
6 ms |
6252 KB |
Output is correct |
33 |
Correct |
6 ms |
6252 KB |
Output is correct |
34 |
Correct |
6 ms |
6252 KB |
Output is correct |
35 |
Correct |
6 ms |
6252 KB |
Output is correct |
36 |
Correct |
6 ms |
6260 KB |
Output is correct |
37 |
Correct |
6 ms |
6260 KB |
Output is correct |
38 |
Correct |
6 ms |
6260 KB |
Output is correct |
39 |
Correct |
6 ms |
6260 KB |
Output is correct |
40 |
Correct |
6 ms |
6260 KB |
Output is correct |
41 |
Correct |
6 ms |
6260 KB |
Output is correct |
42 |
Correct |
6 ms |
6260 KB |
Output is correct |
43 |
Correct |
6 ms |
6260 KB |
Output is correct |
44 |
Correct |
6 ms |
6260 KB |
Output is correct |
45 |
Correct |
7 ms |
6268 KB |
Output is correct |
46 |
Correct |
12 ms |
6652 KB |
Output is correct |
47 |
Correct |
12 ms |
6652 KB |
Output is correct |
48 |
Correct |
12 ms |
6668 KB |
Output is correct |
49 |
Correct |
10 ms |
6812 KB |
Output is correct |
50 |
Correct |
10 ms |
6828 KB |
Output is correct |
51 |
Correct |
10 ms |
6828 KB |
Output is correct |
52 |
Correct |
10 ms |
6828 KB |
Output is correct |
53 |
Correct |
10 ms |
6828 KB |
Output is correct |
54 |
Correct |
10 ms |
6828 KB |
Output is correct |
55 |
Correct |
10 ms |
6828 KB |
Output is correct |
56 |
Correct |
10 ms |
6828 KB |
Output is correct |
57 |
Correct |
13 ms |
6828 KB |
Output is correct |
58 |
Correct |
14 ms |
6828 KB |
Output is correct |
59 |
Correct |
14 ms |
6828 KB |
Output is correct |
60 |
Correct |
13 ms |
6828 KB |
Output is correct |
61 |
Correct |
10 ms |
6828 KB |
Output is correct |
62 |
Correct |
10 ms |
6828 KB |
Output is correct |
63 |
Correct |
11 ms |
6828 KB |
Output is correct |
64 |
Correct |
10 ms |
6828 KB |
Output is correct |
65 |
Correct |
11 ms |
6828 KB |
Output is correct |
66 |
Correct |
11 ms |
6828 KB |
Output is correct |
67 |
Correct |
11 ms |
6828 KB |
Output is correct |
68 |
Correct |
8 ms |
6828 KB |
Output is correct |
69 |
Correct |
10 ms |
6828 KB |
Output is correct |
70 |
Correct |
9 ms |
6828 KB |
Output is correct |
71 |
Correct |
9 ms |
6828 KB |
Output is correct |
72 |
Correct |
13 ms |
6828 KB |
Output is correct |
73 |
Correct |
12 ms |
6828 KB |
Output is correct |
74 |
Correct |
9 ms |
6828 KB |
Output is correct |
75 |
Correct |
10 ms |
6828 KB |
Output is correct |
76 |
Correct |
10 ms |
6828 KB |
Output is correct |
77 |
Correct |
10 ms |
6828 KB |
Output is correct |
78 |
Correct |
9 ms |
6828 KB |
Output is correct |
79 |
Correct |
9 ms |
6828 KB |
Output is correct |
80 |
Correct |
9 ms |
6828 KB |
Output is correct |
81 |
Correct |
10 ms |
6828 KB |
Output is correct |
82 |
Correct |
10 ms |
6828 KB |
Output is correct |
83 |
Correct |
10 ms |
6828 KB |
Output is correct |
84 |
Correct |
9 ms |
6828 KB |
Output is correct |
85 |
Correct |
9 ms |
6828 KB |
Output is correct |
86 |
Correct |
9 ms |
6828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5880 KB |
Output is correct |
2 |
Correct |
6 ms |
5988 KB |
Output is correct |
3 |
Correct |
6 ms |
5988 KB |
Output is correct |
4 |
Correct |
6 ms |
5988 KB |
Output is correct |
5 |
Correct |
6 ms |
6228 KB |
Output is correct |
6 |
Correct |
6 ms |
6228 KB |
Output is correct |
7 |
Correct |
6 ms |
6228 KB |
Output is correct |
8 |
Correct |
6 ms |
6228 KB |
Output is correct |
9 |
Correct |
6 ms |
6244 KB |
Output is correct |
10 |
Correct |
6 ms |
6244 KB |
Output is correct |
11 |
Correct |
6 ms |
6244 KB |
Output is correct |
12 |
Correct |
6 ms |
6244 KB |
Output is correct |
13 |
Correct |
6 ms |
6244 KB |
Output is correct |
14 |
Correct |
6 ms |
6244 KB |
Output is correct |
15 |
Correct |
7 ms |
6252 KB |
Output is correct |
16 |
Correct |
6 ms |
6252 KB |
Output is correct |
17 |
Correct |
6 ms |
6252 KB |
Output is correct |
18 |
Correct |
6 ms |
6252 KB |
Output is correct |
19 |
Correct |
6 ms |
6252 KB |
Output is correct |
20 |
Correct |
6 ms |
6252 KB |
Output is correct |
21 |
Correct |
6 ms |
6252 KB |
Output is correct |
22 |
Correct |
6 ms |
6252 KB |
Output is correct |
23 |
Correct |
6 ms |
6252 KB |
Output is correct |
24 |
Correct |
6 ms |
6252 KB |
Output is correct |
25 |
Correct |
6 ms |
6252 KB |
Output is correct |
26 |
Correct |
6 ms |
6252 KB |
Output is correct |
27 |
Correct |
6 ms |
6252 KB |
Output is correct |
28 |
Correct |
6 ms |
6252 KB |
Output is correct |
29 |
Correct |
6 ms |
6252 KB |
Output is correct |
30 |
Correct |
6 ms |
6252 KB |
Output is correct |
31 |
Correct |
6 ms |
6252 KB |
Output is correct |
32 |
Correct |
6 ms |
6252 KB |
Output is correct |
33 |
Correct |
6 ms |
6252 KB |
Output is correct |
34 |
Correct |
6 ms |
6252 KB |
Output is correct |
35 |
Correct |
6 ms |
6252 KB |
Output is correct |
36 |
Correct |
6 ms |
6260 KB |
Output is correct |
37 |
Correct |
6 ms |
6260 KB |
Output is correct |
38 |
Correct |
6 ms |
6260 KB |
Output is correct |
39 |
Correct |
6 ms |
6260 KB |
Output is correct |
40 |
Correct |
6 ms |
6260 KB |
Output is correct |
41 |
Correct |
6 ms |
6260 KB |
Output is correct |
42 |
Correct |
6 ms |
6260 KB |
Output is correct |
43 |
Correct |
6 ms |
6260 KB |
Output is correct |
44 |
Correct |
6 ms |
6260 KB |
Output is correct |
45 |
Correct |
7 ms |
6268 KB |
Output is correct |
46 |
Correct |
12 ms |
6652 KB |
Output is correct |
47 |
Correct |
12 ms |
6652 KB |
Output is correct |
48 |
Correct |
12 ms |
6668 KB |
Output is correct |
49 |
Correct |
10 ms |
6812 KB |
Output is correct |
50 |
Correct |
10 ms |
6828 KB |
Output is correct |
51 |
Correct |
10 ms |
6828 KB |
Output is correct |
52 |
Correct |
10 ms |
6828 KB |
Output is correct |
53 |
Correct |
10 ms |
6828 KB |
Output is correct |
54 |
Correct |
10 ms |
6828 KB |
Output is correct |
55 |
Correct |
10 ms |
6828 KB |
Output is correct |
56 |
Correct |
10 ms |
6828 KB |
Output is correct |
57 |
Correct |
13 ms |
6828 KB |
Output is correct |
58 |
Correct |
14 ms |
6828 KB |
Output is correct |
59 |
Correct |
14 ms |
6828 KB |
Output is correct |
60 |
Correct |
13 ms |
6828 KB |
Output is correct |
61 |
Correct |
10 ms |
6828 KB |
Output is correct |
62 |
Correct |
10 ms |
6828 KB |
Output is correct |
63 |
Correct |
11 ms |
6828 KB |
Output is correct |
64 |
Correct |
10 ms |
6828 KB |
Output is correct |
65 |
Correct |
11 ms |
6828 KB |
Output is correct |
66 |
Correct |
11 ms |
6828 KB |
Output is correct |
67 |
Correct |
11 ms |
6828 KB |
Output is correct |
68 |
Correct |
8 ms |
6828 KB |
Output is correct |
69 |
Correct |
10 ms |
6828 KB |
Output is correct |
70 |
Correct |
9 ms |
6828 KB |
Output is correct |
71 |
Correct |
9 ms |
6828 KB |
Output is correct |
72 |
Correct |
13 ms |
6828 KB |
Output is correct |
73 |
Correct |
12 ms |
6828 KB |
Output is correct |
74 |
Correct |
9 ms |
6828 KB |
Output is correct |
75 |
Correct |
10 ms |
6828 KB |
Output is correct |
76 |
Correct |
10 ms |
6828 KB |
Output is correct |
77 |
Correct |
10 ms |
6828 KB |
Output is correct |
78 |
Correct |
9 ms |
6828 KB |
Output is correct |
79 |
Correct |
9 ms |
6828 KB |
Output is correct |
80 |
Correct |
9 ms |
6828 KB |
Output is correct |
81 |
Correct |
10 ms |
6828 KB |
Output is correct |
82 |
Correct |
10 ms |
6828 KB |
Output is correct |
83 |
Correct |
10 ms |
6828 KB |
Output is correct |
84 |
Correct |
9 ms |
6828 KB |
Output is correct |
85 |
Correct |
9 ms |
6828 KB |
Output is correct |
86 |
Correct |
9 ms |
6828 KB |
Output is correct |
87 |
Correct |
23 ms |
7356 KB |
Output is correct |
88 |
Correct |
70 ms |
9660 KB |
Output is correct |
89 |
Correct |
281 ms |
17808 KB |
Output is correct |
90 |
Correct |
273 ms |
17816 KB |
Output is correct |
91 |
Correct |
264 ms |
17816 KB |
Output is correct |
92 |
Correct |
171 ms |
22216 KB |
Output is correct |
93 |
Correct |
194 ms |
22216 KB |
Output is correct |
94 |
Correct |
186 ms |
22216 KB |
Output is correct |
95 |
Correct |
175 ms |
22216 KB |
Output is correct |
96 |
Correct |
213 ms |
22216 KB |
Output is correct |
97 |
Correct |
193 ms |
22216 KB |
Output is correct |
98 |
Correct |
185 ms |
22216 KB |
Output is correct |
99 |
Correct |
186 ms |
22216 KB |
Output is correct |
100 |
Correct |
324 ms |
22216 KB |
Output is correct |
101 |
Correct |
361 ms |
22216 KB |
Output is correct |
102 |
Correct |
372 ms |
22216 KB |
Output is correct |
103 |
Correct |
358 ms |
22216 KB |
Output is correct |
104 |
Correct |
189 ms |
22216 KB |
Output is correct |
105 |
Correct |
211 ms |
22216 KB |
Output is correct |
106 |
Correct |
194 ms |
22216 KB |
Output is correct |
107 |
Correct |
192 ms |
22216 KB |
Output is correct |
108 |
Correct |
188 ms |
22216 KB |
Output is correct |
109 |
Correct |
242 ms |
22216 KB |
Output is correct |
110 |
Correct |
97 ms |
22216 KB |
Output is correct |
111 |
Correct |
189 ms |
22216 KB |
Output is correct |
112 |
Correct |
111 ms |
22216 KB |
Output is correct |
113 |
Correct |
115 ms |
22216 KB |
Output is correct |
114 |
Correct |
335 ms |
22216 KB |
Output is correct |
115 |
Correct |
278 ms |
22216 KB |
Output is correct |
116 |
Correct |
135 ms |
22216 KB |
Output is correct |
117 |
Correct |
187 ms |
22216 KB |
Output is correct |
118 |
Correct |
191 ms |
22216 KB |
Output is correct |
119 |
Correct |
189 ms |
22216 KB |
Output is correct |
120 |
Correct |
117 ms |
22216 KB |
Output is correct |
121 |
Correct |
118 ms |
22216 KB |
Output is correct |
122 |
Correct |
114 ms |
22216 KB |
Output is correct |
123 |
Correct |
194 ms |
22216 KB |
Output is correct |
124 |
Correct |
193 ms |
22216 KB |
Output is correct |
125 |
Correct |
201 ms |
22216 KB |
Output is correct |
126 |
Correct |
130 ms |
22216 KB |
Output is correct |
127 |
Correct |
126 ms |
22216 KB |
Output is correct |
128 |
Correct |
129 ms |
22216 KB |
Output is correct |