#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e18;
const int N = 1e5 + 10;
const int LG = 17;
int n, lift[N][LG], dep[N], root[N], sz[N], tin[N], par[N], c[N], fen[N], timer = 0, paint[N];
vector<int> adj[N];
pair<int, int> e[N];
void upd(int idx, int k) {
for (; idx < N; idx += (idx & -idx)) fen[idx] += k;
}
int query(int idx) {
int res = 0;
for (; idx; idx -= (idx & -idx)) res += fen[idx];
return res;
}
void dfs(int u, int p) {
sz[u] = 1;
for (int i = 1; i < LG; i++) lift[u][i] = lift[lift[u][i-1]][i-1];
for (auto& v : adj[u]) {
if (v == p) continue;
lift[v][0] = u;
dep[v] = dep[u] + 1;
dfs(v, u);
sz[u] += sz[v];
if (sz[v] > sz[adj[u][0]]) swap(v, adj[u][0]);
}
}
void hld(int u, int p) {
tin[u] = ++timer;
for (int v : adj[u]) {
if (v == p) continue;
root[v] = (v == adj[u][0] ? root[u] : v);
hld(v, u);
}
}
struct SegTree {
int size = 1;
vector<int> seg, lazy;
void init(int n) {
while (size < n) size *= 2;
seg.assign(2 * size + 10, -inf);
lazy.assign(2 * size + 10, -inf);
}
void push(int id) {
if (lazy[id] == -inf) return;
seg[id] = lazy[id];
if (id < size) for (int i = 0; i < 2; i++) lazy[id*2+i] = lazy[id];
lazy[id] = -inf;
}
void update(int ql, int qr, int val, int l, int r, int id) {
push(id);
if (qr < l || r < ql) return;
if (ql <= l && r <= qr) {
lazy[id] = val;
push(id);
return;
}
int mid = (l + r) / 2;
update(ql, qr, val, l, mid, id * 2);
update(ql, qr, val, mid + 1, r, id * 2 + 1);
}
int query(int pos, int l, int r, int id) {
push(id);
if (l == r) return seg[id];
int mid = (l + r) / 2;
if (pos <= mid) return query(pos, l, mid, id * 2);
return query(pos, mid + 1, r, id * 2 + 1);
}
} ST;
void update_path(int x, int y, int val) {
for (; root[x] != root[y]; y = par[root[y]]) {
if (dep[root[x]] > dep[root[y]]) swap(x, y);
// cout << "update " << tin[root[y]] << ' ' << tin[y] << ' ' << val << '\n';
ST.update(tin[root[y]], tin[y], val, 1, n, 1);
}
if (dep[x] > dep[y]) swap(x, y);
// cout << "update " << tin[x] << ' ' << tin[y] << '\n';
ST.update(tin[x], tin[y], val, 1, n, 1);
}
int jump(int sta, int dist) {
for (int i = LG - 1; i >= 0; i--) if (dist & (1 << i)) sta = lift[sta][i];
return sta;
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
vector<int> disc;
for (int i = 1; i <= n; i++) {
cin >> c[i];
disc.push_back(c[i]);
}
sort(disc.begin(), disc.end());
disc.erase(unique(disc.begin(), disc.end()), disc.end());
for (int i = 1; i <= n; i++) {
c[i] = lower_bound(disc.begin(), disc.end(), c[i]) - disc.begin() + 1;
}
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
paint[i] = c[v];
adj[u].push_back(v);
par[v] = u;
e[i] = {u, v};
}
// for (int i = 1; i < n; i++) cout << paint[i] << '\n';
lift[1][0] = 1, root[1] = 1;
dfs(1, -1);
hld(1, -1);
ST.init(n + 1);
for (int i = 1; i < n; i++) {
int node = e[i].first;
vector<pair<int, int>> vec;
int tmp = ST.query(tin[node], 1, n, 1);
// cout << "yo\n";
int rt = i-1;
if (!rt) rt = -inf;
while (node >= 1) {
// cout << "node: " << node << '\n';
if (tmp == rt) {
vec.push_back({tmp, dep[node] + 1});
break;
}
int l = 1, r = dep[node];
// find min x s.t. col[x] !=
while (l < r) {
int mid = (l + r) / 2;
int tar = jump(node, mid);
if (ST.query(tin[tar], 1, n, 1) != tmp) r = mid;
else l = mid + 1;
}
vec.push_back({tmp, l});
node = jump(node, l);
tmp = ST.query(tin[node], 1, n, 1);
}
reverse(vec.begin(), vec.end());
int inv = 0, tot = 0;
if (i > 1) {
for (auto& [x, y] : vec) {
// cout << "pair: " << x << " " << y << '\n';
inv += (tot - query(paint[x])) * y;
upd(paint[x], y);
tot += y;
}
for (auto& [x, y] : vec) upd(paint[x], -y);
}
// cout << "test\n";
update_path(1, e[i].second, i);
cout << inv << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
10576 KB |
Output is correct |
3 |
Correct |
2 ms |
10576 KB |
Output is correct |
4 |
Correct |
3 ms |
10576 KB |
Output is correct |
5 |
Correct |
2 ms |
10576 KB |
Output is correct |
6 |
Correct |
2 ms |
10576 KB |
Output is correct |
7 |
Correct |
3 ms |
10576 KB |
Output is correct |
8 |
Correct |
3 ms |
10576 KB |
Output is correct |
9 |
Correct |
2 ms |
10576 KB |
Output is correct |
10 |
Correct |
2 ms |
10576 KB |
Output is correct |
11 |
Correct |
3 ms |
10576 KB |
Output is correct |
12 |
Correct |
3 ms |
10576 KB |
Output is correct |
13 |
Correct |
2 ms |
10576 KB |
Output is correct |
14 |
Correct |
2 ms |
10576 KB |
Output is correct |
15 |
Correct |
4 ms |
10576 KB |
Output is correct |
16 |
Correct |
3 ms |
10576 KB |
Output is correct |
17 |
Correct |
3 ms |
10576 KB |
Output is correct |
18 |
Correct |
3 ms |
10576 KB |
Output is correct |
19 |
Correct |
2 ms |
10576 KB |
Output is correct |
20 |
Correct |
3 ms |
10576 KB |
Output is correct |
21 |
Correct |
3 ms |
10576 KB |
Output is correct |
22 |
Correct |
2 ms |
10716 KB |
Output is correct |
23 |
Correct |
3 ms |
10576 KB |
Output is correct |
24 |
Correct |
3 ms |
10576 KB |
Output is correct |
25 |
Correct |
3 ms |
10752 KB |
Output is correct |
26 |
Correct |
2 ms |
10576 KB |
Output is correct |
27 |
Correct |
3 ms |
10576 KB |
Output is correct |
28 |
Correct |
2 ms |
10576 KB |
Output is correct |
29 |
Correct |
3 ms |
10576 KB |
Output is correct |
30 |
Correct |
3 ms |
10576 KB |
Output is correct |
31 |
Correct |
3 ms |
10576 KB |
Output is correct |
32 |
Correct |
3 ms |
10576 KB |
Output is correct |
33 |
Correct |
2 ms |
10576 KB |
Output is correct |
34 |
Correct |
2 ms |
10576 KB |
Output is correct |
35 |
Correct |
3 ms |
10744 KB |
Output is correct |
36 |
Correct |
2 ms |
10576 KB |
Output is correct |
37 |
Correct |
3 ms |
10576 KB |
Output is correct |
38 |
Correct |
3 ms |
10576 KB |
Output is correct |
39 |
Correct |
3 ms |
10576 KB |
Output is correct |
40 |
Correct |
2 ms |
10716 KB |
Output is correct |
41 |
Correct |
3 ms |
10576 KB |
Output is correct |
42 |
Correct |
2 ms |
10576 KB |
Output is correct |
43 |
Correct |
3 ms |
10576 KB |
Output is correct |
44 |
Correct |
3 ms |
10576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
10576 KB |
Output is correct |
3 |
Correct |
2 ms |
10576 KB |
Output is correct |
4 |
Correct |
3 ms |
10576 KB |
Output is correct |
5 |
Correct |
2 ms |
10576 KB |
Output is correct |
6 |
Correct |
2 ms |
10576 KB |
Output is correct |
7 |
Correct |
3 ms |
10576 KB |
Output is correct |
8 |
Correct |
3 ms |
10576 KB |
Output is correct |
9 |
Correct |
2 ms |
10576 KB |
Output is correct |
10 |
Correct |
2 ms |
10576 KB |
Output is correct |
11 |
Correct |
3 ms |
10576 KB |
Output is correct |
12 |
Correct |
3 ms |
10576 KB |
Output is correct |
13 |
Correct |
2 ms |
10576 KB |
Output is correct |
14 |
Correct |
2 ms |
10576 KB |
Output is correct |
15 |
Correct |
4 ms |
10576 KB |
Output is correct |
16 |
Correct |
3 ms |
10576 KB |
Output is correct |
17 |
Correct |
3 ms |
10576 KB |
Output is correct |
18 |
Correct |
3 ms |
10576 KB |
Output is correct |
19 |
Correct |
2 ms |
10576 KB |
Output is correct |
20 |
Correct |
3 ms |
10576 KB |
Output is correct |
21 |
Correct |
3 ms |
10576 KB |
Output is correct |
22 |
Correct |
2 ms |
10716 KB |
Output is correct |
23 |
Correct |
3 ms |
10576 KB |
Output is correct |
24 |
Correct |
3 ms |
10576 KB |
Output is correct |
25 |
Correct |
3 ms |
10752 KB |
Output is correct |
26 |
Correct |
2 ms |
10576 KB |
Output is correct |
27 |
Correct |
3 ms |
10576 KB |
Output is correct |
28 |
Correct |
2 ms |
10576 KB |
Output is correct |
29 |
Correct |
3 ms |
10576 KB |
Output is correct |
30 |
Correct |
3 ms |
10576 KB |
Output is correct |
31 |
Correct |
3 ms |
10576 KB |
Output is correct |
32 |
Correct |
3 ms |
10576 KB |
Output is correct |
33 |
Correct |
2 ms |
10576 KB |
Output is correct |
34 |
Correct |
2 ms |
10576 KB |
Output is correct |
35 |
Correct |
3 ms |
10744 KB |
Output is correct |
36 |
Correct |
2 ms |
10576 KB |
Output is correct |
37 |
Correct |
3 ms |
10576 KB |
Output is correct |
38 |
Correct |
3 ms |
10576 KB |
Output is correct |
39 |
Correct |
3 ms |
10576 KB |
Output is correct |
40 |
Correct |
2 ms |
10716 KB |
Output is correct |
41 |
Correct |
3 ms |
10576 KB |
Output is correct |
42 |
Correct |
2 ms |
10576 KB |
Output is correct |
43 |
Correct |
3 ms |
10576 KB |
Output is correct |
44 |
Correct |
3 ms |
10576 KB |
Output is correct |
45 |
Correct |
4 ms |
10576 KB |
Output is correct |
46 |
Correct |
10 ms |
13136 KB |
Output is correct |
47 |
Correct |
10 ms |
13136 KB |
Output is correct |
48 |
Correct |
9 ms |
13136 KB |
Output is correct |
49 |
Correct |
7 ms |
13392 KB |
Output is correct |
50 |
Correct |
5 ms |
13392 KB |
Output is correct |
51 |
Correct |
5 ms |
13392 KB |
Output is correct |
52 |
Correct |
9 ms |
13136 KB |
Output is correct |
53 |
Correct |
7 ms |
13136 KB |
Output is correct |
54 |
Correct |
7 ms |
13136 KB |
Output is correct |
55 |
Correct |
7 ms |
13136 KB |
Output is correct |
56 |
Correct |
8 ms |
13296 KB |
Output is correct |
57 |
Correct |
15 ms |
12880 KB |
Output is correct |
58 |
Correct |
16 ms |
13136 KB |
Output is correct |
59 |
Correct |
17 ms |
13136 KB |
Output is correct |
60 |
Correct |
17 ms |
13136 KB |
Output is correct |
61 |
Correct |
9 ms |
13136 KB |
Output is correct |
62 |
Correct |
8 ms |
13136 KB |
Output is correct |
63 |
Correct |
9 ms |
13136 KB |
Output is correct |
64 |
Correct |
10 ms |
12976 KB |
Output is correct |
65 |
Correct |
10 ms |
13136 KB |
Output is correct |
66 |
Correct |
10 ms |
13136 KB |
Output is correct |
67 |
Correct |
10 ms |
13148 KB |
Output is correct |
68 |
Correct |
5 ms |
13392 KB |
Output is correct |
69 |
Correct |
7 ms |
13140 KB |
Output is correct |
70 |
Correct |
7 ms |
13136 KB |
Output is correct |
71 |
Correct |
7 ms |
13136 KB |
Output is correct |
72 |
Correct |
20 ms |
13136 KB |
Output is correct |
73 |
Correct |
15 ms |
12880 KB |
Output is correct |
74 |
Correct |
9 ms |
13136 KB |
Output is correct |
75 |
Correct |
8 ms |
13136 KB |
Output is correct |
76 |
Correct |
7 ms |
13136 KB |
Output is correct |
77 |
Correct |
6 ms |
13136 KB |
Output is correct |
78 |
Correct |
7 ms |
13136 KB |
Output is correct |
79 |
Correct |
6 ms |
13136 KB |
Output is correct |
80 |
Correct |
7 ms |
13136 KB |
Output is correct |
81 |
Correct |
9 ms |
13316 KB |
Output is correct |
82 |
Correct |
10 ms |
13136 KB |
Output is correct |
83 |
Correct |
10 ms |
13136 KB |
Output is correct |
84 |
Correct |
9 ms |
13136 KB |
Output is correct |
85 |
Correct |
9 ms |
13160 KB |
Output is correct |
86 |
Correct |
9 ms |
13136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
10576 KB |
Output is correct |
3 |
Correct |
2 ms |
10576 KB |
Output is correct |
4 |
Correct |
3 ms |
10576 KB |
Output is correct |
5 |
Correct |
2 ms |
10576 KB |
Output is correct |
6 |
Correct |
2 ms |
10576 KB |
Output is correct |
7 |
Correct |
3 ms |
10576 KB |
Output is correct |
8 |
Correct |
3 ms |
10576 KB |
Output is correct |
9 |
Correct |
2 ms |
10576 KB |
Output is correct |
10 |
Correct |
2 ms |
10576 KB |
Output is correct |
11 |
Correct |
3 ms |
10576 KB |
Output is correct |
12 |
Correct |
3 ms |
10576 KB |
Output is correct |
13 |
Correct |
2 ms |
10576 KB |
Output is correct |
14 |
Correct |
2 ms |
10576 KB |
Output is correct |
15 |
Correct |
4 ms |
10576 KB |
Output is correct |
16 |
Correct |
3 ms |
10576 KB |
Output is correct |
17 |
Correct |
3 ms |
10576 KB |
Output is correct |
18 |
Correct |
3 ms |
10576 KB |
Output is correct |
19 |
Correct |
2 ms |
10576 KB |
Output is correct |
20 |
Correct |
3 ms |
10576 KB |
Output is correct |
21 |
Correct |
3 ms |
10576 KB |
Output is correct |
22 |
Correct |
2 ms |
10716 KB |
Output is correct |
23 |
Correct |
3 ms |
10576 KB |
Output is correct |
24 |
Correct |
3 ms |
10576 KB |
Output is correct |
25 |
Correct |
3 ms |
10752 KB |
Output is correct |
26 |
Correct |
2 ms |
10576 KB |
Output is correct |
27 |
Correct |
3 ms |
10576 KB |
Output is correct |
28 |
Correct |
2 ms |
10576 KB |
Output is correct |
29 |
Correct |
3 ms |
10576 KB |
Output is correct |
30 |
Correct |
3 ms |
10576 KB |
Output is correct |
31 |
Correct |
3 ms |
10576 KB |
Output is correct |
32 |
Correct |
3 ms |
10576 KB |
Output is correct |
33 |
Correct |
2 ms |
10576 KB |
Output is correct |
34 |
Correct |
2 ms |
10576 KB |
Output is correct |
35 |
Correct |
3 ms |
10744 KB |
Output is correct |
36 |
Correct |
2 ms |
10576 KB |
Output is correct |
37 |
Correct |
3 ms |
10576 KB |
Output is correct |
38 |
Correct |
3 ms |
10576 KB |
Output is correct |
39 |
Correct |
3 ms |
10576 KB |
Output is correct |
40 |
Correct |
2 ms |
10716 KB |
Output is correct |
41 |
Correct |
3 ms |
10576 KB |
Output is correct |
42 |
Correct |
2 ms |
10576 KB |
Output is correct |
43 |
Correct |
3 ms |
10576 KB |
Output is correct |
44 |
Correct |
3 ms |
10576 KB |
Output is correct |
45 |
Correct |
4 ms |
10576 KB |
Output is correct |
46 |
Correct |
10 ms |
13136 KB |
Output is correct |
47 |
Correct |
10 ms |
13136 KB |
Output is correct |
48 |
Correct |
9 ms |
13136 KB |
Output is correct |
49 |
Correct |
7 ms |
13392 KB |
Output is correct |
50 |
Correct |
5 ms |
13392 KB |
Output is correct |
51 |
Correct |
5 ms |
13392 KB |
Output is correct |
52 |
Correct |
9 ms |
13136 KB |
Output is correct |
53 |
Correct |
7 ms |
13136 KB |
Output is correct |
54 |
Correct |
7 ms |
13136 KB |
Output is correct |
55 |
Correct |
7 ms |
13136 KB |
Output is correct |
56 |
Correct |
8 ms |
13296 KB |
Output is correct |
57 |
Correct |
15 ms |
12880 KB |
Output is correct |
58 |
Correct |
16 ms |
13136 KB |
Output is correct |
59 |
Correct |
17 ms |
13136 KB |
Output is correct |
60 |
Correct |
17 ms |
13136 KB |
Output is correct |
61 |
Correct |
9 ms |
13136 KB |
Output is correct |
62 |
Correct |
8 ms |
13136 KB |
Output is correct |
63 |
Correct |
9 ms |
13136 KB |
Output is correct |
64 |
Correct |
10 ms |
12976 KB |
Output is correct |
65 |
Correct |
10 ms |
13136 KB |
Output is correct |
66 |
Correct |
10 ms |
13136 KB |
Output is correct |
67 |
Correct |
10 ms |
13148 KB |
Output is correct |
68 |
Correct |
5 ms |
13392 KB |
Output is correct |
69 |
Correct |
7 ms |
13140 KB |
Output is correct |
70 |
Correct |
7 ms |
13136 KB |
Output is correct |
71 |
Correct |
7 ms |
13136 KB |
Output is correct |
72 |
Correct |
20 ms |
13136 KB |
Output is correct |
73 |
Correct |
15 ms |
12880 KB |
Output is correct |
74 |
Correct |
9 ms |
13136 KB |
Output is correct |
75 |
Correct |
8 ms |
13136 KB |
Output is correct |
76 |
Correct |
7 ms |
13136 KB |
Output is correct |
77 |
Correct |
6 ms |
13136 KB |
Output is correct |
78 |
Correct |
7 ms |
13136 KB |
Output is correct |
79 |
Correct |
6 ms |
13136 KB |
Output is correct |
80 |
Correct |
7 ms |
13136 KB |
Output is correct |
81 |
Correct |
9 ms |
13316 KB |
Output is correct |
82 |
Correct |
10 ms |
13136 KB |
Output is correct |
83 |
Correct |
10 ms |
13136 KB |
Output is correct |
84 |
Correct |
9 ms |
13136 KB |
Output is correct |
85 |
Correct |
9 ms |
13160 KB |
Output is correct |
86 |
Correct |
9 ms |
13136 KB |
Output is correct |
87 |
Correct |
25 ms |
13904 KB |
Output is correct |
88 |
Correct |
89 ms |
17436 KB |
Output is correct |
89 |
Correct |
384 ms |
32964 KB |
Output is correct |
90 |
Correct |
371 ms |
32964 KB |
Output is correct |
91 |
Correct |
408 ms |
32964 KB |
Output is correct |
92 |
Correct |
111 ms |
41952 KB |
Output is correct |
93 |
Correct |
99 ms |
41924 KB |
Output is correct |
94 |
Correct |
106 ms |
41924 KB |
Output is correct |
95 |
Correct |
198 ms |
37572 KB |
Output is correct |
96 |
Correct |
183 ms |
38088 KB |
Output is correct |
97 |
Correct |
181 ms |
38084 KB |
Output is correct |
98 |
Correct |
202 ms |
38116 KB |
Output is correct |
99 |
Correct |
207 ms |
36804 KB |
Output is correct |
100 |
Correct |
779 ms |
32832 KB |
Output is correct |
101 |
Correct |
827 ms |
33132 KB |
Output is correct |
102 |
Correct |
790 ms |
33252 KB |
Output is correct |
103 |
Correct |
785 ms |
33216 KB |
Output is correct |
104 |
Correct |
304 ms |
36548 KB |
Output is correct |
105 |
Correct |
447 ms |
36804 KB |
Output is correct |
106 |
Correct |
378 ms |
36544 KB |
Output is correct |
107 |
Correct |
393 ms |
32196 KB |
Output is correct |
108 |
Correct |
400 ms |
32344 KB |
Output is correct |
109 |
Correct |
430 ms |
32456 KB |
Output is correct |
110 |
Correct |
102 ms |
41300 KB |
Output is correct |
111 |
Correct |
175 ms |
37572 KB |
Output is correct |
112 |
Correct |
170 ms |
37316 KB |
Output is correct |
113 |
Correct |
171 ms |
35932 KB |
Output is correct |
114 |
Correct |
784 ms |
32964 KB |
Output is correct |
115 |
Correct |
767 ms |
32524 KB |
Output is correct |
116 |
Correct |
293 ms |
36136 KB |
Output is correct |
117 |
Correct |
185 ms |
34248 KB |
Output is correct |
118 |
Correct |
179 ms |
33732 KB |
Output is correct |
119 |
Correct |
197 ms |
33440 KB |
Output is correct |
120 |
Correct |
180 ms |
33884 KB |
Output is correct |
121 |
Correct |
165 ms |
33364 KB |
Output is correct |
122 |
Correct |
185 ms |
32964 KB |
Output is correct |
123 |
Correct |
363 ms |
34500 KB |
Output is correct |
124 |
Correct |
346 ms |
33668 KB |
Output is correct |
125 |
Correct |
391 ms |
33500 KB |
Output is correct |
126 |
Correct |
371 ms |
33988 KB |
Output is correct |
127 |
Correct |
357 ms |
33320 KB |
Output is correct |
128 |
Correct |
424 ms |
33220 KB |
Output is correct |