#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
int n, c[maxn], pos[maxn], par[maxn], up[maxn], sz[maxn], st[maxn], Time = 1;
vector<int> t[maxn];
pair<int,int> edge[maxn];
int mxm[4 * maxn], mnm[4 * maxn], lzy[4 * maxn];
void propagate(int, int, int);
pair<int,int> get(int id, int L, int R, int l, int r, int x){
if (r <= L or R <= l)
return {-1, -1};
if (mxm[id] == x and mnm[id] == x)
return {-1, -1};
if (L + 1 == R)
return {L, mxm[id]};
propagate(id, L, R);
int mid = (L + R) >> 1;
auto it = get(2 * id + 1, mid, R, l, r, x);
if (it.first != -1)
return it;
return get(2 * id + 0, L, mid, l, r, x);
}
void change(int id, int L, int R, int l, int r, int x){
if (L == l and R == r){
mxm[id] = x;
mnm[id] = x;
lzy[id] = x;
return;
}
propagate(id, L, R);
int mid = (L + R) >> 1;
if (l < mid)
change(2 * id + 0, L, mid, l, min(mid, r), x);
if (mid < r)
change(2 * id + 1, mid, R, max(l, mid), r, x);
mxm[id] = max(mxm[2 * id + 0], mxm[2 * id + 1]);
mnm[id] = min(mnm[2 * id + 0], mnm[2 * id + 1]);
}
void propagate(int id, int L, int R){
if (lzy[id] == 0)
return;
int mid = (L + R) >> 1;
change(2 * id + 0, L, mid, L, mid, lzy[id]);
change(2 * id + 1, mid, R, mid, R, lzy[id]);
lzy[id] = 0;
}
void change(int v){
int val = c[v];
while (v != 0){
int u = up[v];
change(1, 1, n + 1, st[u], st[v] + 1, val);
v = par[u];
}
}
int fen[maxn];
int getfen(int idx){
int ret = 0;
for (; idx; idx -= idx & -idx)
ret += fen[idx];
return ret;
}
void add(int idx, int x){
for (; idx <= n; idx += idx & -idx){
fen[idx] += x;
if (x == -1)
fen[idx] = 0;
}
}
int vitex[maxn];
ll get(int v){
ll sum = 0;
int cnt = 0;
int tmp = c[v];
int ptr = 0;
while (v != 0){
int u = up[v];
auto it = get(1, 1, n + 1, st[u], st[v] + 1, tmp);
if (it.first == -1){
cnt += st[v] - st[u] + 1;
v = par[u];
}
else{
cnt += st[v] - it.first;
sum += 1ll * cnt * getfen(tmp - 1);
add(tmp, cnt);
vitex[++ptr] = tmp;
cnt = 0;
v = pos[it.first];
tmp = it.second;
}
}
sum += 1ll * cnt * getfen(tmp - 1);
while (ptr > 0){
add(vitex[ptr --], -1);
}
return sum;
}
bool cmp(int fi, int se){
return sz[fi] > sz[se];
}
void dfs(int v){
pos[Time] = v;
st[v] = Time ++;
sort(t[v].begin(), t[v].end(), cmp);
bool heavy = 1;
for (auto u : t[v]){
up[u] = (heavy == 1 ? up[v] : u);
heavy = 0;
dfs(u);
}
}
int dfssz(int v){
sz[v] = 1;
for (auto u : t[v])
sz[v] += dfssz(u);
return sz[v];
}
int main(){
ios_base::sync_with_stdio(false);
cin >> n;
vector<int> v;
for (int i = 1; i <= n; i++){
cin >> c[i];
v.push_back(c[i]);
}
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
for (int i = 1; i <= n; i++)
c[i] = lower_bound(v.begin(), v.end(), c[i]) - v.begin() + 1;
for (int i = 1; i <= n - 1; i++){
int v, u;
cin >> v >> u;
edge[i] = {v, u};
par[u] = v;
t[v].push_back(u);
}
dfssz(1);
up[1] = 1;
dfs(1);
for (int v = 1; v <= n; v++)
change(1, 1, n + 1, st[v], st[v] + 1, c[v]);
for (int i = 1; i <= n - 1; i++){
int v = edge[i].first, u = edge[i].second;
cout << get(v) << '\n';
change(u);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2816 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
4 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2816 KB |
Output is correct |
5 |
Correct |
5 ms |
2816 KB |
Output is correct |
6 |
Correct |
6 ms |
2816 KB |
Output is correct |
7 |
Correct |
6 ms |
2816 KB |
Output is correct |
8 |
Correct |
5 ms |
2816 KB |
Output is correct |
9 |
Correct |
5 ms |
2788 KB |
Output is correct |
10 |
Correct |
5 ms |
2816 KB |
Output is correct |
11 |
Correct |
5 ms |
2816 KB |
Output is correct |
12 |
Correct |
6 ms |
2816 KB |
Output is correct |
13 |
Correct |
5 ms |
2788 KB |
Output is correct |
14 |
Correct |
6 ms |
2816 KB |
Output is correct |
15 |
Correct |
6 ms |
2816 KB |
Output is correct |
16 |
Correct |
6 ms |
2816 KB |
Output is correct |
17 |
Correct |
6 ms |
2816 KB |
Output is correct |
18 |
Correct |
8 ms |
2816 KB |
Output is correct |
19 |
Correct |
6 ms |
2816 KB |
Output is correct |
20 |
Correct |
5 ms |
2816 KB |
Output is correct |
21 |
Correct |
5 ms |
2816 KB |
Output is correct |
22 |
Correct |
4 ms |
2688 KB |
Output is correct |
23 |
Correct |
6 ms |
2816 KB |
Output is correct |
24 |
Correct |
6 ms |
2816 KB |
Output is correct |
25 |
Correct |
6 ms |
2816 KB |
Output is correct |
26 |
Correct |
5 ms |
2816 KB |
Output is correct |
27 |
Correct |
5 ms |
2816 KB |
Output is correct |
28 |
Correct |
5 ms |
2816 KB |
Output is correct |
29 |
Correct |
5 ms |
2816 KB |
Output is correct |
30 |
Correct |
6 ms |
2816 KB |
Output is correct |
31 |
Correct |
6 ms |
2816 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
2816 KB |
Output is correct |
34 |
Correct |
5 ms |
2816 KB |
Output is correct |
35 |
Correct |
7 ms |
2816 KB |
Output is correct |
36 |
Correct |
5 ms |
2816 KB |
Output is correct |
37 |
Correct |
5 ms |
2744 KB |
Output is correct |
38 |
Correct |
6 ms |
2816 KB |
Output is correct |
39 |
Correct |
5 ms |
2816 KB |
Output is correct |
40 |
Correct |
7 ms |
2816 KB |
Output is correct |
41 |
Correct |
6 ms |
2816 KB |
Output is correct |
42 |
Correct |
5 ms |
2816 KB |
Output is correct |
43 |
Correct |
7 ms |
2816 KB |
Output is correct |
44 |
Correct |
6 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2816 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
4 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2816 KB |
Output is correct |
5 |
Correct |
5 ms |
2816 KB |
Output is correct |
6 |
Correct |
6 ms |
2816 KB |
Output is correct |
7 |
Correct |
6 ms |
2816 KB |
Output is correct |
8 |
Correct |
5 ms |
2816 KB |
Output is correct |
9 |
Correct |
5 ms |
2788 KB |
Output is correct |
10 |
Correct |
5 ms |
2816 KB |
Output is correct |
11 |
Correct |
5 ms |
2816 KB |
Output is correct |
12 |
Correct |
6 ms |
2816 KB |
Output is correct |
13 |
Correct |
5 ms |
2788 KB |
Output is correct |
14 |
Correct |
6 ms |
2816 KB |
Output is correct |
15 |
Correct |
6 ms |
2816 KB |
Output is correct |
16 |
Correct |
6 ms |
2816 KB |
Output is correct |
17 |
Correct |
6 ms |
2816 KB |
Output is correct |
18 |
Correct |
8 ms |
2816 KB |
Output is correct |
19 |
Correct |
6 ms |
2816 KB |
Output is correct |
20 |
Correct |
5 ms |
2816 KB |
Output is correct |
21 |
Correct |
5 ms |
2816 KB |
Output is correct |
22 |
Correct |
4 ms |
2688 KB |
Output is correct |
23 |
Correct |
6 ms |
2816 KB |
Output is correct |
24 |
Correct |
6 ms |
2816 KB |
Output is correct |
25 |
Correct |
6 ms |
2816 KB |
Output is correct |
26 |
Correct |
5 ms |
2816 KB |
Output is correct |
27 |
Correct |
5 ms |
2816 KB |
Output is correct |
28 |
Correct |
5 ms |
2816 KB |
Output is correct |
29 |
Correct |
5 ms |
2816 KB |
Output is correct |
30 |
Correct |
6 ms |
2816 KB |
Output is correct |
31 |
Correct |
6 ms |
2816 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
2816 KB |
Output is correct |
34 |
Correct |
5 ms |
2816 KB |
Output is correct |
35 |
Correct |
7 ms |
2816 KB |
Output is correct |
36 |
Correct |
5 ms |
2816 KB |
Output is correct |
37 |
Correct |
5 ms |
2744 KB |
Output is correct |
38 |
Correct |
6 ms |
2816 KB |
Output is correct |
39 |
Correct |
5 ms |
2816 KB |
Output is correct |
40 |
Correct |
7 ms |
2816 KB |
Output is correct |
41 |
Correct |
6 ms |
2816 KB |
Output is correct |
42 |
Correct |
5 ms |
2816 KB |
Output is correct |
43 |
Correct |
7 ms |
2816 KB |
Output is correct |
44 |
Correct |
6 ms |
2816 KB |
Output is correct |
45 |
Correct |
7 ms |
2816 KB |
Output is correct |
46 |
Correct |
16 ms |
3116 KB |
Output is correct |
47 |
Correct |
17 ms |
3072 KB |
Output is correct |
48 |
Correct |
18 ms |
3192 KB |
Output is correct |
49 |
Correct |
11 ms |
3328 KB |
Output is correct |
50 |
Correct |
11 ms |
3504 KB |
Output is correct |
51 |
Correct |
9 ms |
3300 KB |
Output is correct |
52 |
Correct |
9 ms |
3200 KB |
Output is correct |
53 |
Correct |
15 ms |
3200 KB |
Output is correct |
54 |
Correct |
13 ms |
3200 KB |
Output is correct |
55 |
Correct |
13 ms |
3200 KB |
Output is correct |
56 |
Correct |
11 ms |
3200 KB |
Output is correct |
57 |
Correct |
22 ms |
3072 KB |
Output is correct |
58 |
Correct |
22 ms |
3072 KB |
Output is correct |
59 |
Correct |
25 ms |
3072 KB |
Output is correct |
60 |
Correct |
28 ms |
3192 KB |
Output is correct |
61 |
Correct |
14 ms |
3200 KB |
Output is correct |
62 |
Correct |
12 ms |
3200 KB |
Output is correct |
63 |
Correct |
12 ms |
3200 KB |
Output is correct |
64 |
Correct |
15 ms |
3072 KB |
Output is correct |
65 |
Correct |
15 ms |
3072 KB |
Output is correct |
66 |
Correct |
15 ms |
3072 KB |
Output is correct |
67 |
Correct |
19 ms |
3072 KB |
Output is correct |
68 |
Correct |
9 ms |
3328 KB |
Output is correct |
69 |
Correct |
11 ms |
3200 KB |
Output is correct |
70 |
Correct |
12 ms |
3200 KB |
Output is correct |
71 |
Correct |
12 ms |
3200 KB |
Output is correct |
72 |
Correct |
24 ms |
3072 KB |
Output is correct |
73 |
Correct |
25 ms |
3072 KB |
Output is correct |
74 |
Correct |
11 ms |
3200 KB |
Output is correct |
75 |
Correct |
11 ms |
3200 KB |
Output is correct |
76 |
Correct |
13 ms |
3200 KB |
Output is correct |
77 |
Correct |
11 ms |
3072 KB |
Output is correct |
78 |
Correct |
11 ms |
3200 KB |
Output is correct |
79 |
Correct |
11 ms |
3072 KB |
Output is correct |
80 |
Correct |
11 ms |
3072 KB |
Output is correct |
81 |
Correct |
18 ms |
3200 KB |
Output is correct |
82 |
Correct |
17 ms |
3200 KB |
Output is correct |
83 |
Correct |
13 ms |
3072 KB |
Output is correct |
84 |
Correct |
16 ms |
3200 KB |
Output is correct |
85 |
Correct |
14 ms |
3072 KB |
Output is correct |
86 |
Correct |
11 ms |
3072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2816 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
4 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2816 KB |
Output is correct |
5 |
Correct |
5 ms |
2816 KB |
Output is correct |
6 |
Correct |
6 ms |
2816 KB |
Output is correct |
7 |
Correct |
6 ms |
2816 KB |
Output is correct |
8 |
Correct |
5 ms |
2816 KB |
Output is correct |
9 |
Correct |
5 ms |
2788 KB |
Output is correct |
10 |
Correct |
5 ms |
2816 KB |
Output is correct |
11 |
Correct |
5 ms |
2816 KB |
Output is correct |
12 |
Correct |
6 ms |
2816 KB |
Output is correct |
13 |
Correct |
5 ms |
2788 KB |
Output is correct |
14 |
Correct |
6 ms |
2816 KB |
Output is correct |
15 |
Correct |
6 ms |
2816 KB |
Output is correct |
16 |
Correct |
6 ms |
2816 KB |
Output is correct |
17 |
Correct |
6 ms |
2816 KB |
Output is correct |
18 |
Correct |
8 ms |
2816 KB |
Output is correct |
19 |
Correct |
6 ms |
2816 KB |
Output is correct |
20 |
Correct |
5 ms |
2816 KB |
Output is correct |
21 |
Correct |
5 ms |
2816 KB |
Output is correct |
22 |
Correct |
4 ms |
2688 KB |
Output is correct |
23 |
Correct |
6 ms |
2816 KB |
Output is correct |
24 |
Correct |
6 ms |
2816 KB |
Output is correct |
25 |
Correct |
6 ms |
2816 KB |
Output is correct |
26 |
Correct |
5 ms |
2816 KB |
Output is correct |
27 |
Correct |
5 ms |
2816 KB |
Output is correct |
28 |
Correct |
5 ms |
2816 KB |
Output is correct |
29 |
Correct |
5 ms |
2816 KB |
Output is correct |
30 |
Correct |
6 ms |
2816 KB |
Output is correct |
31 |
Correct |
6 ms |
2816 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
2816 KB |
Output is correct |
34 |
Correct |
5 ms |
2816 KB |
Output is correct |
35 |
Correct |
7 ms |
2816 KB |
Output is correct |
36 |
Correct |
5 ms |
2816 KB |
Output is correct |
37 |
Correct |
5 ms |
2744 KB |
Output is correct |
38 |
Correct |
6 ms |
2816 KB |
Output is correct |
39 |
Correct |
5 ms |
2816 KB |
Output is correct |
40 |
Correct |
7 ms |
2816 KB |
Output is correct |
41 |
Correct |
6 ms |
2816 KB |
Output is correct |
42 |
Correct |
5 ms |
2816 KB |
Output is correct |
43 |
Correct |
7 ms |
2816 KB |
Output is correct |
44 |
Correct |
6 ms |
2816 KB |
Output is correct |
45 |
Correct |
7 ms |
2816 KB |
Output is correct |
46 |
Correct |
16 ms |
3116 KB |
Output is correct |
47 |
Correct |
17 ms |
3072 KB |
Output is correct |
48 |
Correct |
18 ms |
3192 KB |
Output is correct |
49 |
Correct |
11 ms |
3328 KB |
Output is correct |
50 |
Correct |
11 ms |
3504 KB |
Output is correct |
51 |
Correct |
9 ms |
3300 KB |
Output is correct |
52 |
Correct |
9 ms |
3200 KB |
Output is correct |
53 |
Correct |
15 ms |
3200 KB |
Output is correct |
54 |
Correct |
13 ms |
3200 KB |
Output is correct |
55 |
Correct |
13 ms |
3200 KB |
Output is correct |
56 |
Correct |
11 ms |
3200 KB |
Output is correct |
57 |
Correct |
22 ms |
3072 KB |
Output is correct |
58 |
Correct |
22 ms |
3072 KB |
Output is correct |
59 |
Correct |
25 ms |
3072 KB |
Output is correct |
60 |
Correct |
28 ms |
3192 KB |
Output is correct |
61 |
Correct |
14 ms |
3200 KB |
Output is correct |
62 |
Correct |
12 ms |
3200 KB |
Output is correct |
63 |
Correct |
12 ms |
3200 KB |
Output is correct |
64 |
Correct |
15 ms |
3072 KB |
Output is correct |
65 |
Correct |
15 ms |
3072 KB |
Output is correct |
66 |
Correct |
15 ms |
3072 KB |
Output is correct |
67 |
Correct |
19 ms |
3072 KB |
Output is correct |
68 |
Correct |
9 ms |
3328 KB |
Output is correct |
69 |
Correct |
11 ms |
3200 KB |
Output is correct |
70 |
Correct |
12 ms |
3200 KB |
Output is correct |
71 |
Correct |
12 ms |
3200 KB |
Output is correct |
72 |
Correct |
24 ms |
3072 KB |
Output is correct |
73 |
Correct |
25 ms |
3072 KB |
Output is correct |
74 |
Correct |
11 ms |
3200 KB |
Output is correct |
75 |
Correct |
11 ms |
3200 KB |
Output is correct |
76 |
Correct |
13 ms |
3200 KB |
Output is correct |
77 |
Correct |
11 ms |
3072 KB |
Output is correct |
78 |
Correct |
11 ms |
3200 KB |
Output is correct |
79 |
Correct |
11 ms |
3072 KB |
Output is correct |
80 |
Correct |
11 ms |
3072 KB |
Output is correct |
81 |
Correct |
18 ms |
3200 KB |
Output is correct |
82 |
Correct |
17 ms |
3200 KB |
Output is correct |
83 |
Correct |
13 ms |
3072 KB |
Output is correct |
84 |
Correct |
16 ms |
3200 KB |
Output is correct |
85 |
Correct |
14 ms |
3072 KB |
Output is correct |
86 |
Correct |
11 ms |
3072 KB |
Output is correct |
87 |
Correct |
50 ms |
3712 KB |
Output is correct |
88 |
Correct |
137 ms |
5240 KB |
Output is correct |
89 |
Correct |
683 ms |
11636 KB |
Output is correct |
90 |
Correct |
730 ms |
13796 KB |
Output is correct |
91 |
Correct |
691 ms |
13812 KB |
Output is correct |
92 |
Correct |
211 ms |
21104 KB |
Output is correct |
93 |
Correct |
227 ms |
21032 KB |
Output is correct |
94 |
Correct |
220 ms |
20984 KB |
Output is correct |
95 |
Correct |
317 ms |
16500 KB |
Output is correct |
96 |
Correct |
282 ms |
16976 KB |
Output is correct |
97 |
Correct |
272 ms |
16884 KB |
Output is correct |
98 |
Correct |
277 ms |
16756 KB |
Output is correct |
99 |
Correct |
270 ms |
16892 KB |
Output is correct |
100 |
Correct |
1272 ms |
13968 KB |
Output is correct |
101 |
Correct |
1383 ms |
14196 KB |
Output is correct |
102 |
Correct |
1506 ms |
14172 KB |
Output is correct |
103 |
Correct |
1460 ms |
14260 KB |
Output is correct |
104 |
Correct |
430 ms |
16832 KB |
Output is correct |
105 |
Correct |
348 ms |
16756 KB |
Output is correct |
106 |
Correct |
373 ms |
16756 KB |
Output is correct |
107 |
Correct |
556 ms |
12660 KB |
Output is correct |
108 |
Correct |
666 ms |
12668 KB |
Output is correct |
109 |
Correct |
727 ms |
13044 KB |
Output is correct |
110 |
Correct |
195 ms |
20340 KB |
Output is correct |
111 |
Correct |
288 ms |
16472 KB |
Output is correct |
112 |
Correct |
276 ms |
15996 KB |
Output is correct |
113 |
Correct |
282 ms |
15728 KB |
Output is correct |
114 |
Correct |
1302 ms |
14048 KB |
Output is correct |
115 |
Correct |
1161 ms |
13208 KB |
Output is correct |
116 |
Correct |
308 ms |
15860 KB |
Output is correct |
117 |
Correct |
306 ms |
14836 KB |
Output is correct |
118 |
Correct |
300 ms |
14324 KB |
Output is correct |
119 |
Correct |
348 ms |
14192 KB |
Output is correct |
120 |
Correct |
258 ms |
14196 KB |
Output is correct |
121 |
Correct |
283 ms |
13812 KB |
Output is correct |
122 |
Correct |
379 ms |
13428 KB |
Output is correct |
123 |
Correct |
402 ms |
15116 KB |
Output is correct |
124 |
Correct |
434 ms |
14452 KB |
Output is correct |
125 |
Correct |
448 ms |
14196 KB |
Output is correct |
126 |
Correct |
432 ms |
14324 KB |
Output is correct |
127 |
Correct |
367 ms |
13784 KB |
Output is correct |
128 |
Correct |
419 ms |
13588 KB |
Output is correct |