답안 #767150

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
767150 2023-06-26T12:42:47 Z lollipop Cat Exercise (JOI23_ho_t4) C++17
100 / 100
341 ms 77008 KB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define int long long
#define pb push_back
#define s second
#define f first
#define pf push_front
#define inf 100000000000000000
#define bitebi __builtin_popcountll
#define FOR( i , n ) for( int i = 0 ; i < n ; i ++ )
#define YES cout <<"YES\n"
#define NO cout << "NO\n"
#define debug cout << "Here Fine" << endl ;
#define pr pair < int , int >
#define fbo find_by_order // returns iterator
#define ook order_of_key // returns strictly less numbers than key
using namespace std ;
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const double Pi=acos(-1.0);
const double EPS=1E-8;
const int mod =  1000000007 ;
const int mod1 = 998244353 ;
const int N = 2e5 + 10 ;
mt19937 R(time(0));
map < int , int > ma , ma1 ;
int node[ 4 * N ] , w[ N ] , inv[ N ] , ap[ N ] ;
void build( int v , int vl , int vr ){
	 if( vl == vr ){
	 	node[ v ] = ap[ vl ] ;
	 	return ;
	 }
	 int vm = ( vl + vr ) / 2 ;
	 build( v + v , vl , vm ) ;
	 build( v + v + 1 , vm + 1 , vr ) ;
	 node[ v ] = max( node[ v + v ] , node[ v + v + 1 ] ) ;
	 return ;
}
void update( int v , int vl , int vr , int pos ){
	 if( vl == vr ){
	 	node[ v ] = - 1 ;
	 	return ; 
	 }
	 int vm = ( vl + vr ) / 2 ;
	 if( vm >= pos ) update( v + v , vl , vm , pos ) ;
	 else update( v + v + 1 , vm + 1 , vr , pos ) ;
	 node[ v ] = max( node[ v + v ] , node[ v + v + 1 ] ) ; 
	 return ;
}
int get( int v , int vl , int vr , int l , int r ){
	if( l > r ) return -1 ;
	if( vl == l && vr == r ) return node[ v ] ;
	int vm = ( vl + vr ) / 2 ;
	int a = get( v + v , vl , vm , l , min( r , vm ) ) ;
	int b = get( v + v + 1 , vm + 1 , vr , max( vm + 1 , l ) , r ) ;
	return max( a , b ) ;
}
vector < int > v[ N ] ;
int h[ N ] , tin[ N ] , tout[ N ] , timer = 1 ; 
int pp[ N ][ 20 ] , xx[ 20 ] , n ; 
void dfs( int node , int parent , int hh ){
	 ap[ timer ] = w[ node ] ;
	 tin[ node ] = timer ; 
	 h[ node ] = hh ;
	 timer ++ ; 
	 pp[ node ][ 0 ] = parent ;
	 for( int j = 1 ; j < 20 ; j ++ ){
	 	if( hh - xx[ j ] < 0 ) pp[ node ][ j ] = -1 ;
	 	else pp[ node ][ j ] = pp[ pp[ node ][ j - 1 ] ][ j - 1 ] ; 
	 }
	 for( auto x : v[ node ] ){
	 	if( x == parent ) continue ;
	 	dfs( x , node , hh + 1 ) ; 
	 }
	 tout[ node ] = timer - 1 ; 
}
bool is_lca( int a , int b ){
	if( a == -1 ) return true ; 
	if( a == b ) return true ;
	if( tin[ a ] <= tin[ b ] && tout[ a ] >= tout[ b ] ) return true ;
	return false ; 
}
int find_lca( int a , int b ){
	if( is_lca( a , b ) == true ) return a ;
	if( is_lca( b , a ) == true ) return b ;
	for( int j = 19 ; j >= 0 ; j -- ){
		if( is_lca( pp[ a ][ j ] , b ) == false ) a = pp[ a ][ j ] ; 
	}
	return pp[ a ][ 0 ] ; 
}
int dist( int a , int b ){
	if( a == -1 || b == -1 ) return 0 ;
	int c = find_lca( a , b ) ;
	int ans = abs( h[ c ] - h[ a ] ) + abs( h[ c ] - h[ b ] ) ;
	return ans ; 
}
int been[ N ] ; 
int sol( int st , int frm , int papi ){
//	cout << st << " " << frm << " " << papi << " tur " ; 
	int alr = dist( st , frm ) ;
//	cout << alr << endl ; 
	been[ st ] = 1 ;
	update( 1 , 1 , n , tin[ st ] ) ; 
	int ans = alr ; 
	for( auto x : v[ st ] ){
		if( h[ x ] < h[ st ] ) continue ; 
		if( been[ x ] == 1 ) continue ; 
		int mx = get( 1 , 1 , n , tin[ x ] , tout[ x ] ) ; 
		if( mx == -1 ) continue ; 
		mx = inv[ mx ] ;
		ans = max( ans , sol( mx , st , x ) + alr ) ; 
	}
   if( papi != -1 ){
	int x = papi ; 
	int mx = get( 1 , 1 , n , tin[ x ] , tout[ x ] ) ;
	if( mx != -1 ){
        mx = inv[ mx ] ; 
    	ans = max( ans , sol( mx , st , x ) + alr ) ;
    }
   }
	return ans ; 
}
void solve(){
     cin >> n ;
     FOR( i , n ){
     	cin >> w[ i + 1 ] ;
     	inv[ w[ i + 1 ] ] = i + 1 ; 
	 }
     FOR( i , n - 1 ){
     	int x , y ; cin >> x >> y ;
     	v[ x ].pb( y ) ; v[ y ].pb( x ) ; 
	 }
     dfs( inv[ n ] , -1 , 0 ) ; 
     build( 1 , 1 , n ) ; 
     int ans = sol( inv[ n ] , -1 , -1 ) ; 
     cout << ans ; 
}
signed main() {
   ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
   xx[ 0 ] = 1 ; 
   for( int j = 1 ; j < 20 ; j ++ ) xx[ j ] = xx[ j - 1 ] * 2 ; 
   int t = 1 ; // cin >> t ;
   while( t -- ){
   	 solve() ;
   }

}




# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5048 KB Output is correct
3 Correct 2 ms 5040 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 2 ms 5044 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5044 KB Output is correct
9 Correct 3 ms 5040 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5048 KB Output is correct
3 Correct 2 ms 5040 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 2 ms 5044 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5044 KB Output is correct
9 Correct 3 ms 5040 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 4 ms 5172 KB Output is correct
15 Correct 3 ms 5152 KB Output is correct
16 Correct 2 ms 5172 KB Output is correct
17 Correct 3 ms 5172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5048 KB Output is correct
3 Correct 2 ms 5040 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 2 ms 5044 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5044 KB Output is correct
9 Correct 3 ms 5040 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 4 ms 5172 KB Output is correct
15 Correct 3 ms 5152 KB Output is correct
16 Correct 2 ms 5172 KB Output is correct
17 Correct 3 ms 5172 KB Output is correct
18 Correct 5 ms 6836 KB Output is correct
19 Correct 7 ms 6760 KB Output is correct
20 Correct 6 ms 6832 KB Output is correct
21 Correct 5 ms 6612 KB Output is correct
22 Correct 6 ms 6592 KB Output is correct
23 Correct 6 ms 6596 KB Output is correct
24 Correct 6 ms 6616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5048 KB Output is correct
3 Correct 2 ms 5040 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 2 ms 5044 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5044 KB Output is correct
9 Correct 3 ms 5040 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 4 ms 5172 KB Output is correct
15 Correct 3 ms 5152 KB Output is correct
16 Correct 2 ms 5172 KB Output is correct
17 Correct 3 ms 5172 KB Output is correct
18 Correct 5 ms 6836 KB Output is correct
19 Correct 7 ms 6760 KB Output is correct
20 Correct 6 ms 6832 KB Output is correct
21 Correct 5 ms 6612 KB Output is correct
22 Correct 6 ms 6592 KB Output is correct
23 Correct 6 ms 6596 KB Output is correct
24 Correct 6 ms 6616 KB Output is correct
25 Correct 2 ms 5076 KB Output is correct
26 Correct 6 ms 6776 KB Output is correct
27 Correct 7 ms 6740 KB Output is correct
28 Correct 6 ms 6740 KB Output is correct
29 Correct 7 ms 6716 KB Output is correct
30 Correct 6 ms 6484 KB Output is correct
31 Correct 6 ms 6484 KB Output is correct
32 Correct 6 ms 6484 KB Output is correct
33 Correct 8 ms 6468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5048 KB Output is correct
3 Correct 2 ms 5040 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 2 ms 5044 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5044 KB Output is correct
9 Correct 3 ms 5040 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 4 ms 5172 KB Output is correct
15 Correct 3 ms 5152 KB Output is correct
16 Correct 2 ms 5172 KB Output is correct
17 Correct 3 ms 5172 KB Output is correct
18 Correct 5 ms 6836 KB Output is correct
19 Correct 7 ms 6760 KB Output is correct
20 Correct 6 ms 6832 KB Output is correct
21 Correct 5 ms 6612 KB Output is correct
22 Correct 6 ms 6592 KB Output is correct
23 Correct 6 ms 6596 KB Output is correct
24 Correct 6 ms 6616 KB Output is correct
25 Correct 156 ms 77008 KB Output is correct
26 Correct 161 ms 75592 KB Output is correct
27 Correct 154 ms 75596 KB Output is correct
28 Correct 170 ms 68300 KB Output is correct
29 Correct 204 ms 69452 KB Output is correct
30 Correct 160 ms 69244 KB Output is correct
31 Correct 159 ms 69804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 188 ms 64060 KB Output is correct
4 Correct 187 ms 64128 KB Output is correct
5 Correct 186 ms 63912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5048 KB Output is correct
3 Correct 2 ms 5040 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 2 ms 5044 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5044 KB Output is correct
9 Correct 3 ms 5040 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 4 ms 5172 KB Output is correct
15 Correct 3 ms 5152 KB Output is correct
16 Correct 2 ms 5172 KB Output is correct
17 Correct 3 ms 5172 KB Output is correct
18 Correct 5 ms 6836 KB Output is correct
19 Correct 7 ms 6760 KB Output is correct
20 Correct 6 ms 6832 KB Output is correct
21 Correct 5 ms 6612 KB Output is correct
22 Correct 6 ms 6592 KB Output is correct
23 Correct 6 ms 6596 KB Output is correct
24 Correct 6 ms 6616 KB Output is correct
25 Correct 2 ms 5076 KB Output is correct
26 Correct 6 ms 6776 KB Output is correct
27 Correct 7 ms 6740 KB Output is correct
28 Correct 6 ms 6740 KB Output is correct
29 Correct 7 ms 6716 KB Output is correct
30 Correct 6 ms 6484 KB Output is correct
31 Correct 6 ms 6484 KB Output is correct
32 Correct 6 ms 6484 KB Output is correct
33 Correct 8 ms 6468 KB Output is correct
34 Correct 156 ms 77008 KB Output is correct
35 Correct 161 ms 75592 KB Output is correct
36 Correct 154 ms 75596 KB Output is correct
37 Correct 170 ms 68300 KB Output is correct
38 Correct 204 ms 69452 KB Output is correct
39 Correct 160 ms 69244 KB Output is correct
40 Correct 159 ms 69804 KB Output is correct
41 Correct 3 ms 5076 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 188 ms 64060 KB Output is correct
44 Correct 187 ms 64128 KB Output is correct
45 Correct 186 ms 63912 KB Output is correct
46 Correct 314 ms 72776 KB Output is correct
47 Correct 314 ms 72836 KB Output is correct
48 Correct 333 ms 72956 KB Output is correct
49 Correct 341 ms 72848 KB Output is correct
50 Correct 311 ms 62716 KB Output is correct
51 Correct 318 ms 62744 KB Output is correct
52 Correct 302 ms 62596 KB Output is correct
53 Correct 294 ms 62716 KB Output is correct