#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")
#define pii pair<int,int>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
#define f first
#define s second
#define pii pair<int,int>
template<class T>bool umax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<class T>bool umin(T &a,T b){if(b<a){a=b;return true;}return false;}
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
const int mod= 1e9 +7;
const int N=1e5*4;
int binpow (int a, int n) {
if (n == 0)
return 1;
if (n % 2 == 1)
return binpow (a, n-1) * a;
else {
int b = binpow (a, n/2);
return b * b;
}
}
vector<int>vis(N+1),vis1(N+1);
vector<int>v[N];
pii t[N*4];
void build(int tl,int tr,int v){
if(tl==tr){
t[v].f = vis[tl];
t[v].s = vis[tl];
return;
}
int tm = (tl+tr)/2;
build(tl,tm,v*2);
build(tm+1,tr,v*2+1);
t[v].f = max(t[v*2].f,t[v*2+1].f);
t[v].s = min(t[v*2].s,t[v*2+1].s);
}
pii get(int l,int r,int v,int tl,int tr){
if(tl>r||tr<l)return {-1,mod};
if(l<=tl&&tr<=r){
return t[v];
}
int tm = (tl + tr)/2;
pii x,y;
x = get(l,r,v*2,tl,tm);
y = get(l,r,v*2+1,tm+1,tr);
pii val;
val.f = max(x.f,y.f);
val.s = min(x.s,y.s);
return val;
}
void solve(){
int n,q,m,k;
cin>>n>>m>>q;
for(int i = 1;i<n;i++){
int a,b;
cin>>a>>b;
v[a].pb(b);
v[b].pb(a);
}
for(int i = 1;i<=m;i++){
int a;
cin>>a;
vis[i] = a;
vis1[a] = i;
}
if(n<=2000){
while (q--)
{
int l , r;
cin>>l>>r;
/*for (int i = 0; i < n + 1; i++){
dist[i] = mod;
}*/
vector<int>dist(n+1);
queue<pii>q;
q.push({vis[l],-1});
dist[vis[l]] = 1;
vector<int>pred(n+1);
while(!q.empty()){
auto [x,pr] = q.front();
q.pop();
for(auto to:v[x]){
if(to!=pr){
dist[to] = dist[x] + 1;
pred[to] = x;
q.push({to,x});
}
}
}
vector<int>g(n+1);
for(int i = l + 1;i<=r;i++){
int y = vis[i];
while(y!=vis[l]){
g[y] = 1;
y = pred[y];
}
}
int cnt = 0;
for(int i = 1;i<=n;i++)cnt+=g[i];
cout<<cnt + 1<<"\n";
}
}
else{
build(1,n,1);
while(q--){
int l,r;
cin>>l>>r;
pii x = get(l,r,1,1,n);
cout<<x.f - x.s + 1<<"\n";
}
}
}
signed main()
{
// freopen("seq.in", "r", stdin);
// freopen("seq.out", "w", stdout);
ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int tt=1;//cin>>tt;
while(tt--)solve();
}
Compilation message
tourism.cpp: In function 'void solve()':
tourism.cpp:67:12: warning: unused variable 'k' [-Wunused-variable]
67 | int n,q,m,k;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16224 KB |
Output is correct |
2 |
Correct |
5 ms |
16228 KB |
Output is correct |
3 |
Correct |
5 ms |
16232 KB |
Output is correct |
4 |
Correct |
6 ms |
16488 KB |
Output is correct |
5 |
Correct |
5 ms |
16396 KB |
Output is correct |
6 |
Correct |
5 ms |
16208 KB |
Output is correct |
7 |
Correct |
5 ms |
16388 KB |
Output is correct |
8 |
Correct |
5 ms |
16220 KB |
Output is correct |
9 |
Correct |
6 ms |
16484 KB |
Output is correct |
10 |
Correct |
6 ms |
16232 KB |
Output is correct |
11 |
Correct |
6 ms |
16232 KB |
Output is correct |
12 |
Correct |
7 ms |
16412 KB |
Output is correct |
13 |
Correct |
6 ms |
16224 KB |
Output is correct |
14 |
Correct |
7 ms |
16392 KB |
Output is correct |
15 |
Correct |
11 ms |
16216 KB |
Output is correct |
16 |
Correct |
10 ms |
16220 KB |
Output is correct |
17 |
Correct |
10 ms |
16408 KB |
Output is correct |
18 |
Correct |
8 ms |
16220 KB |
Output is correct |
19 |
Correct |
9 ms |
16392 KB |
Output is correct |
20 |
Correct |
9 ms |
16224 KB |
Output is correct |
21 |
Correct |
5 ms |
16220 KB |
Output is correct |
22 |
Correct |
6 ms |
16424 KB |
Output is correct |
23 |
Correct |
5 ms |
16220 KB |
Output is correct |
24 |
Correct |
5 ms |
16220 KB |
Output is correct |
25 |
Correct |
5 ms |
16220 KB |
Output is correct |
26 |
Correct |
5 ms |
16220 KB |
Output is correct |
27 |
Correct |
4 ms |
16224 KB |
Output is correct |
28 |
Correct |
5 ms |
16224 KB |
Output is correct |
29 |
Correct |
6 ms |
16220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16224 KB |
Output is correct |
2 |
Correct |
5 ms |
16228 KB |
Output is correct |
3 |
Correct |
5 ms |
16232 KB |
Output is correct |
4 |
Correct |
6 ms |
16488 KB |
Output is correct |
5 |
Correct |
5 ms |
16396 KB |
Output is correct |
6 |
Correct |
5 ms |
16208 KB |
Output is correct |
7 |
Correct |
5 ms |
16388 KB |
Output is correct |
8 |
Correct |
5 ms |
16220 KB |
Output is correct |
9 |
Correct |
6 ms |
16484 KB |
Output is correct |
10 |
Correct |
6 ms |
16232 KB |
Output is correct |
11 |
Correct |
6 ms |
16232 KB |
Output is correct |
12 |
Correct |
7 ms |
16412 KB |
Output is correct |
13 |
Correct |
6 ms |
16224 KB |
Output is correct |
14 |
Correct |
7 ms |
16392 KB |
Output is correct |
15 |
Correct |
11 ms |
16216 KB |
Output is correct |
16 |
Correct |
10 ms |
16220 KB |
Output is correct |
17 |
Correct |
10 ms |
16408 KB |
Output is correct |
18 |
Correct |
8 ms |
16220 KB |
Output is correct |
19 |
Correct |
9 ms |
16392 KB |
Output is correct |
20 |
Correct |
9 ms |
16224 KB |
Output is correct |
21 |
Correct |
5 ms |
16220 KB |
Output is correct |
22 |
Correct |
6 ms |
16424 KB |
Output is correct |
23 |
Correct |
5 ms |
16220 KB |
Output is correct |
24 |
Correct |
5 ms |
16220 KB |
Output is correct |
25 |
Correct |
5 ms |
16220 KB |
Output is correct |
26 |
Correct |
5 ms |
16220 KB |
Output is correct |
27 |
Correct |
4 ms |
16224 KB |
Output is correct |
28 |
Correct |
5 ms |
16224 KB |
Output is correct |
29 |
Correct |
6 ms |
16220 KB |
Output is correct |
30 |
Correct |
48 ms |
16520 KB |
Output is correct |
31 |
Correct |
56 ms |
16480 KB |
Output is correct |
32 |
Correct |
91 ms |
16556 KB |
Output is correct |
33 |
Correct |
89 ms |
16480 KB |
Output is correct |
34 |
Correct |
84 ms |
16536 KB |
Output is correct |
35 |
Correct |
151 ms |
16476 KB |
Output is correct |
36 |
Correct |
163 ms |
16564 KB |
Output is correct |
37 |
Correct |
160 ms |
16540 KB |
Output is correct |
38 |
Correct |
1439 ms |
16732 KB |
Output is correct |
39 |
Correct |
1423 ms |
16724 KB |
Output is correct |
40 |
Correct |
1425 ms |
16524 KB |
Output is correct |
41 |
Correct |
4297 ms |
16724 KB |
Output is correct |
42 |
Correct |
3927 ms |
16520 KB |
Output is correct |
43 |
Correct |
4353 ms |
16980 KB |
Output is correct |
44 |
Correct |
666 ms |
16516 KB |
Output is correct |
45 |
Correct |
555 ms |
16520 KB |
Output is correct |
46 |
Correct |
654 ms |
16724 KB |
Output is correct |
47 |
Correct |
2017 ms |
16540 KB |
Output is correct |
48 |
Correct |
1368 ms |
16532 KB |
Output is correct |
49 |
Correct |
2050 ms |
16784 KB |
Output is correct |
50 |
Correct |
33 ms |
16476 KB |
Output is correct |
51 |
Correct |
34 ms |
16560 KB |
Output is correct |
52 |
Correct |
33 ms |
16472 KB |
Output is correct |
53 |
Correct |
33 ms |
16476 KB |
Output is correct |
54 |
Correct |
33 ms |
16472 KB |
Output is correct |
55 |
Correct |
34 ms |
16568 KB |
Output is correct |
56 |
Correct |
6 ms |
16220 KB |
Output is correct |
57 |
Correct |
31 ms |
16472 KB |
Output is correct |
58 |
Correct |
49 ms |
16472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
16220 KB |
Output is correct |
2 |
Correct |
4 ms |
16132 KB |
Output is correct |
3 |
Correct |
6 ms |
16220 KB |
Output is correct |
4 |
Incorrect |
48 ms |
22280 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
16472 KB |
Output is correct |
2 |
Incorrect |
22 ms |
20676 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16216 KB |
Output is correct |
2 |
Correct |
5 ms |
16216 KB |
Output is correct |
3 |
Correct |
6 ms |
16216 KB |
Output is correct |
4 |
Incorrect |
56 ms |
23144 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16224 KB |
Output is correct |
2 |
Correct |
5 ms |
16228 KB |
Output is correct |
3 |
Correct |
5 ms |
16232 KB |
Output is correct |
4 |
Correct |
6 ms |
16488 KB |
Output is correct |
5 |
Correct |
5 ms |
16396 KB |
Output is correct |
6 |
Correct |
5 ms |
16208 KB |
Output is correct |
7 |
Correct |
5 ms |
16388 KB |
Output is correct |
8 |
Correct |
5 ms |
16220 KB |
Output is correct |
9 |
Correct |
6 ms |
16484 KB |
Output is correct |
10 |
Correct |
6 ms |
16232 KB |
Output is correct |
11 |
Correct |
6 ms |
16232 KB |
Output is correct |
12 |
Correct |
7 ms |
16412 KB |
Output is correct |
13 |
Correct |
6 ms |
16224 KB |
Output is correct |
14 |
Correct |
7 ms |
16392 KB |
Output is correct |
15 |
Correct |
11 ms |
16216 KB |
Output is correct |
16 |
Correct |
10 ms |
16220 KB |
Output is correct |
17 |
Correct |
10 ms |
16408 KB |
Output is correct |
18 |
Correct |
8 ms |
16220 KB |
Output is correct |
19 |
Correct |
9 ms |
16392 KB |
Output is correct |
20 |
Correct |
9 ms |
16224 KB |
Output is correct |
21 |
Correct |
5 ms |
16220 KB |
Output is correct |
22 |
Correct |
6 ms |
16424 KB |
Output is correct |
23 |
Correct |
5 ms |
16220 KB |
Output is correct |
24 |
Correct |
5 ms |
16220 KB |
Output is correct |
25 |
Correct |
5 ms |
16220 KB |
Output is correct |
26 |
Correct |
5 ms |
16220 KB |
Output is correct |
27 |
Correct |
4 ms |
16224 KB |
Output is correct |
28 |
Correct |
5 ms |
16224 KB |
Output is correct |
29 |
Correct |
6 ms |
16220 KB |
Output is correct |
30 |
Correct |
48 ms |
16520 KB |
Output is correct |
31 |
Correct |
56 ms |
16480 KB |
Output is correct |
32 |
Correct |
91 ms |
16556 KB |
Output is correct |
33 |
Correct |
89 ms |
16480 KB |
Output is correct |
34 |
Correct |
84 ms |
16536 KB |
Output is correct |
35 |
Correct |
151 ms |
16476 KB |
Output is correct |
36 |
Correct |
163 ms |
16564 KB |
Output is correct |
37 |
Correct |
160 ms |
16540 KB |
Output is correct |
38 |
Correct |
1439 ms |
16732 KB |
Output is correct |
39 |
Correct |
1423 ms |
16724 KB |
Output is correct |
40 |
Correct |
1425 ms |
16524 KB |
Output is correct |
41 |
Correct |
4297 ms |
16724 KB |
Output is correct |
42 |
Correct |
3927 ms |
16520 KB |
Output is correct |
43 |
Correct |
4353 ms |
16980 KB |
Output is correct |
44 |
Correct |
666 ms |
16516 KB |
Output is correct |
45 |
Correct |
555 ms |
16520 KB |
Output is correct |
46 |
Correct |
654 ms |
16724 KB |
Output is correct |
47 |
Correct |
2017 ms |
16540 KB |
Output is correct |
48 |
Correct |
1368 ms |
16532 KB |
Output is correct |
49 |
Correct |
2050 ms |
16784 KB |
Output is correct |
50 |
Correct |
33 ms |
16476 KB |
Output is correct |
51 |
Correct |
34 ms |
16560 KB |
Output is correct |
52 |
Correct |
33 ms |
16472 KB |
Output is correct |
53 |
Correct |
33 ms |
16476 KB |
Output is correct |
54 |
Correct |
33 ms |
16472 KB |
Output is correct |
55 |
Correct |
34 ms |
16568 KB |
Output is correct |
56 |
Correct |
6 ms |
16220 KB |
Output is correct |
57 |
Correct |
31 ms |
16472 KB |
Output is correct |
58 |
Correct |
49 ms |
16472 KB |
Output is correct |
59 |
Correct |
5 ms |
16220 KB |
Output is correct |
60 |
Correct |
4 ms |
16132 KB |
Output is correct |
61 |
Correct |
6 ms |
16220 KB |
Output is correct |
62 |
Incorrect |
48 ms |
22280 KB |
Output isn't correct |
63 |
Halted |
0 ms |
0 KB |
- |