#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] = 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 |
1 ms |
6236 KB |
Output is correct |
2 |
Correct |
1 ms |
6236 KB |
Output is correct |
3 |
Correct |
1 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 |
6380 KB |
Output is correct |
11 |
Correct |
3 ms |
6236 KB |
Output is correct |
12 |
Correct |
4 ms |
6236 KB |
Output is correct |
13 |
Correct |
4 ms |
6236 KB |
Output is correct |
14 |
Correct |
4 ms |
6236 KB |
Output is correct |
15 |
Correct |
7 ms |
6236 KB |
Output is correct |
16 |
Correct |
7 ms |
6400 KB |
Output is correct |
17 |
Correct |
8 ms |
6396 KB |
Output is correct |
18 |
Correct |
5 ms |
6236 KB |
Output is correct |
19 |
Correct |
6 ms |
6400 KB |
Output is correct |
20 |
Correct |
6 ms |
6232 KB |
Output is correct |
21 |
Correct |
2 ms |
6236 KB |
Output is correct |
22 |
Correct |
2 ms |
6236 KB |
Output is correct |
23 |
Correct |
2 ms |
6236 KB |
Output is correct |
24 |
Correct |
2 ms |
6236 KB |
Output is correct |
25 |
Correct |
3 ms |
6236 KB |
Output is correct |
26 |
Correct |
3 ms |
6232 KB |
Output is correct |
27 |
Correct |
2 ms |
6232 KB |
Output is correct |
28 |
Correct |
2 ms |
6392 KB |
Output is correct |
29 |
Correct |
2 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6236 KB |
Output is correct |
2 |
Correct |
1 ms |
6236 KB |
Output is correct |
3 |
Correct |
1 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 |
6380 KB |
Output is correct |
11 |
Correct |
3 ms |
6236 KB |
Output is correct |
12 |
Correct |
4 ms |
6236 KB |
Output is correct |
13 |
Correct |
4 ms |
6236 KB |
Output is correct |
14 |
Correct |
4 ms |
6236 KB |
Output is correct |
15 |
Correct |
7 ms |
6236 KB |
Output is correct |
16 |
Correct |
7 ms |
6400 KB |
Output is correct |
17 |
Correct |
8 ms |
6396 KB |
Output is correct |
18 |
Correct |
5 ms |
6236 KB |
Output is correct |
19 |
Correct |
6 ms |
6400 KB |
Output is correct |
20 |
Correct |
6 ms |
6232 KB |
Output is correct |
21 |
Correct |
2 ms |
6236 KB |
Output is correct |
22 |
Correct |
2 ms |
6236 KB |
Output is correct |
23 |
Correct |
2 ms |
6236 KB |
Output is correct |
24 |
Correct |
2 ms |
6236 KB |
Output is correct |
25 |
Correct |
3 ms |
6236 KB |
Output is correct |
26 |
Correct |
3 ms |
6232 KB |
Output is correct |
27 |
Correct |
2 ms |
6232 KB |
Output is correct |
28 |
Correct |
2 ms |
6392 KB |
Output is correct |
29 |
Correct |
2 ms |
6236 KB |
Output is correct |
30 |
Correct |
41 ms |
6236 KB |
Output is correct |
31 |
Correct |
54 ms |
6232 KB |
Output is correct |
32 |
Correct |
84 ms |
6488 KB |
Output is correct |
33 |
Correct |
87 ms |
6492 KB |
Output is correct |
34 |
Correct |
83 ms |
6492 KB |
Output is correct |
35 |
Correct |
160 ms |
6492 KB |
Output is correct |
36 |
Correct |
157 ms |
6492 KB |
Output is correct |
37 |
Correct |
159 ms |
6488 KB |
Output is correct |
38 |
Correct |
1412 ms |
6720 KB |
Output is correct |
39 |
Correct |
1429 ms |
6732 KB |
Output is correct |
40 |
Correct |
1411 ms |
6492 KB |
Output is correct |
41 |
Correct |
4367 ms |
6724 KB |
Output is correct |
42 |
Correct |
3952 ms |
6488 KB |
Output is correct |
43 |
Execution timed out |
5010 ms |
6492 KB |
Time limit exceeded |
44 |
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 |
Correct |
50 ms |
12936 KB |
Output is correct |
5 |
Correct |
48 ms |
12368 KB |
Output is correct |
6 |
Correct |
50 ms |
14672 KB |
Output is correct |
7 |
Correct |
113 ms |
15444 KB |
Output is correct |
8 |
Correct |
68 ms |
15444 KB |
Output is correct |
9 |
Correct |
78 ms |
15444 KB |
Output is correct |
10 |
Correct |
75 ms |
15648 KB |
Output is correct |
11 |
Correct |
79 ms |
15476 KB |
Output is correct |
12 |
Correct |
61 ms |
15632 KB |
Output is correct |
13 |
Correct |
63 ms |
15616 KB |
Output is correct |
14 |
Correct |
61 ms |
15552 KB |
Output is correct |
15 |
Correct |
41 ms |
10580 KB |
Output is correct |
16 |
Correct |
58 ms |
15088 KB |
Output is correct |
17 |
Correct |
2234 ms |
7300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6236 KB |
Output is correct |
2 |
Incorrect |
21 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 |
59 ms |
13296 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6236 KB |
Output is correct |
2 |
Correct |
1 ms |
6236 KB |
Output is correct |
3 |
Correct |
1 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 |
6380 KB |
Output is correct |
11 |
Correct |
3 ms |
6236 KB |
Output is correct |
12 |
Correct |
4 ms |
6236 KB |
Output is correct |
13 |
Correct |
4 ms |
6236 KB |
Output is correct |
14 |
Correct |
4 ms |
6236 KB |
Output is correct |
15 |
Correct |
7 ms |
6236 KB |
Output is correct |
16 |
Correct |
7 ms |
6400 KB |
Output is correct |
17 |
Correct |
8 ms |
6396 KB |
Output is correct |
18 |
Correct |
5 ms |
6236 KB |
Output is correct |
19 |
Correct |
6 ms |
6400 KB |
Output is correct |
20 |
Correct |
6 ms |
6232 KB |
Output is correct |
21 |
Correct |
2 ms |
6236 KB |
Output is correct |
22 |
Correct |
2 ms |
6236 KB |
Output is correct |
23 |
Correct |
2 ms |
6236 KB |
Output is correct |
24 |
Correct |
2 ms |
6236 KB |
Output is correct |
25 |
Correct |
3 ms |
6236 KB |
Output is correct |
26 |
Correct |
3 ms |
6232 KB |
Output is correct |
27 |
Correct |
2 ms |
6232 KB |
Output is correct |
28 |
Correct |
2 ms |
6392 KB |
Output is correct |
29 |
Correct |
2 ms |
6236 KB |
Output is correct |
30 |
Correct |
41 ms |
6236 KB |
Output is correct |
31 |
Correct |
54 ms |
6232 KB |
Output is correct |
32 |
Correct |
84 ms |
6488 KB |
Output is correct |
33 |
Correct |
87 ms |
6492 KB |
Output is correct |
34 |
Correct |
83 ms |
6492 KB |
Output is correct |
35 |
Correct |
160 ms |
6492 KB |
Output is correct |
36 |
Correct |
157 ms |
6492 KB |
Output is correct |
37 |
Correct |
159 ms |
6488 KB |
Output is correct |
38 |
Correct |
1412 ms |
6720 KB |
Output is correct |
39 |
Correct |
1429 ms |
6732 KB |
Output is correct |
40 |
Correct |
1411 ms |
6492 KB |
Output is correct |
41 |
Correct |
4367 ms |
6724 KB |
Output is correct |
42 |
Correct |
3952 ms |
6488 KB |
Output is correct |
43 |
Execution timed out |
5010 ms |
6492 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |