#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("Ofast,unroll-loops")
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define pb push_back
const int N = 1e5 + 1,INF = 1e18;
vector<int> c1(N),c2(N);
vector<int> g[N];
pair<int,int> t[N * 4];
void build(int tl,int tr,int v){
if(tl == tr){
t[v].first = c1[tl];
t[v].second = c1[tl];
return;
}
int tm = (tl + tr) >> 1;
build(tl,tm,v * 2);
build(tm + 1,tr,v * 2 + 1);
t[v].first = max(t[v * 2].first,t[v * 2 + 1].first);
t[v].second = min(t[v * 2].second,t[v * 2 + 1].second);
}
pair<int,int> get(int l,int r,int v,int tl,int tr){
if(tl > r || tr < l){
return {-INF,INF};
}
if(l <= tl && tr <= r){
return t[v];
}
int tm = (tl + tr) >> 1;
pair<int,int> x,y;
x = get(l, r, v * 2,tl,tm);
y = get(l,r,v * 2 + 1,tm + 1,tr);
pair<int,int> res;
res.first = max(x.first,y.first);
res.second = min(x.second,y.second);
return res;
}
void solve(){
int n,q,m;
cin >> n >> m >> q;
for(int i = 0;i < n - 1;i++){
int a,b;
cin>>a>>b;
g[a].pb(b);
g[b].pb(a);
}
for(int i = 1;i <= m;i++){
int a;
cin>>a;
c1[i] = a;
c2[a] = i;
}
if(n <= 2000){
while(q--){
int l,r;
cin >> l >> r;
vector<int> dis(n + 1);
queue<pair<int,int>> q;
q.push({c1[l],-1});
dis[c1[l]] = 1;
vector<int> pr(n + 1);
while(!q.empty()){
auto [v,p] = q.front();
q.pop();
for(auto u : g[v]){
if(u != p){
dis[u] = dis[v] + 1;
pr[u] = v;
q.push({u,v});
}
}
}
vector<int> ans(n + 1);
for(int i = l + 1;i <= r;i++){
int y = c1[i];
while(y != c1[l] && !ans[y]){
ans[y] = 1;
y = pr[y];
}
}
int cnt = 0;
for(int i = 1;i <= n;i++){
cnt += ans[i];
}
cout<<cnt + 1<<"\n";
}
}
else{
build(1,m,1);
while(q--){
int l,r;
cin >> l >> r;
auto [ans1,ans2] = get(l,r,1,1,m);
cout << ans1 - ans2 + 1 << "\n";
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6236 KB |
Output is correct |
2 |
Correct |
1 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6236 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6236 KB |
Output is correct |
8 |
Correct |
2 ms |
6236 KB |
Output is correct |
9 |
Correct |
3 ms |
6236 KB |
Output is correct |
10 |
Correct |
3 ms |
6236 KB |
Output is correct |
11 |
Correct |
3 ms |
6236 KB |
Output is correct |
12 |
Correct |
3 ms |
6232 KB |
Output is correct |
13 |
Correct |
4 ms |
6400 KB |
Output is correct |
14 |
Correct |
2 ms |
6236 KB |
Output is correct |
15 |
Correct |
3 ms |
6236 KB |
Output is correct |
16 |
Correct |
3 ms |
6236 KB |
Output is correct |
17 |
Correct |
3 ms |
6236 KB |
Output is correct |
18 |
Correct |
3 ms |
6232 KB |
Output is correct |
19 |
Correct |
3 ms |
6236 KB |
Output is correct |
20 |
Correct |
4 ms |
6236 KB |
Output is correct |
21 |
Correct |
2 ms |
6232 KB |
Output is correct |
22 |
Correct |
3 ms |
6488 KB |
Output is correct |
23 |
Correct |
2 ms |
6232 KB |
Output is correct |
24 |
Correct |
2 ms |
6236 KB |
Output is correct |
25 |
Correct |
3 ms |
6236 KB |
Output is correct |
26 |
Correct |
2 ms |
6236 KB |
Output is correct |
27 |
Correct |
2 ms |
6236 KB |
Output is correct |
28 |
Correct |
2 ms |
6236 KB |
Output is correct |
29 |
Correct |
3 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6236 KB |
Output is correct |
2 |
Correct |
1 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6236 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6236 KB |
Output is correct |
8 |
Correct |
2 ms |
6236 KB |
Output is correct |
9 |
Correct |
3 ms |
6236 KB |
Output is correct |
10 |
Correct |
3 ms |
6236 KB |
Output is correct |
11 |
Correct |
3 ms |
6236 KB |
Output is correct |
12 |
Correct |
3 ms |
6232 KB |
Output is correct |
13 |
Correct |
4 ms |
6400 KB |
Output is correct |
14 |
Correct |
2 ms |
6236 KB |
Output is correct |
15 |
Correct |
3 ms |
6236 KB |
Output is correct |
16 |
Correct |
3 ms |
6236 KB |
Output is correct |
17 |
Correct |
3 ms |
6236 KB |
Output is correct |
18 |
Correct |
3 ms |
6232 KB |
Output is correct |
19 |
Correct |
3 ms |
6236 KB |
Output is correct |
20 |
Correct |
4 ms |
6236 KB |
Output is correct |
21 |
Correct |
2 ms |
6232 KB |
Output is correct |
22 |
Correct |
3 ms |
6488 KB |
Output is correct |
23 |
Correct |
2 ms |
6232 KB |
Output is correct |
24 |
Correct |
2 ms |
6236 KB |
Output is correct |
25 |
Correct |
3 ms |
6236 KB |
Output is correct |
26 |
Correct |
2 ms |
6236 KB |
Output is correct |
27 |
Correct |
2 ms |
6236 KB |
Output is correct |
28 |
Correct |
2 ms |
6236 KB |
Output is correct |
29 |
Correct |
3 ms |
6236 KB |
Output is correct |
30 |
Correct |
32 ms |
6236 KB |
Output is correct |
31 |
Correct |
42 ms |
6236 KB |
Output is correct |
32 |
Correct |
71 ms |
6748 KB |
Output is correct |
33 |
Correct |
69 ms |
6492 KB |
Output is correct |
34 |
Correct |
66 ms |
6492 KB |
Output is correct |
35 |
Correct |
70 ms |
6492 KB |
Output is correct |
36 |
Correct |
70 ms |
6512 KB |
Output is correct |
37 |
Correct |
70 ms |
6492 KB |
Output is correct |
38 |
Correct |
55 ms |
6488 KB |
Output is correct |
39 |
Correct |
51 ms |
6488 KB |
Output is correct |
40 |
Correct |
51 ms |
6492 KB |
Output is correct |
41 |
Correct |
55 ms |
6492 KB |
Output is correct |
42 |
Correct |
65 ms |
6492 KB |
Output is correct |
43 |
Correct |
58 ms |
6488 KB |
Output is correct |
44 |
Correct |
44 ms |
6488 KB |
Output is correct |
45 |
Correct |
69 ms |
6236 KB |
Output is correct |
46 |
Correct |
60 ms |
6488 KB |
Output is correct |
47 |
Correct |
43 ms |
6488 KB |
Output is correct |
48 |
Correct |
43 ms |
6488 KB |
Output is correct |
49 |
Correct |
44 ms |
6496 KB |
Output is correct |
50 |
Correct |
33 ms |
6492 KB |
Output is correct |
51 |
Correct |
33 ms |
6492 KB |
Output is correct |
52 |
Correct |
33 ms |
6492 KB |
Output is correct |
53 |
Correct |
33 ms |
6532 KB |
Output is correct |
54 |
Correct |
34 ms |
6520 KB |
Output is correct |
55 |
Correct |
35 ms |
6536 KB |
Output is correct |
56 |
Correct |
4 ms |
6388 KB |
Output is correct |
57 |
Correct |
31 ms |
6492 KB |
Output is correct |
58 |
Correct |
54 ms |
6508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6236 KB |
Output is correct |
2 |
Correct |
2 ms |
6320 KB |
Output is correct |
3 |
Correct |
3 ms |
6236 KB |
Output is correct |
4 |
Correct |
50 ms |
12676 KB |
Output is correct |
5 |
Correct |
48 ms |
11344 KB |
Output is correct |
6 |
Correct |
57 ms |
13648 KB |
Output is correct |
7 |
Correct |
79 ms |
14376 KB |
Output is correct |
8 |
Correct |
79 ms |
14156 KB |
Output is correct |
9 |
Correct |
69 ms |
14236 KB |
Output is correct |
10 |
Correct |
68 ms |
14376 KB |
Output is correct |
11 |
Correct |
90 ms |
14364 KB |
Output is correct |
12 |
Correct |
61 ms |
14152 KB |
Output is correct |
13 |
Correct |
60 ms |
14160 KB |
Output is correct |
14 |
Correct |
60 ms |
14264 KB |
Output is correct |
15 |
Correct |
32 ms |
9472 KB |
Output is correct |
16 |
Correct |
57 ms |
13916 KB |
Output is correct |
17 |
Correct |
2184 ms |
6532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6232 KB |
Output is correct |
2 |
Incorrect |
18 ms |
10588 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6232 KB |
Output is correct |
2 |
Correct |
2 ms |
6236 KB |
Output is correct |
3 |
Correct |
3 ms |
6236 KB |
Output is correct |
4 |
Incorrect |
58 ms |
13380 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6236 KB |
Output is correct |
2 |
Correct |
1 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6236 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6236 KB |
Output is correct |
8 |
Correct |
2 ms |
6236 KB |
Output is correct |
9 |
Correct |
3 ms |
6236 KB |
Output is correct |
10 |
Correct |
3 ms |
6236 KB |
Output is correct |
11 |
Correct |
3 ms |
6236 KB |
Output is correct |
12 |
Correct |
3 ms |
6232 KB |
Output is correct |
13 |
Correct |
4 ms |
6400 KB |
Output is correct |
14 |
Correct |
2 ms |
6236 KB |
Output is correct |
15 |
Correct |
3 ms |
6236 KB |
Output is correct |
16 |
Correct |
3 ms |
6236 KB |
Output is correct |
17 |
Correct |
3 ms |
6236 KB |
Output is correct |
18 |
Correct |
3 ms |
6232 KB |
Output is correct |
19 |
Correct |
3 ms |
6236 KB |
Output is correct |
20 |
Correct |
4 ms |
6236 KB |
Output is correct |
21 |
Correct |
2 ms |
6232 KB |
Output is correct |
22 |
Correct |
3 ms |
6488 KB |
Output is correct |
23 |
Correct |
2 ms |
6232 KB |
Output is correct |
24 |
Correct |
2 ms |
6236 KB |
Output is correct |
25 |
Correct |
3 ms |
6236 KB |
Output is correct |
26 |
Correct |
2 ms |
6236 KB |
Output is correct |
27 |
Correct |
2 ms |
6236 KB |
Output is correct |
28 |
Correct |
2 ms |
6236 KB |
Output is correct |
29 |
Correct |
3 ms |
6236 KB |
Output is correct |
30 |
Correct |
32 ms |
6236 KB |
Output is correct |
31 |
Correct |
42 ms |
6236 KB |
Output is correct |
32 |
Correct |
71 ms |
6748 KB |
Output is correct |
33 |
Correct |
69 ms |
6492 KB |
Output is correct |
34 |
Correct |
66 ms |
6492 KB |
Output is correct |
35 |
Correct |
70 ms |
6492 KB |
Output is correct |
36 |
Correct |
70 ms |
6512 KB |
Output is correct |
37 |
Correct |
70 ms |
6492 KB |
Output is correct |
38 |
Correct |
55 ms |
6488 KB |
Output is correct |
39 |
Correct |
51 ms |
6488 KB |
Output is correct |
40 |
Correct |
51 ms |
6492 KB |
Output is correct |
41 |
Correct |
55 ms |
6492 KB |
Output is correct |
42 |
Correct |
65 ms |
6492 KB |
Output is correct |
43 |
Correct |
58 ms |
6488 KB |
Output is correct |
44 |
Correct |
44 ms |
6488 KB |
Output is correct |
45 |
Correct |
69 ms |
6236 KB |
Output is correct |
46 |
Correct |
60 ms |
6488 KB |
Output is correct |
47 |
Correct |
43 ms |
6488 KB |
Output is correct |
48 |
Correct |
43 ms |
6488 KB |
Output is correct |
49 |
Correct |
44 ms |
6496 KB |
Output is correct |
50 |
Correct |
33 ms |
6492 KB |
Output is correct |
51 |
Correct |
33 ms |
6492 KB |
Output is correct |
52 |
Correct |
33 ms |
6492 KB |
Output is correct |
53 |
Correct |
33 ms |
6532 KB |
Output is correct |
54 |
Correct |
34 ms |
6520 KB |
Output is correct |
55 |
Correct |
35 ms |
6536 KB |
Output is correct |
56 |
Correct |
4 ms |
6388 KB |
Output is correct |
57 |
Correct |
31 ms |
6492 KB |
Output is correct |
58 |
Correct |
54 ms |
6508 KB |
Output is correct |
59 |
Correct |
2 ms |
6236 KB |
Output is correct |
60 |
Correct |
2 ms |
6320 KB |
Output is correct |
61 |
Correct |
3 ms |
6236 KB |
Output is correct |
62 |
Correct |
50 ms |
12676 KB |
Output is correct |
63 |
Correct |
48 ms |
11344 KB |
Output is correct |
64 |
Correct |
57 ms |
13648 KB |
Output is correct |
65 |
Correct |
79 ms |
14376 KB |
Output is correct |
66 |
Correct |
79 ms |
14156 KB |
Output is correct |
67 |
Correct |
69 ms |
14236 KB |
Output is correct |
68 |
Correct |
68 ms |
14376 KB |
Output is correct |
69 |
Correct |
90 ms |
14364 KB |
Output is correct |
70 |
Correct |
61 ms |
14152 KB |
Output is correct |
71 |
Correct |
60 ms |
14160 KB |
Output is correct |
72 |
Correct |
60 ms |
14264 KB |
Output is correct |
73 |
Correct |
32 ms |
9472 KB |
Output is correct |
74 |
Correct |
57 ms |
13916 KB |
Output is correct |
75 |
Correct |
2184 ms |
6532 KB |
Output is correct |
76 |
Correct |
1 ms |
6232 KB |
Output is correct |
77 |
Incorrect |
18 ms |
10588 KB |
Output isn't correct |
78 |
Halted |
0 ms |
0 KB |
- |