Submission #889620

#TimeUsernameProblemLanguageResultExecution timeMemory
889620vjudge1Tourism (JOI23_tourism)C++17
10 / 100
5013 ms14560 KiB
#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) ;
}

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] ;
	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 = c[x] , mx = c[x] ;
		for ( int i = x ; i <= y ; i ++ ){
			chmin ( mn , c[i] ) ;
			chmax ( mx , c[i] ) ;
		}
		cout << mx-mn+1 << endl ;
	}
	
}

signed main(){
    ios ;
	int t = 1 ;
	//cin >> t ;
	while ( t -- ) solve() ;
}

Compilation message (stderr)

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);}
      |                                                                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...