QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#407141 | #7103. Red Black Tree | Sorting | TL | 1ms | 5716kb | C++20 | 6.2kb | 2024-05-08 01:47:18 | 2024-05-08 01:47:19 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector < int > vi ;
#define fi first
#define se second
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
const int MAXN = 1e5 + 7 ;
const int LOG = 20 ;
const ll inf = 1e18 ;
int n , m , q ;
vector < pii > v[ MAXN ] ;
bool spec[ MAXN ] ;
int st[ MAXN ] , en[ MAXN ] , tp ;
ll pref[ MAXN ] ;
int lvl[ MAXN ] , LCA[ MAXN ][ LOG ] ;
int anc[ MAXN ] ;
void dfs ( int x , int prv , int lst ) {
st[ x ] = ++ tp ;
anc[ x ] = lst ;
for ( int i = 1 ; i < LOG ; ++ i ) {
LCA[ x ][ i ] = LCA[ LCA[ x ][ i - 1 ] ][ i - 1 ] ;
}
for ( auto [ y , len ] : v[ x ] ) {
if ( y == prv ) { continue ; }
pref[ y ] = pref[ x ] + len ;
lvl[ y ] = lvl[ x ] + 1 ;
LCA[ y ][ 0 ] = x ;
if ( spec[ y ] == true ) { dfs ( y , x , y ) ; }
else { dfs ( y , x , lst ) ; }
}
en[ x ] = tp ;
}
int get_lca ( int x , int y ) {
for ( int i = LOG - 1 ; i >= 0 ; -- i ) {
if ( lvl[ x ] - ( 1 << i ) >= lvl[ y ] ) {
x = LCA[ x ][ i ] ;
}
if ( lvl[ y ] - ( 1 << i ) >= lvl[ x ] ) {
y = LCA[ y ][ i ] ;
}
}
for ( int i = LOG - 1 ; i >= 0 ; -- i ) {
if ( LCA[ x ][ i ] != LCA[ y ][ i ] ) {
x = LCA[ x ][ i ] , y = LCA[ y ][ i ] ;
}
}
if ( x != y ) { x = LCA[ x ][ 0 ] ; }
return x ;
}
class Tree {
public :
ll mx_ans[ 4 * MAXN ] , mx_pref[ 4 * MAXN ] ;
void init ( int w , int l , int r ) {
mx_ans[ w ] = mx_pref[ w ] ;
if ( l == r ) { return ; }
int mid = ( l + r ) / 2 ;
init ( 2 * w , l , mid ) ;
init ( 2 * w + 1 , mid + 1 , r ) ;
}
void update ( int w , int l , int r , int pos , ll nw_ans , ll nw_pref ) {
if ( l == r ) {
if ( nw_ans < 0 ) { mx_ans[ w ] = mx_pref[ w ] = 0 ; }
else { mx_ans[ w ] = nw_ans , mx_pref[ w ] = nw_pref ; }
return ;
}
int mid = ( l + r ) / 2 ;
if ( pos <= mid ) { update ( 2 * w , l , mid , pos , nw_ans , nw_pref ) ; }
else { update ( 2 * w + 1 , mid + 1 , r , pos , nw_ans , nw_pref ) ; }
mx_ans[ w ] = max ( mx_ans[ 2 * w ] , mx_ans[ 2 * w + 1 ] ) ;
mx_pref[ w ] = max ( mx_pref[ 2 * w ] , mx_pref[ 2 * w + 1 ] ) ;
}
ll query_ans ( int w , int l , int r , int st , int en ) {
if ( l > r || st > en ) { return 0 ; }
if ( r < st || en < l ) { return 0 ; }
if ( st <= l && r <= en ) { return mx_ans[ w ] ; }
int mid = ( l + r ) / 2 ;
return max ( query_ans ( 2 * w , l , mid , st , en ) , query_ans ( 2 * w + 1 , mid + 1 , r , st , en ) ) ;
}
ll query_pref ( int w , int l , int r , int st , int en ) {
if ( l > r || st > en ) { return 0 ; }
if ( r < st || en < l ) { return 0 ; }
if ( st <= l && r <= en ) { return mx_pref[ w ] ; }
int mid = ( l + r ) / 2 ;
return max ( query_pref ( 2 * w , l , mid , st , en ) , query_pref ( 2 * w + 1 , mid + 1 , r , st , en ) ) ;
}
};
Tree w ;
int k ;
int vals[ MAXN ] ;
int pos[ MAXN ] , neg[ MAXN ] ;
void solve ( ) {
cin >> n >> m >> q ;
for ( int i = 1 ; i <= n ; ++ i ) {
v[ i ].clear ( ) ;
spec[ i ] = false ;
}
for ( int i = 1 , x ; i <= m ; ++ i ) {
cin >> x ;
spec[ x ] = true ;
}
for ( int i = 1 , x , y , z ; i < n ; ++ i ) {
cin >> x >> y >> z ;
v[ x ].push_back ( { y , z } ) ;
v[ y ].push_back ( { x , z } ) ;
}
tp = 0 ;
dfs ( 1 , -1 , 1 ) ;
w.init ( 1 , 1 , n ) ;
auto cmp_ord = [ & ] ( int x , int y ) {
return ( st[ x ] < st[ y ] ) ;
};
auto cmp_lvl = [ & ] ( int x , int y ) {
return ( lvl[ x ] > lvl[ y ] ) ;
};
auto cmp_anc_lvl = [ & ] ( int x , int y ) {
return ( lvl[ anc[ x ] ] > lvl[ anc[ y ] ] ) ;
};
while ( q -- ) {
cin >> k ;
vector < int > cands ;
map < ll , int > aux ;
for ( int i = 1 ; i <= k ; ++ i ) {
cin >> vals[ i ] ;
pos[ i ] = neg[ i ] = vals[ i ] ;
cands.push_back ( vals[ i ] ) ;
aux[ pref[ vals[ i ] ] - pref[ anc[ vals[ i ] ] ] ] ++ ;
}
sort ( vals + 1 , vals + k + 1 , cmp_ord ) ;
sort ( pos + 1 , pos + k + 1 , cmp_lvl ) ;
sort ( neg + 1 , neg + k + 1 , cmp_anc_lvl ) ;
for ( int i = 0 ; i + 1 < k ; ++ i ) {
cands.push_back ( get_lca ( vals[ i + 1 ] , vals[ i + 2 ] ) ) ;
}
sort ( cands.begin ( ) , cands.end ( ) , cmp_lvl ) ;
int tp_vert = 0 , tp_anc = 0 ;
ll ans = inf ;
for ( auto x : cands ) {
while ( tp_vert < k && lvl[ pos[ tp_vert + 1 ] ] >= lvl[ x ] ) {
int y = pos[ ++ tp_vert ] , sr = pref[ y ] - pref[ anc[ y ] ] ;
-- aux[ sr ] ;
if ( aux[ sr ] == 0 ) { aux.erase ( sr ) ; }
w.update ( 1 , 1 , n , st[ y ] , pref[ y ] - pref[ anc[ y ] ] , pref[ y ] ) ;
}
while ( tp_anc < k && lvl[ anc[ neg[ tp_anc + 1 ] ] ] >= lvl[ x ] ) {
int y = neg[ ++ tp_anc ] , sr = pref[ y ] - pref[ anc[ y ] ] ;
++ aux[ sr ] ;
w.update ( 1 , 1 , n , st[ y ] , -1 , -1 ) ;
}
ll cand = 0 ;
if ( aux.empty ( ) == false ) { cand = (*aux.rbegin ( )).fi ; }
cand = max ( { cand , w.query_ans ( 1 , 1 , n , 1 , st[ x ] - 1 ) , w.query_ans ( 1 , 1 , n , en[ x ] + 1 , n ) } ) ;
cand = max ( cand , w.query_pref ( 1 , 1 , n , st[ x ] , en[ x ] ) - pref[ x ] ) ;
// printf ( "%d --> %lld\n" , x , cand ) ;
ans = min ( ans , cand ) ;
}
while ( tp_anc < k ) {
int y = neg[ ++ tp_anc ] , sr = pref[ y ] - pref[ anc[ y ] ] ;
w.update ( 1 , 1 , n , st[ y ] , -1 , -1 ) ;
}
cout << ans << "\n" ;
}
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; cin >> t ;
while ( t -- ) { solve ( ) ; }
return 0 ;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5716kb
input:
2 12 2 4 1 9 1 2 1 2 3 4 3 4 3 3 5 2 2 6 2 6 7 1 6 8 2 2 9 5 9 10 2 9 11 3 1 12 10 3 3 7 8 4 4 5 7 8 4 7 8 10 11 3 4 5 12 3 2 3 1 2 1 2 1 1 3 1 1 1 2 1 2 3 1 2 3
output:
4 5 3 8 0 0 0
result:
ok 7 lines
Test #2:
score: -100
Time Limit Exceeded
input:
522 26 1 3 1 1 4 276455 18 6 49344056 18 25 58172365 19 9 12014251 2 1 15079181 17 1 50011746 8 9 2413085 23 24 23767115 22 2 26151339 26 21 50183935 17 14 16892041 9 26 53389093 1 20 62299200 24 18 56114328 11 2 50160143 6 26 14430542 16 7 32574577 3 16 59227555 3 15 8795685 4 12 5801074 5 20 57457...
output:
148616264 148616264 0 319801028 319801028 255904892 317070839 1265145897 1265145897 1072765445 667742619 455103436 285643094 285643094 285643094 317919339 0 785245841 691421476 605409472 479058444 371688030 303203698 493383271 919185207 910180170 919185207 121535083 181713164 181713164 181713164 181...