#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
bool YES(bool f){ if(f) cout << "Yes\n" ; else cout << "No\n" ; return f ; }
void YES(){cout << "Yes\n" ;}
void NO(){cout << "No\n" ;}
void fopn(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
//#define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ios ios_base::sync_with_stdio(0) ; cin.tie(0) ; cout.tie(0);
#define int long long
#define ld long double
#define pii pair <int , int>
#define ff first
#define ss second
#define endl '\n'
const int N = 3e5 + 5 ;
const int inf = 1e18 ;
const int mod = 998244353 ;
const double eps = 1e-8 ;
int binpow( int a , int b ){
if ( b == 0 ) return 1 ;
int x = binpow ( a , b/2 ) ;
if ( !(b%2) ) return (x*x) ;
return (x*x*a) ;
}
template <class T>
bool chmax( T& x , const T& y ){
bool f = 0 ;
if ( x < y ) x = y , f = 1 ;
return f ;
}
template <class T>
bool chmin( T &x , const T &y ){
bool f = 0 ;
if ( x > y ) x = y , f = 1 ;
return f ;
}
//code
int n , m , q ;
int c[N] , p[N] ;
vector <int> g[N] ;
bool vis[N] ;
void dfs ( int v , int par ){
p[v] = par ;
for ( auto to : g[v] ) if ( to != par ) dfs(to,v) ;
}
struct segtree{
int n ;
vector <int> gx , gm ;
segtree( int m ){
n = m ;
gx.resize(m*4) ;
gm.resize(m*4) ;
}
void buildx( int v , int l , int r ){
if ( l == r ){
gx[v] = c[l] ;
return ;
}
int mid = ( l + r ) / 2 ;
buildx (v + v , l , mid ) ;
buildx ( v + v + 1 , mid+1 , r ) ;
gx[v] = max ( gx[v+v] , gx[v+v+1] ) ;
}
void buildm( int v , int l , int r ){
if ( l == r ){
gm[v] = c[l] ;
return ;
}
int mid = ( l + r ) / 2 ;
buildm (v + v , l , mid ) ;
buildm ( v + v + 1 , mid+1 , r ) ;
gm[v] = min ( gm[v+v] , gm[v+v+1] ) ;
}
int getx( int v , int l , int r , int tl , int tr ){
if ( l > tr || r < tl ) return 0 ;
if ( l >= tl && r <= tr ) return gx[v] ;
int mid = ( l + r ) / 2 ;
return max ( getx ( v+v , l , mid , tl , tr ) , getx ( v+v+1 , mid+1 , r , tl , tr )) ;
}
int getm( int v , int l , int r , int tl , int tr ){
if ( l > tr || r < tl ) return inf ;
if ( l >= tl && r <= tr ) return gm[v] ;
int mid = ( l + r ) / 2 ;
return min ( getm ( v+v , l , mid , tl , tr ) , getm ( v+v+1 , mid+1 , r , tl , tr )) ;
}
};
void solve(){
cin >> n >> m >> q ;
for ( int i = 1 ; i < n ; i ++ ){
int x , y ; cin >> x >> y ;
g[x].push_back(y) ;
g[y].push_back(x) ;
}
for ( int i = 1 ; i <= m ; i ++ ) cin >> c[i] ;
segtree T(m) ;
T.buildx( 1 , 1 , m ) ;
T.buildm( 1 , 1 , m ) ;
if ( max({n,m,q}) <= 2000 ){
while ( q -- ){
int l , r ;
cin >> l >> r ;
for ( int i = 1 ; i <= n ; i ++ ) vis[i] = 0 ;
dfs(c[l],0) ; vis[c[l]] = 1 ;
int ans = 0 ;
for ( int i = l+1 ; i <= r ; i ++ ){
if(vis[c[i]]) continue ;
vis[c[i]] = 1 ;
int cur = p[c[i]] ;
while ( cur != c[l] && !vis[cur] ){
vis[cur] = 1 ;
cur = p[cur] ;
}
}
for ( int i = 1 ; i <= n ; i ++ ) ans += vis[i] ;
cout<< ans << endl ;
}
return ;
}
while ( q -- ){
int x , y ; cin >> x >> y ;
int mn = T.getm( 1 , 1 , m , x , y ) ;
int mx = T.getx( 1 , 1 , m , x , y ) ;
cout << mx-mn+1 << endl ;
}
}
signed main(){
ios ;
int t = 1 ;
//cin >> t ;
while ( t -- ) solve() ;
}
Compilation message
tourism.cpp: In function 'void fopn(std::string)':
tourism.cpp:10:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | void fopn(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:10:72: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | void fopn(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
3 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
10668 KB |
Output is correct |
16 |
Correct |
3 ms |
10588 KB |
Output is correct |
17 |
Correct |
3 ms |
10760 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10584 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
2 ms |
10584 KB |
Output is correct |
24 |
Correct |
2 ms |
10588 KB |
Output is correct |
25 |
Correct |
2 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
3 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
10668 KB |
Output is correct |
16 |
Correct |
3 ms |
10588 KB |
Output is correct |
17 |
Correct |
3 ms |
10760 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10584 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
2 ms |
10584 KB |
Output is correct |
24 |
Correct |
2 ms |
10588 KB |
Output is correct |
25 |
Correct |
2 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
28 ms |
10880 KB |
Output is correct |
31 |
Correct |
36 ms |
11100 KB |
Output is correct |
32 |
Correct |
55 ms |
10840 KB |
Output is correct |
33 |
Correct |
56 ms |
10844 KB |
Output is correct |
34 |
Correct |
55 ms |
10924 KB |
Output is correct |
35 |
Correct |
54 ms |
10844 KB |
Output is correct |
36 |
Correct |
55 ms |
10908 KB |
Output is correct |
37 |
Correct |
57 ms |
10920 KB |
Output is correct |
38 |
Correct |
46 ms |
10964 KB |
Output is correct |
39 |
Correct |
45 ms |
10948 KB |
Output is correct |
40 |
Correct |
44 ms |
10840 KB |
Output is correct |
41 |
Correct |
47 ms |
10844 KB |
Output is correct |
42 |
Correct |
51 ms |
10948 KB |
Output is correct |
43 |
Correct |
50 ms |
10844 KB |
Output is correct |
44 |
Correct |
45 ms |
10840 KB |
Output is correct |
45 |
Correct |
45 ms |
10840 KB |
Output is correct |
46 |
Correct |
44 ms |
10840 KB |
Output is correct |
47 |
Correct |
46 ms |
10844 KB |
Output is correct |
48 |
Correct |
46 ms |
10840 KB |
Output is correct |
49 |
Correct |
49 ms |
11096 KB |
Output is correct |
50 |
Correct |
20 ms |
10840 KB |
Output is correct |
51 |
Correct |
20 ms |
10844 KB |
Output is correct |
52 |
Correct |
21 ms |
10932 KB |
Output is correct |
53 |
Correct |
21 ms |
10844 KB |
Output is correct |
54 |
Correct |
21 ms |
10928 KB |
Output is correct |
55 |
Correct |
21 ms |
10844 KB |
Output is correct |
56 |
Correct |
3 ms |
10840 KB |
Output is correct |
57 |
Correct |
24 ms |
10800 KB |
Output is correct |
58 |
Correct |
40 ms |
10920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
3 ms |
10844 KB |
Output is correct |
4 |
Correct |
72 ms |
18372 KB |
Output is correct |
5 |
Correct |
65 ms |
17012 KB |
Output is correct |
6 |
Correct |
63 ms |
19028 KB |
Output is correct |
7 |
Correct |
98 ms |
20560 KB |
Output is correct |
8 |
Correct |
100 ms |
20548 KB |
Output is correct |
9 |
Correct |
112 ms |
20676 KB |
Output is correct |
10 |
Correct |
98 ms |
20560 KB |
Output is correct |
11 |
Correct |
97 ms |
20564 KB |
Output is correct |
12 |
Correct |
88 ms |
20916 KB |
Output is correct |
13 |
Correct |
87 ms |
20516 KB |
Output is correct |
14 |
Correct |
83 ms |
20560 KB |
Output is correct |
15 |
Correct |
33 ms |
13848 KB |
Output is correct |
16 |
Correct |
71 ms |
20304 KB |
Output is correct |
17 |
Correct |
84 ms |
17492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Incorrect |
20 ms |
15964 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
3 ms |
10840 KB |
Output is correct |
4 |
Incorrect |
86 ms |
19008 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
3 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
10668 KB |
Output is correct |
16 |
Correct |
3 ms |
10588 KB |
Output is correct |
17 |
Correct |
3 ms |
10760 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10584 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
2 ms |
10584 KB |
Output is correct |
24 |
Correct |
2 ms |
10588 KB |
Output is correct |
25 |
Correct |
2 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
28 ms |
10880 KB |
Output is correct |
31 |
Correct |
36 ms |
11100 KB |
Output is correct |
32 |
Correct |
55 ms |
10840 KB |
Output is correct |
33 |
Correct |
56 ms |
10844 KB |
Output is correct |
34 |
Correct |
55 ms |
10924 KB |
Output is correct |
35 |
Correct |
54 ms |
10844 KB |
Output is correct |
36 |
Correct |
55 ms |
10908 KB |
Output is correct |
37 |
Correct |
57 ms |
10920 KB |
Output is correct |
38 |
Correct |
46 ms |
10964 KB |
Output is correct |
39 |
Correct |
45 ms |
10948 KB |
Output is correct |
40 |
Correct |
44 ms |
10840 KB |
Output is correct |
41 |
Correct |
47 ms |
10844 KB |
Output is correct |
42 |
Correct |
51 ms |
10948 KB |
Output is correct |
43 |
Correct |
50 ms |
10844 KB |
Output is correct |
44 |
Correct |
45 ms |
10840 KB |
Output is correct |
45 |
Correct |
45 ms |
10840 KB |
Output is correct |
46 |
Correct |
44 ms |
10840 KB |
Output is correct |
47 |
Correct |
46 ms |
10844 KB |
Output is correct |
48 |
Correct |
46 ms |
10840 KB |
Output is correct |
49 |
Correct |
49 ms |
11096 KB |
Output is correct |
50 |
Correct |
20 ms |
10840 KB |
Output is correct |
51 |
Correct |
20 ms |
10844 KB |
Output is correct |
52 |
Correct |
21 ms |
10932 KB |
Output is correct |
53 |
Correct |
21 ms |
10844 KB |
Output is correct |
54 |
Correct |
21 ms |
10928 KB |
Output is correct |
55 |
Correct |
21 ms |
10844 KB |
Output is correct |
56 |
Correct |
3 ms |
10840 KB |
Output is correct |
57 |
Correct |
24 ms |
10800 KB |
Output is correct |
58 |
Correct |
40 ms |
10920 KB |
Output is correct |
59 |
Correct |
3 ms |
10588 KB |
Output is correct |
60 |
Correct |
2 ms |
10840 KB |
Output is correct |
61 |
Correct |
3 ms |
10844 KB |
Output is correct |
62 |
Correct |
72 ms |
18372 KB |
Output is correct |
63 |
Correct |
65 ms |
17012 KB |
Output is correct |
64 |
Correct |
63 ms |
19028 KB |
Output is correct |
65 |
Correct |
98 ms |
20560 KB |
Output is correct |
66 |
Correct |
100 ms |
20548 KB |
Output is correct |
67 |
Correct |
112 ms |
20676 KB |
Output is correct |
68 |
Correct |
98 ms |
20560 KB |
Output is correct |
69 |
Correct |
97 ms |
20564 KB |
Output is correct |
70 |
Correct |
88 ms |
20916 KB |
Output is correct |
71 |
Correct |
87 ms |
20516 KB |
Output is correct |
72 |
Correct |
83 ms |
20560 KB |
Output is correct |
73 |
Correct |
33 ms |
13848 KB |
Output is correct |
74 |
Correct |
71 ms |
20304 KB |
Output is correct |
75 |
Correct |
84 ms |
17492 KB |
Output is correct |
76 |
Correct |
2 ms |
10588 KB |
Output is correct |
77 |
Incorrect |
20 ms |
15964 KB |
Output isn't correct |
78 |
Halted |
0 ms |
0 KB |
- |