#include<bits/stdc++.h>
using namespace std ;
#define MAXN 100007
#define LOG 20
int n ;
int a[ MAXN ] ;
pair < int , int > edges[ MAXN ] ;
vector < int > v[ MAXN ] ;
int pos_in_tree[ MAXN ] ;
int LCA[ MAXN ][ LOG ] ;
int subtree[ MAXN ] , heavy[ MAXN ] ;
int root[ MAXN ] ;
void dfs ( int vertex ) {
for ( int i = 1 ; i < LOG ; ++ i ) {
LCA[ vertex ][ i ] = LCA[ LCA[ vertex ][ i - 1 ] ][ i - 1 ] ;
}
subtree[ vertex ] = 1 ;
int mx = -1 ;
heavy[ vertex ] = 0 ;
for ( auto x : v[ vertex ] ) {
LCA[ x ][ 0 ] = vertex ;
dfs ( x ) ;
subtree[ vertex ] += subtree[ x ] ;
if ( mx < subtree[ x ] ) {
mx = subtree[ x ] ;
heavy[ vertex ] = x ;
}
}
}
class Tree {
public :
int tr[ 4 * MAXN ] , lazy[ 4 * MAXN ] ;
void push_lazy ( int where , int IL , int IR ) {
if ( lazy[ where ] == 0 ) { return ; }
tr[ where ] = lazy[ where ] ;
if ( IL != IR ) {
lazy[ 2 * where ] = lazy[ where ] ;
lazy[ 2 * where + 1 ] = lazy[ where ] ;
}
lazy[ where ] = 0 ;
}
void init ( int where , int IL , int IR ) {
tr[ where ] = lazy[ where ] = 0 ;
if ( IL == IR ) { return ; }
int mid = ( IL + IR ) / 2 ;
init ( 2 * where , IL , mid ) ;
init ( 2 * where + 1 , mid + 1 , IR ) ;
}
void update ( int where , int IL , int IR , int CURL , int CURR , int nw ) {
push_lazy ( where , IL , IR ) ;
if ( IL > IR || CURL > CURR ) { return ; }
if ( IR < CURL || CURR < IL ) { return ; }
if ( CURL <= IL && IR <= CURR ) {
lazy[ where ] = nw ;
push_lazy ( where , IL , IR ) ;
return ;
}
int mid = ( IL + IR ) / 2 ;
update ( 2 * where , IL , mid , CURL , CURR , nw ) ;
update ( 2 * where + 1 , mid + 1 , IR , CURL , CURR , nw ) ;
}
int query ( int where , int IL , int IR , int pos ) {
push_lazy ( where , IL , IR ) ;
if ( IL == IR ) { return tr[ where ] ; }
int mid = ( IL + IR ) / 2 ;
if ( pos <= mid ) { return query ( 2 * where , IL , mid , pos ) ; }
return query ( 2 * where + 1 , mid + 1 , IR , pos ) ;
}
};
Tree w ;
void input ( ) {
cin >> n ;
for ( int i = 1 ; i <= n ; ++ i ) {
cin >> a[ i ] ;
}
for ( int i = 1 ; i < n ; ++ i ) {
cin >> edges[ i ].first >> edges[ i ].second ;
v[ edges[ i ].first ].push_back ( edges[ i ].second ) ;
}
}
stack < pair < int , int > > s ;
pair < int , int > srt[ MAXN ] ;
pair < int , int > aux[ MAXN ] ;
long long f ( int l , int r ) {
if ( l == r ) { return 0 ; }
int mid = ( l + r ) / 2 ;
long long ret = f ( l , mid ) + f ( mid + 1 , r ) ;
int pos1 , pos2 ;
pos1 = l , pos2 = mid + 1 ;
int tp = l - 1 ;
long long sm_l = 0 ;
for ( int i = l ; i <= mid ; ++ i ) {
sm_l += srt[ i ].second ;
}
while ( pos1 <= mid || pos2 <= r ) {
if ( pos1 > mid ) {
aux[ ++ tp ] = srt[ pos2 ++ ] ;
}
else if ( pos2 > r ) {
aux[ ++ tp ] = srt[ pos1 ++ ] ;
}
else {
if ( srt[ pos1 ].first <= srt[ pos2 ].first ) {
aux[ ++ tp ] = srt[ pos1 ] ;
sm_l -= srt[ pos1 ++ ].second ;
}
else {
aux[ ++ tp ] = srt[ pos2 ] ;
ret += srt[ pos2 ++ ].second * sm_l ;
}
}
}
for ( int i = l ; i <= r ; ++ i ) {
srt[ i ] = aux[ i ] ;
}
return ret ;
}
void solve ( ) {
dfs ( 1 ) ;
int tp = 0 ;
for ( int i = 1 ; i <= n ; ++ i ) {
if ( heavy[ LCA[ i ][ 0 ] ] != i ) {
int x = i ;
while ( x != 0 ) {
pos_in_tree[ x ] = ++ tp ;
root[ x ] = i ;
x = heavy[ x ] ;
}
}
}
w.init ( 1 , 1 , n ) ;
w.update ( 1 , 1 , n , 1 , 1 , 1 ) ;
for ( int i = 1 ; i < n ; ++ i ) {
int wh = edges[ i ].first ;
while ( wh > 0 ) {
int sr = w.query ( 1 , 1 , n , pos_in_tree[ wh ] ) ;
int cnt = 1 ;
for ( int j = LOG - 1 ; j >= 0 ; -- j ) {
if ( LCA[ wh ][ j ] > 0 && w.query ( 1 , 1 , n , pos_in_tree[ LCA[ wh ][ j ] ] ) == sr ) {
wh = LCA[ wh ][ j ] ;
cnt += ( 1 << j ) ;
}
}
s.push ( { a[ sr ] , cnt } ) ;
wh = LCA[ wh ][ 0 ] ;
}
tp = 0 ;
while ( s.empty ( ) == false ) {
srt[ ++ tp ] = s.top ( ) ;
s.pop ( ) ;
}
cout << f ( 1 , tp ) << "\n" ;
wh = edges[ i ].second ;
while ( wh > 0 ) {
w.update ( 1 , 1 , n , pos_in_tree[ root[ wh ] ] , pos_in_tree[ wh ] , edges[ i ].second ) ;
wh = LCA[ root[ wh ] ][ 0 ] ;
}
}
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ;
// cin >> t ;
while ( t -- ) {
input ( ) ;
solve ( ) ;
}
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2704 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2828 KB |
Output is correct |
9 |
Correct |
2 ms |
2836 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2836 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2836 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
2 ms |
2696 KB |
Output is correct |
16 |
Correct |
3 ms |
2708 KB |
Output is correct |
17 |
Correct |
3 ms |
2708 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2832 KB |
Output is correct |
21 |
Correct |
2 ms |
2828 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2696 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
3 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2772 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2704 KB |
Output is correct |
36 |
Correct |
2 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2772 KB |
Output is correct |
38 |
Correct |
2 ms |
2772 KB |
Output is correct |
39 |
Correct |
2 ms |
2772 KB |
Output is correct |
40 |
Correct |
2 ms |
2772 KB |
Output is correct |
41 |
Correct |
3 ms |
2696 KB |
Output is correct |
42 |
Correct |
3 ms |
2772 KB |
Output is correct |
43 |
Correct |
2 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2704 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2828 KB |
Output is correct |
9 |
Correct |
2 ms |
2836 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2836 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2836 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
2 ms |
2696 KB |
Output is correct |
16 |
Correct |
3 ms |
2708 KB |
Output is correct |
17 |
Correct |
3 ms |
2708 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2832 KB |
Output is correct |
21 |
Correct |
2 ms |
2828 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2696 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
3 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2772 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2704 KB |
Output is correct |
36 |
Correct |
2 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2772 KB |
Output is correct |
38 |
Correct |
2 ms |
2772 KB |
Output is correct |
39 |
Correct |
2 ms |
2772 KB |
Output is correct |
40 |
Correct |
2 ms |
2772 KB |
Output is correct |
41 |
Correct |
3 ms |
2696 KB |
Output is correct |
42 |
Correct |
3 ms |
2772 KB |
Output is correct |
43 |
Correct |
2 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2772 KB |
Output is correct |
45 |
Correct |
4 ms |
2900 KB |
Output is correct |
46 |
Correct |
10 ms |
3368 KB |
Output is correct |
47 |
Correct |
10 ms |
3284 KB |
Output is correct |
48 |
Correct |
9 ms |
3344 KB |
Output is correct |
49 |
Correct |
6 ms |
3732 KB |
Output is correct |
50 |
Correct |
6 ms |
3668 KB |
Output is correct |
51 |
Correct |
6 ms |
3676 KB |
Output is correct |
52 |
Correct |
8 ms |
3540 KB |
Output is correct |
53 |
Correct |
8 ms |
3476 KB |
Output is correct |
54 |
Correct |
9 ms |
3480 KB |
Output is correct |
55 |
Correct |
7 ms |
3552 KB |
Output is correct |
56 |
Correct |
7 ms |
3412 KB |
Output is correct |
57 |
Correct |
16 ms |
3272 KB |
Output is correct |
58 |
Correct |
17 ms |
3352 KB |
Output is correct |
59 |
Correct |
16 ms |
3364 KB |
Output is correct |
60 |
Correct |
18 ms |
3420 KB |
Output is correct |
61 |
Correct |
9 ms |
3412 KB |
Output is correct |
62 |
Correct |
8 ms |
3424 KB |
Output is correct |
63 |
Correct |
9 ms |
3524 KB |
Output is correct |
64 |
Correct |
10 ms |
3332 KB |
Output is correct |
65 |
Correct |
10 ms |
3284 KB |
Output is correct |
66 |
Correct |
10 ms |
3348 KB |
Output is correct |
67 |
Correct |
11 ms |
3340 KB |
Output is correct |
68 |
Correct |
6 ms |
3668 KB |
Output is correct |
69 |
Correct |
8 ms |
3540 KB |
Output is correct |
70 |
Correct |
7 ms |
3472 KB |
Output is correct |
71 |
Correct |
8 ms |
3412 KB |
Output is correct |
72 |
Correct |
16 ms |
3348 KB |
Output is correct |
73 |
Correct |
16 ms |
3348 KB |
Output is correct |
74 |
Correct |
8 ms |
3412 KB |
Output is correct |
75 |
Correct |
8 ms |
3344 KB |
Output is correct |
76 |
Correct |
8 ms |
3284 KB |
Output is correct |
77 |
Correct |
7 ms |
3284 KB |
Output is correct |
78 |
Correct |
7 ms |
3340 KB |
Output is correct |
79 |
Correct |
7 ms |
3384 KB |
Output is correct |
80 |
Correct |
7 ms |
3284 KB |
Output is correct |
81 |
Correct |
9 ms |
3412 KB |
Output is correct |
82 |
Correct |
8 ms |
3348 KB |
Output is correct |
83 |
Correct |
8 ms |
3284 KB |
Output is correct |
84 |
Correct |
9 ms |
3412 KB |
Output is correct |
85 |
Correct |
9 ms |
3344 KB |
Output is correct |
86 |
Correct |
8 ms |
3284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2704 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2828 KB |
Output is correct |
9 |
Correct |
2 ms |
2836 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2836 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2836 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
2 ms |
2696 KB |
Output is correct |
16 |
Correct |
3 ms |
2708 KB |
Output is correct |
17 |
Correct |
3 ms |
2708 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2832 KB |
Output is correct |
21 |
Correct |
2 ms |
2828 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2696 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
3 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2772 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2704 KB |
Output is correct |
36 |
Correct |
2 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2772 KB |
Output is correct |
38 |
Correct |
2 ms |
2772 KB |
Output is correct |
39 |
Correct |
2 ms |
2772 KB |
Output is correct |
40 |
Correct |
2 ms |
2772 KB |
Output is correct |
41 |
Correct |
3 ms |
2696 KB |
Output is correct |
42 |
Correct |
3 ms |
2772 KB |
Output is correct |
43 |
Correct |
2 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2772 KB |
Output is correct |
45 |
Correct |
4 ms |
2900 KB |
Output is correct |
46 |
Correct |
10 ms |
3368 KB |
Output is correct |
47 |
Correct |
10 ms |
3284 KB |
Output is correct |
48 |
Correct |
9 ms |
3344 KB |
Output is correct |
49 |
Correct |
6 ms |
3732 KB |
Output is correct |
50 |
Correct |
6 ms |
3668 KB |
Output is correct |
51 |
Correct |
6 ms |
3676 KB |
Output is correct |
52 |
Correct |
8 ms |
3540 KB |
Output is correct |
53 |
Correct |
8 ms |
3476 KB |
Output is correct |
54 |
Correct |
9 ms |
3480 KB |
Output is correct |
55 |
Correct |
7 ms |
3552 KB |
Output is correct |
56 |
Correct |
7 ms |
3412 KB |
Output is correct |
57 |
Correct |
16 ms |
3272 KB |
Output is correct |
58 |
Correct |
17 ms |
3352 KB |
Output is correct |
59 |
Correct |
16 ms |
3364 KB |
Output is correct |
60 |
Correct |
18 ms |
3420 KB |
Output is correct |
61 |
Correct |
9 ms |
3412 KB |
Output is correct |
62 |
Correct |
8 ms |
3424 KB |
Output is correct |
63 |
Correct |
9 ms |
3524 KB |
Output is correct |
64 |
Correct |
10 ms |
3332 KB |
Output is correct |
65 |
Correct |
10 ms |
3284 KB |
Output is correct |
66 |
Correct |
10 ms |
3348 KB |
Output is correct |
67 |
Correct |
11 ms |
3340 KB |
Output is correct |
68 |
Correct |
6 ms |
3668 KB |
Output is correct |
69 |
Correct |
8 ms |
3540 KB |
Output is correct |
70 |
Correct |
7 ms |
3472 KB |
Output is correct |
71 |
Correct |
8 ms |
3412 KB |
Output is correct |
72 |
Correct |
16 ms |
3348 KB |
Output is correct |
73 |
Correct |
16 ms |
3348 KB |
Output is correct |
74 |
Correct |
8 ms |
3412 KB |
Output is correct |
75 |
Correct |
8 ms |
3344 KB |
Output is correct |
76 |
Correct |
8 ms |
3284 KB |
Output is correct |
77 |
Correct |
7 ms |
3284 KB |
Output is correct |
78 |
Correct |
7 ms |
3340 KB |
Output is correct |
79 |
Correct |
7 ms |
3384 KB |
Output is correct |
80 |
Correct |
7 ms |
3284 KB |
Output is correct |
81 |
Correct |
9 ms |
3412 KB |
Output is correct |
82 |
Correct |
8 ms |
3348 KB |
Output is correct |
83 |
Correct |
8 ms |
3284 KB |
Output is correct |
84 |
Correct |
9 ms |
3412 KB |
Output is correct |
85 |
Correct |
9 ms |
3344 KB |
Output is correct |
86 |
Correct |
8 ms |
3284 KB |
Output is correct |
87 |
Correct |
26 ms |
4308 KB |
Output is correct |
88 |
Correct |
96 ms |
7784 KB |
Output is correct |
89 |
Correct |
426 ms |
19320 KB |
Output is correct |
90 |
Correct |
424 ms |
19424 KB |
Output is correct |
91 |
Correct |
421 ms |
19268 KB |
Output is correct |
92 |
Correct |
205 ms |
28656 KB |
Output is correct |
93 |
Correct |
190 ms |
28532 KB |
Output is correct |
94 |
Correct |
199 ms |
28492 KB |
Output is correct |
95 |
Correct |
273 ms |
23836 KB |
Output is correct |
96 |
Correct |
266 ms |
24244 KB |
Output is correct |
97 |
Correct |
269 ms |
24244 KB |
Output is correct |
98 |
Correct |
294 ms |
24208 KB |
Output is correct |
99 |
Correct |
287 ms |
23156 KB |
Output is correct |
100 |
Correct |
827 ms |
19436 KB |
Output is correct |
101 |
Correct |
857 ms |
19668 KB |
Output is correct |
102 |
Correct |
855 ms |
19660 KB |
Output is correct |
103 |
Correct |
846 ms |
19704 KB |
Output is correct |
104 |
Correct |
344 ms |
23120 KB |
Output is correct |
105 |
Correct |
344 ms |
23112 KB |
Output is correct |
106 |
Correct |
346 ms |
23208 KB |
Output is correct |
107 |
Correct |
392 ms |
18536 KB |
Output is correct |
108 |
Correct |
415 ms |
18552 KB |
Output is correct |
109 |
Correct |
439 ms |
19012 KB |
Output is correct |
110 |
Correct |
189 ms |
27900 KB |
Output is correct |
111 |
Correct |
272 ms |
23852 KB |
Output is correct |
112 |
Correct |
271 ms |
23664 KB |
Output is correct |
113 |
Correct |
271 ms |
22700 KB |
Output is correct |
114 |
Correct |
837 ms |
19512 KB |
Output is correct |
115 |
Correct |
862 ms |
19292 KB |
Output is correct |
116 |
Correct |
350 ms |
22880 KB |
Output is correct |
117 |
Correct |
263 ms |
20892 KB |
Output is correct |
118 |
Correct |
275 ms |
20172 KB |
Output is correct |
119 |
Correct |
250 ms |
19880 KB |
Output is correct |
120 |
Correct |
264 ms |
20632 KB |
Output is correct |
121 |
Correct |
270 ms |
19948 KB |
Output is correct |
122 |
Correct |
265 ms |
19548 KB |
Output is correct |
123 |
Correct |
351 ms |
21056 KB |
Output is correct |
124 |
Correct |
347 ms |
20228 KB |
Output is correct |
125 |
Correct |
334 ms |
19964 KB |
Output is correct |
126 |
Correct |
353 ms |
20564 KB |
Output is correct |
127 |
Correct |
340 ms |
19976 KB |
Output is correct |
128 |
Correct |
344 ms |
19660 KB |
Output is correct |