1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "vector" using namespace std; #define MAXN 100006 typedef long long ll; #define min( a , b ) ( (a) < (b) ? (a) : (b) ) int n , m , L; #define D 100006 int A[MAXN] , B[MAXN]; vector<int> G[MAXN];
struct line { ll k , b; ll re( ll x ) { return k * x + b; } } f[MAXN] ;
int ls[MAXN << 4] , rs[MAXN << 4] , id[MAXN << 4] , cnt , rt[MAXN]; void ins( int& x , int l , int r , int d ) { if( !x ) { x = ++ cnt , id[x] = d; return; } int m = l + r >> 1; if( f[id[x]].re( m ) > f[d].re( m ) ) swap( d , id[x] ); if( f[id[x]].re( l ) <= f[d].re( l ) && f[id[x]].re( r ) <= f[d].re( r ) ) return; if( f[id[x]].re( l ) > f[d].re( l ) ) ins( ls[x] , l , m , d ); else ins( rs[x] , m + 1 , r , d ); } long long que( int x , int l , int r , int p ) { if( !x ) return 0x3f3f3f3f3f3f3f3f; int m = l + r >> 1; ll re = f[id[x]].re( p ); if( p <= m ) return min( re , que( ls[x] , l , m , p )); else return min( re , que( rs[x] , m + 1 , r , p )); } int merge( int x , int y , int l , int r ) {
if( !x || !y ) return x + y; ins( x , l , r , id[y] ); int m = l + r >> 1; ls[x] = merge( ls[x] , ls[y] , l , m ); rs[x] = merge( rs[x] , rs[y] , m + 1 , r ); return x; } long long ans[MAXN]; void dfs( int u , int fa ) { for( int v : G[u] ) if( v != fa ) { dfs( v , u ); rt[u] = merge( rt[u] , rt[v] , 1 , D << 1 ); } ans[u] = que( rt[u] , 1 , D << 1 , A[u] + D ); if( ans[u] > 1e18 ) ans[u] = 0; f[u] = (line) { B[u] , ans[u] - 1ll * B[u] * D }; ins( rt[u] , 1 , D << 1 , u ); } int main() { cin >> n; for( int i = 1 ; i <= n ; ++ i ) scanf("%d",&A[i]); for( int i = 1 ; i <= n ; ++ i ) scanf("%d",&B[i]); for( int i = 1 , u , v ; i < n ; ++ i ) { scanf("%d%d",&u,&v) , G[u].push_back( v ) , G[v].push_back( u ); } dfs( 1 , 1 ); for( int i = 1 ; i <= n ; ++ i ) printf("%lld ",ans[i]); }
|