#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int mod = 998244353;
const int N = 1e5;
vector<int> g[N+1];
int depth[N+1], c[N+1];
int timer=1;
int tin[N+1], tout[N+1];
int up[N+1][18];
int n, m, q;
void dfs(int v, int par){
up[v][0] = par;
tin[v] = timer++;
for(int i = 1;i < 18; i++){
up[v][i] = up[up[v][i-1]][i-1];
}
for(int to : g[v]){
if(to == par) continue;
depth[to] = depth[v] + 1;
dfs(to, v);
}
tout[v] = timer++;
}
int lca(int a, int b){
if(depth[a] > depth[b]) swap(a, b);
int k = depth[b] - depth[a];
for(int i = 0;i < 18; i++){
if(k & (1<<i)){
b = up[b][i];
}
}
if(a == b) return a;
for(int i = 17; i >= 0; i--){
if(up[a][i] != up[b][i]){
a = up[a][i];
b = up[b][i];
}
}
return up[a][0];
}
int vert;
vector<int> vec;
int in_subtree(int v){
int it = lower_bound(all(vec), tin[v]) - vec.begin();
if(it < (int)vec.size() && vec[it] <= tout[v]) return 1;
return 0;
}
void ans(int v, int par){
if(!in_subtree(v)) return;
vert++;
for(int to : g[v]){
if(to == par) continue;
ans(to, v);
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m >> q;
for(int i = 1;i < n; i++){
int a, b; cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i = 0;i < m; i++){
cin >> c[i];
}
dfs(1, 1);
while(q--){
int l, r; cin >> l >> r;
l--, r--;
int mn = c[l], mx = c[l];
vec.clear();
for(int i = l;i <= r; i++){
vec.push_back(tin[c[i]]);
if(tin[mn] > tin[c[i]]){
mn = c[i];
}
if(tin[mx] < tin[c[i]]){
mx = c[i];
}
}
sort(all(vec));
vec.erase(unique(all(vec)), vec.end());
vert = 0;
int lc = lca(mn, mx);
ans(lc, up[lc][0]);
cout << vert << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
4956 KB |
Output is correct |
7 |
Correct |
3 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
5 ms |
4956 KB |
Output is correct |
10 |
Correct |
5 ms |
4956 KB |
Output is correct |
11 |
Correct |
5 ms |
5044 KB |
Output is correct |
12 |
Correct |
7 ms |
4956 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
7 ms |
4956 KB |
Output is correct |
15 |
Correct |
4 ms |
4956 KB |
Output is correct |
16 |
Correct |
4 ms |
4964 KB |
Output is correct |
17 |
Correct |
4 ms |
4956 KB |
Output is correct |
18 |
Correct |
4 ms |
5124 KB |
Output is correct |
19 |
Correct |
4 ms |
4956 KB |
Output is correct |
20 |
Correct |
5 ms |
5120 KB |
Output is correct |
21 |
Correct |
4 ms |
4952 KB |
Output is correct |
22 |
Correct |
4 ms |
4956 KB |
Output is correct |
23 |
Correct |
5 ms |
4956 KB |
Output is correct |
24 |
Correct |
5 ms |
4956 KB |
Output is correct |
25 |
Correct |
5 ms |
4956 KB |
Output is correct |
26 |
Correct |
4 ms |
4956 KB |
Output is correct |
27 |
Correct |
2 ms |
4956 KB |
Output is correct |
28 |
Correct |
1 ms |
4956 KB |
Output is correct |
29 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
4956 KB |
Output is correct |
7 |
Correct |
3 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
5 ms |
4956 KB |
Output is correct |
10 |
Correct |
5 ms |
4956 KB |
Output is correct |
11 |
Correct |
5 ms |
5044 KB |
Output is correct |
12 |
Correct |
7 ms |
4956 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
7 ms |
4956 KB |
Output is correct |
15 |
Correct |
4 ms |
4956 KB |
Output is correct |
16 |
Correct |
4 ms |
4964 KB |
Output is correct |
17 |
Correct |
4 ms |
4956 KB |
Output is correct |
18 |
Correct |
4 ms |
5124 KB |
Output is correct |
19 |
Correct |
4 ms |
4956 KB |
Output is correct |
20 |
Correct |
5 ms |
5120 KB |
Output is correct |
21 |
Correct |
4 ms |
4952 KB |
Output is correct |
22 |
Correct |
4 ms |
4956 KB |
Output is correct |
23 |
Correct |
5 ms |
4956 KB |
Output is correct |
24 |
Correct |
5 ms |
4956 KB |
Output is correct |
25 |
Correct |
5 ms |
4956 KB |
Output is correct |
26 |
Correct |
4 ms |
4956 KB |
Output is correct |
27 |
Correct |
2 ms |
4956 KB |
Output is correct |
28 |
Correct |
1 ms |
4956 KB |
Output is correct |
29 |
Correct |
2 ms |
4956 KB |
Output is correct |
30 |
Correct |
89 ms |
5212 KB |
Output is correct |
31 |
Correct |
129 ms |
5212 KB |
Output is correct |
32 |
Correct |
186 ms |
5288 KB |
Output is correct |
33 |
Correct |
194 ms |
5296 KB |
Output is correct |
34 |
Correct |
199 ms |
5288 KB |
Output is correct |
35 |
Correct |
376 ms |
5220 KB |
Output is correct |
36 |
Correct |
371 ms |
5292 KB |
Output is correct |
37 |
Correct |
384 ms |
5224 KB |
Output is correct |
38 |
Correct |
181 ms |
5380 KB |
Output is correct |
39 |
Correct |
171 ms |
5208 KB |
Output is correct |
40 |
Correct |
176 ms |
5208 KB |
Output is correct |
41 |
Correct |
353 ms |
5392 KB |
Output is correct |
42 |
Correct |
357 ms |
5392 KB |
Output is correct |
43 |
Correct |
349 ms |
5360 KB |
Output is correct |
44 |
Correct |
173 ms |
5208 KB |
Output is correct |
45 |
Correct |
172 ms |
5312 KB |
Output is correct |
46 |
Correct |
175 ms |
5332 KB |
Output is correct |
47 |
Correct |
346 ms |
5320 KB |
Output is correct |
48 |
Correct |
347 ms |
5304 KB |
Output is correct |
49 |
Correct |
346 ms |
5208 KB |
Output is correct |
50 |
Correct |
180 ms |
5296 KB |
Output is correct |
51 |
Correct |
185 ms |
5300 KB |
Output is correct |
52 |
Correct |
180 ms |
5300 KB |
Output is correct |
53 |
Correct |
181 ms |
5296 KB |
Output is correct |
54 |
Correct |
182 ms |
5300 KB |
Output is correct |
55 |
Correct |
183 ms |
5212 KB |
Output is correct |
56 |
Correct |
16 ms |
5104 KB |
Output is correct |
57 |
Correct |
3 ms |
5296 KB |
Output is correct |
58 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5208 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
14 ms |
4956 KB |
Output is correct |
4 |
Execution timed out |
5086 ms |
15152 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
51 ms |
11100 KB |
Output is correct |
3 |
Correct |
81 ms |
11856 KB |
Output is correct |
4 |
Correct |
65 ms |
14360 KB |
Output is correct |
5 |
Correct |
55 ms |
16076 KB |
Output is correct |
6 |
Correct |
59 ms |
15952 KB |
Output is correct |
7 |
Correct |
67 ms |
15696 KB |
Output is correct |
8 |
Correct |
83 ms |
15452 KB |
Output is correct |
9 |
Correct |
95 ms |
15676 KB |
Output is correct |
10 |
Correct |
101 ms |
15440 KB |
Output is correct |
11 |
Correct |
110 ms |
15668 KB |
Output is correct |
12 |
Correct |
105 ms |
15672 KB |
Output is correct |
13 |
Correct |
112 ms |
15916 KB |
Output is correct |
14 |
Correct |
138 ms |
16064 KB |
Output is correct |
15 |
Correct |
64 ms |
16428 KB |
Output is correct |
16 |
Correct |
86 ms |
16208 KB |
Output is correct |
17 |
Correct |
95 ms |
16212 KB |
Output is correct |
18 |
Correct |
99 ms |
16496 KB |
Output is correct |
19 |
Correct |
47 ms |
16220 KB |
Output is correct |
20 |
Correct |
50 ms |
15952 KB |
Output is correct |
21 |
Correct |
57 ms |
15700 KB |
Output is correct |
22 |
Correct |
66 ms |
15696 KB |
Output is correct |
23 |
Correct |
88 ms |
15960 KB |
Output is correct |
24 |
Correct |
124 ms |
15708 KB |
Output is correct |
25 |
Correct |
168 ms |
15444 KB |
Output is correct |
26 |
Correct |
242 ms |
15700 KB |
Output is correct |
27 |
Correct |
344 ms |
15444 KB |
Output is correct |
28 |
Correct |
421 ms |
15696 KB |
Output is correct |
29 |
Correct |
517 ms |
15888 KB |
Output is correct |
30 |
Correct |
674 ms |
15704 KB |
Output is correct |
31 |
Correct |
747 ms |
16116 KB |
Output is correct |
32 |
Correct |
832 ms |
16264 KB |
Output is correct |
33 |
Correct |
646 ms |
16140 KB |
Output is correct |
34 |
Correct |
64 ms |
16492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
14 ms |
4956 KB |
Output is correct |
4 |
Execution timed out |
5044 ms |
12036 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
4956 KB |
Output is correct |
7 |
Correct |
3 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
5 ms |
4956 KB |
Output is correct |
10 |
Correct |
5 ms |
4956 KB |
Output is correct |
11 |
Correct |
5 ms |
5044 KB |
Output is correct |
12 |
Correct |
7 ms |
4956 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
7 ms |
4956 KB |
Output is correct |
15 |
Correct |
4 ms |
4956 KB |
Output is correct |
16 |
Correct |
4 ms |
4964 KB |
Output is correct |
17 |
Correct |
4 ms |
4956 KB |
Output is correct |
18 |
Correct |
4 ms |
5124 KB |
Output is correct |
19 |
Correct |
4 ms |
4956 KB |
Output is correct |
20 |
Correct |
5 ms |
5120 KB |
Output is correct |
21 |
Correct |
4 ms |
4952 KB |
Output is correct |
22 |
Correct |
4 ms |
4956 KB |
Output is correct |
23 |
Correct |
5 ms |
4956 KB |
Output is correct |
24 |
Correct |
5 ms |
4956 KB |
Output is correct |
25 |
Correct |
5 ms |
4956 KB |
Output is correct |
26 |
Correct |
4 ms |
4956 KB |
Output is correct |
27 |
Correct |
2 ms |
4956 KB |
Output is correct |
28 |
Correct |
1 ms |
4956 KB |
Output is correct |
29 |
Correct |
2 ms |
4956 KB |
Output is correct |
30 |
Correct |
89 ms |
5212 KB |
Output is correct |
31 |
Correct |
129 ms |
5212 KB |
Output is correct |
32 |
Correct |
186 ms |
5288 KB |
Output is correct |
33 |
Correct |
194 ms |
5296 KB |
Output is correct |
34 |
Correct |
199 ms |
5288 KB |
Output is correct |
35 |
Correct |
376 ms |
5220 KB |
Output is correct |
36 |
Correct |
371 ms |
5292 KB |
Output is correct |
37 |
Correct |
384 ms |
5224 KB |
Output is correct |
38 |
Correct |
181 ms |
5380 KB |
Output is correct |
39 |
Correct |
171 ms |
5208 KB |
Output is correct |
40 |
Correct |
176 ms |
5208 KB |
Output is correct |
41 |
Correct |
353 ms |
5392 KB |
Output is correct |
42 |
Correct |
357 ms |
5392 KB |
Output is correct |
43 |
Correct |
349 ms |
5360 KB |
Output is correct |
44 |
Correct |
173 ms |
5208 KB |
Output is correct |
45 |
Correct |
172 ms |
5312 KB |
Output is correct |
46 |
Correct |
175 ms |
5332 KB |
Output is correct |
47 |
Correct |
346 ms |
5320 KB |
Output is correct |
48 |
Correct |
347 ms |
5304 KB |
Output is correct |
49 |
Correct |
346 ms |
5208 KB |
Output is correct |
50 |
Correct |
180 ms |
5296 KB |
Output is correct |
51 |
Correct |
185 ms |
5300 KB |
Output is correct |
52 |
Correct |
180 ms |
5300 KB |
Output is correct |
53 |
Correct |
181 ms |
5296 KB |
Output is correct |
54 |
Correct |
182 ms |
5300 KB |
Output is correct |
55 |
Correct |
183 ms |
5212 KB |
Output is correct |
56 |
Correct |
16 ms |
5104 KB |
Output is correct |
57 |
Correct |
3 ms |
5296 KB |
Output is correct |
58 |
Correct |
2 ms |
5212 KB |
Output is correct |
59 |
Correct |
1 ms |
5208 KB |
Output is correct |
60 |
Correct |
1 ms |
4956 KB |
Output is correct |
61 |
Correct |
14 ms |
4956 KB |
Output is correct |
62 |
Execution timed out |
5086 ms |
15152 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |