/**
_ _ __ _ _ _ _ _ _
|a ||t ||o d | |o |
| __ _| | _ | __| _ |
| __ |/_ | __ /__\ / _\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 100000;
const int M_MAX = 100000;
const int Q_MAX = 100000;
const int LOG_N = 18;
int N, M, Q;
vector <int> adj[N_MAX + 2];
int C[M_MAX + 2];
int ql[Q_MAX + 2], qr[Q_MAX + 2];
int parent[N_MAX + 2];
int depth[N_MAX + 2];
int order[N_MAX + 2];
int L[N_MAX + 2], R[N_MAX + 2];
int curr;
int euler[N_MAX * 2 + 2];
int ecurr;
void dfs (int u) {
euler[++ecurr] = u;
L[u] = ++curr;
order[curr] = u;
for (int v : adj[u]) {
if (v != parent[u]) {
parent[v] = u;
depth[v] = depth[u] + 1;
dfs(v);
euler[++ecurr] = u;
}
}
R[u] = curr;
}
int range_lca[N_MAX * 2 + 2][LOG_N];
int arg_min_depth (int u, int v) {
return (depth[u] < depth[v] ? u : v);
}
int euler_L[N_MAX + 2], euler_R[N_MAX + 2];
void precalc () {
for (int i = 1; i <= N * 2; i++) {
range_lca[i][0] = euler[i];
if (euler_L[euler[i]] == 0) {
euler_L[euler[i]] = i;
}
euler_R[euler[i]] = i;
}
for (int e = 1; e < LOG_N; e++) {
for (int i = 1; i + (1 << e) - 1 <= N * 2; i++) {
range_lca[i][e] = arg_min_depth(range_lca[i][e - 1],
range_lca[i + (1 << (e - 1))][e - 1]);
}
}
}
int lca (int u, int v) {
if (u == 0 || v == 0) {
return 0;
}
if (L[u] > L[v]) {
swap(u, v);
}
int l = euler_L[u], r = euler_R[v];
int len = r - l + 1;
int e = 31 - __builtin_clz(len);
return arg_min_depth(range_lca[l][e], range_lca[r - (1 << e) + 1][e]);
}
int bucket;
int get_bucket (int i) {
return (i - 1) / bucket + 1;
}
vector <int> queries[M_MAX + 2];
int cnt, root;
int Fen[N_MAX + 2];
void update (int pos, int sgn) {
for (int i = pos; i <= N; i += i & -i) {
Fen[i] += sgn;
}
}
int query (int pos) {
int cnt = 0;
for (int i = pos; i >= 1; i -= i & -i) {
cnt += Fen[i];
}
return cnt;
}
int get_kth (int k) {
int i = 0;
for (int e = LOG_N - 1; e >= 0; e--) {
if (i + (1 << e) <= N && Fen[i + (1 << e)] < k) {
i += (1 << e);
k -= Fen[i];
}
}
return i + 1;
}
int occ[N_MAX + 2];
int total = 0;
void add (int u) {
if (++occ[u] == 1) {
update(L[u], +1); total++;
if (total == 1) {
cnt = 1; root = u;
return;
}
int upos = query(L[u]);
int ul = (upos > 1 ? order[get_kth(upos - 1)] : 0);
int ur = (upos < total ? order[get_kth(upos + 1)] : 0);
if (ur == 0 || R[u] < L[ur]) {
ul = lca(u, ul);
ur = lca(u, ur);
int v = (depth[ul] > depth[ur] ? ul : ur);
cnt += depth[u] - depth[v];
if (depth[v] < depth[root]) {
cnt += depth[root] - depth[v];
root = v;
}
} else {
int v = lca(u, root);
cnt += depth[root] - depth[v];
root = v;
}
}
}
void del (int u) {
if (--occ[u] == 0) {
update(L[u], -1); total--;
}
}
int answer[Q_MAX + 2];
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M >> Q;
for (int i = 1; i <= N - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= M; i++) {
cin >> C[i];
}
for (int i = 1; i <= Q; i++) {
cin >> ql[i] >> qr[i];
}
depth[1] = 1;
dfs(1);
precalc();
bucket = max(1, min(M, (int) sqrt(M)));
for (int i = 1; i <= Q; i++) {
int bl = get_bucket(ql[i]), br = get_bucket(qr[i]);
if (bl < br) {
queries[bl].push_back(i);
} else {
for (int j = ql[i]; j <= qr[i]; j++) {
add(C[j]);
}
answer[i] = cnt;
for (int j = qr[i]; j >= ql[i]; j--) {
del(C[j]);
}
cnt = root = 0;
}
}
for (int b = 1; b <= get_bucket(M) - 1; b++) {
sort(queries[b].begin(), queries[b].end(), [&] (const int &i, const int &j) {
return qr[i] < qr[j];
});
int r = b * bucket;
for (int i : queries[b]) {
while (r < qr[i]) {
add(C[++r]);
}
int prv_cnt = cnt, prv_root = root;
for (int j = b * bucket; j >= ql[i]; j--) {
add(C[j]);
}
answer[i] = cnt;
for (int j = ql[i]; j <= b * bucket; j++) {
del(C[j]);
}
cnt = prv_cnt; root = prv_root;
}
while (r > b * bucket) {
del(C[r--]);
}
cnt = root = 0;
}
for (int i = 1; i <= Q; i++) {
cout << answer[i] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12460 KB |
Output is correct |
7 |
Correct |
3 ms |
12888 KB |
Output is correct |
8 |
Correct |
3 ms |
12660 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
4 ms |
12636 KB |
Output is correct |
11 |
Correct |
3 ms |
12636 KB |
Output is correct |
12 |
Correct |
3 ms |
12636 KB |
Output is correct |
13 |
Correct |
3 ms |
12636 KB |
Output is correct |
14 |
Correct |
3 ms |
12632 KB |
Output is correct |
15 |
Correct |
3 ms |
12636 KB |
Output is correct |
16 |
Correct |
3 ms |
12636 KB |
Output is correct |
17 |
Correct |
3 ms |
12636 KB |
Output is correct |
18 |
Correct |
3 ms |
12636 KB |
Output is correct |
19 |
Correct |
3 ms |
12636 KB |
Output is correct |
20 |
Correct |
3 ms |
12636 KB |
Output is correct |
21 |
Correct |
4 ms |
12636 KB |
Output is correct |
22 |
Correct |
3 ms |
12636 KB |
Output is correct |
23 |
Correct |
4 ms |
12636 KB |
Output is correct |
24 |
Correct |
4 ms |
12636 KB |
Output is correct |
25 |
Correct |
3 ms |
12636 KB |
Output is correct |
26 |
Correct |
4 ms |
12636 KB |
Output is correct |
27 |
Correct |
3 ms |
12636 KB |
Output is correct |
28 |
Correct |
2 ms |
12636 KB |
Output is correct |
29 |
Correct |
3 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12460 KB |
Output is correct |
7 |
Correct |
3 ms |
12888 KB |
Output is correct |
8 |
Correct |
3 ms |
12660 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
4 ms |
12636 KB |
Output is correct |
11 |
Correct |
3 ms |
12636 KB |
Output is correct |
12 |
Correct |
3 ms |
12636 KB |
Output is correct |
13 |
Correct |
3 ms |
12636 KB |
Output is correct |
14 |
Correct |
3 ms |
12632 KB |
Output is correct |
15 |
Correct |
3 ms |
12636 KB |
Output is correct |
16 |
Correct |
3 ms |
12636 KB |
Output is correct |
17 |
Correct |
3 ms |
12636 KB |
Output is correct |
18 |
Correct |
3 ms |
12636 KB |
Output is correct |
19 |
Correct |
3 ms |
12636 KB |
Output is correct |
20 |
Correct |
3 ms |
12636 KB |
Output is correct |
21 |
Correct |
4 ms |
12636 KB |
Output is correct |
22 |
Correct |
3 ms |
12636 KB |
Output is correct |
23 |
Correct |
4 ms |
12636 KB |
Output is correct |
24 |
Correct |
4 ms |
12636 KB |
Output is correct |
25 |
Correct |
3 ms |
12636 KB |
Output is correct |
26 |
Correct |
4 ms |
12636 KB |
Output is correct |
27 |
Correct |
3 ms |
12636 KB |
Output is correct |
28 |
Correct |
2 ms |
12636 KB |
Output is correct |
29 |
Correct |
3 ms |
12636 KB |
Output is correct |
30 |
Correct |
10 ms |
12636 KB |
Output is correct |
31 |
Correct |
11 ms |
12672 KB |
Output is correct |
32 |
Correct |
14 ms |
12636 KB |
Output is correct |
33 |
Correct |
14 ms |
12636 KB |
Output is correct |
34 |
Correct |
14 ms |
12636 KB |
Output is correct |
35 |
Correct |
6 ms |
12636 KB |
Output is correct |
36 |
Correct |
6 ms |
12636 KB |
Output is correct |
37 |
Correct |
7 ms |
12632 KB |
Output is correct |
38 |
Correct |
14 ms |
13048 KB |
Output is correct |
39 |
Correct |
13 ms |
12832 KB |
Output is correct |
40 |
Correct |
13 ms |
12632 KB |
Output is correct |
41 |
Correct |
6 ms |
12636 KB |
Output is correct |
42 |
Correct |
5 ms |
12636 KB |
Output is correct |
43 |
Correct |
6 ms |
12724 KB |
Output is correct |
44 |
Correct |
15 ms |
12632 KB |
Output is correct |
45 |
Correct |
14 ms |
12632 KB |
Output is correct |
46 |
Correct |
13 ms |
12636 KB |
Output is correct |
47 |
Correct |
7 ms |
12888 KB |
Output is correct |
48 |
Correct |
6 ms |
12636 KB |
Output is correct |
49 |
Correct |
7 ms |
12776 KB |
Output is correct |
50 |
Correct |
14 ms |
12636 KB |
Output is correct |
51 |
Correct |
13 ms |
12636 KB |
Output is correct |
52 |
Correct |
13 ms |
12632 KB |
Output is correct |
53 |
Correct |
14 ms |
12636 KB |
Output is correct |
54 |
Correct |
14 ms |
12748 KB |
Output is correct |
55 |
Correct |
14 ms |
12636 KB |
Output is correct |
56 |
Correct |
4 ms |
12636 KB |
Output is correct |
57 |
Correct |
3 ms |
12636 KB |
Output is correct |
58 |
Correct |
3 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12604 KB |
Output is correct |
4 |
Correct |
4021 ms |
25144 KB |
Output is correct |
5 |
Correct |
3072 ms |
33572 KB |
Output is correct |
6 |
Correct |
4408 ms |
35404 KB |
Output is correct |
7 |
Execution timed out |
5034 ms |
37852 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
39 ms |
20800 KB |
Output is correct |
3 |
Correct |
52 ms |
23888 KB |
Output is correct |
4 |
Correct |
63 ms |
24040 KB |
Output is correct |
5 |
Correct |
71 ms |
29008 KB |
Output is correct |
6 |
Correct |
77 ms |
29192 KB |
Output is correct |
7 |
Correct |
95 ms |
29184 KB |
Output is correct |
8 |
Correct |
92 ms |
29172 KB |
Output is correct |
9 |
Correct |
84 ms |
29008 KB |
Output is correct |
10 |
Correct |
103 ms |
29196 KB |
Output is correct |
11 |
Correct |
95 ms |
29176 KB |
Output is correct |
12 |
Correct |
82 ms |
29060 KB |
Output is correct |
13 |
Correct |
94 ms |
29432 KB |
Output is correct |
14 |
Correct |
105 ms |
29412 KB |
Output is correct |
15 |
Correct |
104 ms |
30180 KB |
Output is correct |
16 |
Correct |
98 ms |
29420 KB |
Output is correct |
17 |
Correct |
106 ms |
29444 KB |
Output is correct |
18 |
Correct |
102 ms |
29668 KB |
Output is correct |
19 |
Correct |
97 ms |
29012 KB |
Output is correct |
20 |
Correct |
88 ms |
28988 KB |
Output is correct |
21 |
Correct |
98 ms |
29112 KB |
Output is correct |
22 |
Correct |
102 ms |
28984 KB |
Output is correct |
23 |
Correct |
102 ms |
29192 KB |
Output is correct |
24 |
Correct |
91 ms |
29008 KB |
Output is correct |
25 |
Correct |
108 ms |
29172 KB |
Output is correct |
26 |
Correct |
99 ms |
29024 KB |
Output is correct |
27 |
Correct |
112 ms |
29376 KB |
Output is correct |
28 |
Correct |
87 ms |
29028 KB |
Output is correct |
29 |
Correct |
100 ms |
29208 KB |
Output is correct |
30 |
Correct |
91 ms |
29220 KB |
Output is correct |
31 |
Correct |
101 ms |
29476 KB |
Output is correct |
32 |
Correct |
102 ms |
29604 KB |
Output is correct |
33 |
Correct |
105 ms |
29588 KB |
Output is correct |
34 |
Correct |
81 ms |
30240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12376 KB |
Output is correct |
2 |
Correct |
3 ms |
12624 KB |
Output is correct |
3 |
Correct |
4 ms |
12636 KB |
Output is correct |
4 |
Correct |
4845 ms |
22148 KB |
Output is correct |
5 |
Correct |
4753 ms |
26416 KB |
Output is correct |
6 |
Execution timed out |
5037 ms |
29064 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12460 KB |
Output is correct |
7 |
Correct |
3 ms |
12888 KB |
Output is correct |
8 |
Correct |
3 ms |
12660 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
4 ms |
12636 KB |
Output is correct |
11 |
Correct |
3 ms |
12636 KB |
Output is correct |
12 |
Correct |
3 ms |
12636 KB |
Output is correct |
13 |
Correct |
3 ms |
12636 KB |
Output is correct |
14 |
Correct |
3 ms |
12632 KB |
Output is correct |
15 |
Correct |
3 ms |
12636 KB |
Output is correct |
16 |
Correct |
3 ms |
12636 KB |
Output is correct |
17 |
Correct |
3 ms |
12636 KB |
Output is correct |
18 |
Correct |
3 ms |
12636 KB |
Output is correct |
19 |
Correct |
3 ms |
12636 KB |
Output is correct |
20 |
Correct |
3 ms |
12636 KB |
Output is correct |
21 |
Correct |
4 ms |
12636 KB |
Output is correct |
22 |
Correct |
3 ms |
12636 KB |
Output is correct |
23 |
Correct |
4 ms |
12636 KB |
Output is correct |
24 |
Correct |
4 ms |
12636 KB |
Output is correct |
25 |
Correct |
3 ms |
12636 KB |
Output is correct |
26 |
Correct |
4 ms |
12636 KB |
Output is correct |
27 |
Correct |
3 ms |
12636 KB |
Output is correct |
28 |
Correct |
2 ms |
12636 KB |
Output is correct |
29 |
Correct |
3 ms |
12636 KB |
Output is correct |
30 |
Correct |
10 ms |
12636 KB |
Output is correct |
31 |
Correct |
11 ms |
12672 KB |
Output is correct |
32 |
Correct |
14 ms |
12636 KB |
Output is correct |
33 |
Correct |
14 ms |
12636 KB |
Output is correct |
34 |
Correct |
14 ms |
12636 KB |
Output is correct |
35 |
Correct |
6 ms |
12636 KB |
Output is correct |
36 |
Correct |
6 ms |
12636 KB |
Output is correct |
37 |
Correct |
7 ms |
12632 KB |
Output is correct |
38 |
Correct |
14 ms |
13048 KB |
Output is correct |
39 |
Correct |
13 ms |
12832 KB |
Output is correct |
40 |
Correct |
13 ms |
12632 KB |
Output is correct |
41 |
Correct |
6 ms |
12636 KB |
Output is correct |
42 |
Correct |
5 ms |
12636 KB |
Output is correct |
43 |
Correct |
6 ms |
12724 KB |
Output is correct |
44 |
Correct |
15 ms |
12632 KB |
Output is correct |
45 |
Correct |
14 ms |
12632 KB |
Output is correct |
46 |
Correct |
13 ms |
12636 KB |
Output is correct |
47 |
Correct |
7 ms |
12888 KB |
Output is correct |
48 |
Correct |
6 ms |
12636 KB |
Output is correct |
49 |
Correct |
7 ms |
12776 KB |
Output is correct |
50 |
Correct |
14 ms |
12636 KB |
Output is correct |
51 |
Correct |
13 ms |
12636 KB |
Output is correct |
52 |
Correct |
13 ms |
12632 KB |
Output is correct |
53 |
Correct |
14 ms |
12636 KB |
Output is correct |
54 |
Correct |
14 ms |
12748 KB |
Output is correct |
55 |
Correct |
14 ms |
12636 KB |
Output is correct |
56 |
Correct |
4 ms |
12636 KB |
Output is correct |
57 |
Correct |
3 ms |
12636 KB |
Output is correct |
58 |
Correct |
3 ms |
12636 KB |
Output is correct |
59 |
Correct |
2 ms |
12636 KB |
Output is correct |
60 |
Correct |
2 ms |
12636 KB |
Output is correct |
61 |
Correct |
3 ms |
12604 KB |
Output is correct |
62 |
Correct |
4021 ms |
25144 KB |
Output is correct |
63 |
Correct |
3072 ms |
33572 KB |
Output is correct |
64 |
Correct |
4408 ms |
35404 KB |
Output is correct |
65 |
Execution timed out |
5034 ms |
37852 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |