QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#204215 | #2429. Conquer The World | Sorting | AC ✓ | 897ms | 418052kb | C++20 | 3.7kb | 2023-10-07 06:26:56 | 2023-10-07 06:26:57 |
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 = 3e5 + 7 ;
int n ;
vector < pii > v[ MAXN ] ;
pii a[ MAXN ] ;
bool is_root[ MAXN ] ;
int sub[ MAXN ] ;
int val[ MAXN ] ;
ll pref[ MAXN ] ;
pii tot ;
ll ans , dp[ MAXN ] ;
void init ( int x , int prv ) {
sub[ x ] = val[ x ] ;
for ( auto [ y , len ] : v[ x ] ) {
if ( y == prv ) { continue ; }
pref[ y ] = pref[ x ] + len ;
init ( y , x ) ;
sub[ x ] += sub[ y ] ;
}
if ( sub[ x ] < 0 ) {
ans += ( pref[ x ] - pref[ prv ] ) * ( -sub[ x ] ) ;
val[ x ] += ( -sub[ x ] ) ;
val[ prv ] -= ( -sub[ x ] ) ;
}
else if ( tot.fi - tot.se - sub[ x ] < 0 ) {
int hh = tot.fi - tot.se - sub[ x ] ;
ans += ( pref[ x ] - pref[ prv ] ) * ( -hh ) ;
val[ x ] -= ( -hh ) ;
val[ prv ] += ( -hh ) ;
}
}
multiset < ll > complete[ MAXN ] ;
multiset < ll > lft[ MAXN ] ;
void dfs ( int x , int prv ) {
for ( auto [ y , len ] : v[ x ] ) {
if ( y == prv ) { continue ; }
dfs ( y , x ) ;
dp[ x ] += dp[ y ] ;
if ( complete[ x ].size ( ) < complete[ y ].size ( ) ) {
swap ( complete[ x ] , complete[ y ] ) ;
}
if ( lft[ x ].size ( ) < lft[ y ].size ( ) ) {
swap ( lft[ x ] , lft[ y ] ) ;
}
}
for ( auto [ y , len ] : v[ x ] ) {
if ( y == prv ) { continue ; }
for ( auto sr : complete[ y ] ) {
complete[ x ].insert ( sr ) ;
}
for ( auto sr : lft[ y ] ) {
lft[ x ].insert ( sr ) ;
}
}
while ( val[ x ] > 0 ) {
-- val[ x ] ;
lft[ x ].insert ( pref[ x ] ) ;
}
while ( val[ x ] < 0 ) {
++ val[ x ] ;
auto it = lft[ x ].begin ( ) ;
ll aux = *it - pref[ x ] ; lft[ x ].erase ( it ) ;
dp[ x ] += aux ;
complete[ x ].insert ( -aux + pref[ x ] ) ;
}
/**
while ( val[ x ] < 0 ) {
++ val[ x ] ;
ll aux = ( 1LL << 40 ) ;
dp[ x ] += aux ;
complete[ x ].insert ( { -aux , x } ) ;
}
**/
while ( complete[ x ].empty ( ) == false && lft[ x ].empty ( ) == false ) {
auto it1 = complete[ x ].begin ( ) ;
auto it2 = lft[ x ].begin ( ) ;
ll add = *it1 ;
ll cand = *it2 ;
if ( add + cand - 2 * pref[ x ] < 0 ) {
complete[ x ].erase ( it1 ) ;
lft[ x ].erase ( it2 ) ;
dp[ x ] += add + cand - 2 * pref[ x ] ;
complete[ x ].insert ( - ( cand - pref[ x ] ) + pref[ x ] ) ;
lft[ x ].insert ( cand - ( add + cand - 2 * pref[ x ] ) ) ;
}
else { break ; }
}
}
void solve ( ) {
cin >> n ;
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 } ) ;
}
for ( int i = 1 ; i <= n ; ++ i ) {
cin >> a[ i ].fi >> a[ i ].se ;
int aux = min ( a[ i ].fi , a[ i ].se ) ;
a[ i ].fi -= aux , a[ i ].se -= aux ;
tot.fi += a[ i ].fi , tot.se += a[ i ].se ;
val[ i ] = a[ i ].fi - a[ i ].se ;
}
init ( 1 , -1 ) ;
dfs ( 1 , -1 ) ;
cout << ans + dp[ 1 ] << "\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: 0ms
memory: 46392kb
Test #2:
score: 0
Accepted
time: 10ms
memory: 44492kb
Test #3:
score: 0
Accepted
time: 3ms
memory: 44496kb
Test #4:
score: 0
Accepted
time: 3ms
memory: 44352kb
Test #5:
score: 0
Accepted
time: 7ms
memory: 46544kb
Test #6:
score: 0
Accepted
time: 8ms
memory: 47060kb
Test #7:
score: 0
Accepted
time: 3ms
memory: 46588kb
Test #8:
score: 0
Accepted
time: 0ms
memory: 44540kb
Test #9:
score: 0
Accepted
time: 128ms
memory: 90744kb
Test #10:
score: 0
Accepted
time: 50ms
memory: 59996kb
Test #11:
score: 0
Accepted
time: 202ms
memory: 95856kb
Test #12:
score: 0
Accepted
time: 0ms
memory: 46596kb
Test #13:
score: 0
Accepted
time: 0ms
memory: 44516kb
Test #14:
score: 0
Accepted
time: 3ms
memory: 46728kb
Test #15:
score: 0
Accepted
time: 0ms
memory: 47108kb
Test #16:
score: 0
Accepted
time: 3ms
memory: 44780kb
Test #17:
score: 0
Accepted
time: 9ms
memory: 48080kb
Test #18:
score: 0
Accepted
time: 15ms
memory: 53008kb
Test #19:
score: 0
Accepted
time: 66ms
memory: 78796kb
Test #20:
score: 0
Accepted
time: 253ms
memory: 172616kb
Test #21:
score: 0
Accepted
time: 94ms
memory: 56188kb
Test #22:
score: 0
Accepted
time: 87ms
memory: 56176kb
Test #23:
score: 0
Accepted
time: 112ms
memory: 57472kb
Test #24:
score: 0
Accepted
time: 48ms
memory: 53828kb
Test #25:
score: 0
Accepted
time: 63ms
memory: 54360kb
Test #26:
score: 0
Accepted
time: 113ms
memory: 59484kb
Test #27:
score: 0
Accepted
time: 469ms
memory: 230628kb
Test #28:
score: 0
Accepted
time: 262ms
memory: 120980kb
Test #29:
score: 0
Accepted
time: 651ms
memory: 295852kb
Test #30:
score: 0
Accepted
time: 701ms
memory: 319216kb
Test #31:
score: 0
Accepted
time: 827ms
memory: 366016kb
Test #32:
score: 0
Accepted
time: 839ms
memory: 391336kb
Test #33:
score: 0
Accepted
time: 897ms
memory: 418052kb
Test #34:
score: 0
Accepted
time: 856ms
memory: 392796kb
Test #35:
score: 0
Accepted
time: 314ms
memory: 104340kb
Test #36:
score: 0
Accepted
time: 277ms
memory: 98508kb
Test #37:
score: 0
Accepted
time: 699ms
memory: 307824kb
Test #38:
score: 0
Accepted
time: 143ms
memory: 88460kb
Test #39:
score: 0
Accepted
time: 121ms
memory: 77196kb
Test #40:
score: 0
Accepted
time: 418ms
memory: 149748kb
Test #41:
score: 0
Accepted
time: 192ms
memory: 85172kb
Test #42:
score: 0
Accepted
time: 282ms
memory: 97800kb
Test #43:
score: 0
Accepted
time: 267ms
memory: 99168kb
Test #44:
score: 0
Accepted
time: 268ms
memory: 106512kb
Test #45:
score: 0
Accepted
time: 211ms
memory: 92640kb
Test #46:
score: 0
Accepted
time: 7ms
memory: 46400kb
Test #47:
score: 0
Accepted
time: 208ms
memory: 93420kb
Test #48:
score: 0
Accepted
time: 0ms
memory: 46468kb
Test #49:
score: 0
Accepted
time: 660ms
memory: 298904kb
Test #50:
score: 0
Accepted
time: 114ms
memory: 57404kb
Test #51:
score: 0
Accepted
time: 5ms
memory: 46460kb
Test #52:
score: 0
Accepted
time: 6ms
memory: 46520kb
Test #53:
score: 0
Accepted
time: 6ms
memory: 44492kb
Test #54:
score: 0
Accepted
time: 0ms
memory: 46528kb
Test #55:
score: 0
Accepted
time: 0ms
memory: 44348kb
Test #56:
score: 0
Accepted
time: 3ms
memory: 46456kb
Test #57:
score: 0
Accepted
time: 0ms
memory: 44568kb
Test #58:
score: 0
Accepted
time: 12ms
memory: 47832kb
Test #59:
score: 0
Accepted
time: 108ms
memory: 76896kb
Test #60:
score: 0
Accepted
time: 307ms
memory: 147320kb
Test #61:
score: 0
Accepted
time: 476ms
memory: 214560kb
Test #62:
score: 0
Accepted
time: 722ms
memory: 283620kb
Test #63:
score: 0
Accepted
time: 83ms
memory: 56224kb
Test #64:
score: 0
Accepted
time: 86ms
memory: 56848kb
Test #65:
score: 0
Accepted
time: 80ms
memory: 57312kb
Test #66:
score: 0
Accepted
time: 80ms
memory: 56044kb
Test #67:
score: 0
Accepted
time: 192ms
memory: 88960kb
Test #68:
score: 0
Accepted
time: 126ms
memory: 87624kb
Test #69:
score: 0
Accepted
time: 133ms
memory: 89616kb
Test #70:
score: 0
Accepted
time: 132ms
memory: 88640kb