#include<bits/stdc++.h>
using namespace std ;
#define MAXN 100007
#define LOG 17
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 ;
}
# |
Verdict |
Execution time |
Memory |
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 |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
3 ms |
2788 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
2832 KB |
Output is correct |
10 |
Correct |
3 ms |
2776 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
3 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
3 ms |
2756 KB |
Output is correct |
24 |
Correct |
3 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 |
2 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 |
2772 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 |
2 ms |
2772 KB |
Output is correct |
42 |
Correct |
2 ms |
2772 KB |
Output is correct |
43 |
Correct |
3 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
3 ms |
2788 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
2832 KB |
Output is correct |
10 |
Correct |
3 ms |
2776 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
3 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
3 ms |
2756 KB |
Output is correct |
24 |
Correct |
3 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 |
2 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 |
2772 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 |
2 ms |
2772 KB |
Output is correct |
42 |
Correct |
2 ms |
2772 KB |
Output is correct |
43 |
Correct |
3 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2772 KB |
Output is correct |
45 |
Correct |
3 ms |
2772 KB |
Output is correct |
46 |
Correct |
10 ms |
3232 KB |
Output is correct |
47 |
Correct |
11 ms |
3156 KB |
Output is correct |
48 |
Correct |
10 ms |
3232 KB |
Output is correct |
49 |
Correct |
6 ms |
3540 KB |
Output is correct |
50 |
Correct |
6 ms |
3540 KB |
Output is correct |
51 |
Correct |
6 ms |
3596 KB |
Output is correct |
52 |
Correct |
6 ms |
3412 KB |
Output is correct |
53 |
Correct |
7 ms |
3396 KB |
Output is correct |
54 |
Correct |
7 ms |
3384 KB |
Output is correct |
55 |
Correct |
8 ms |
3412 KB |
Output is correct |
56 |
Correct |
8 ms |
3384 KB |
Output is correct |
57 |
Correct |
15 ms |
3228 KB |
Output is correct |
58 |
Correct |
17 ms |
3248 KB |
Output is correct |
59 |
Correct |
18 ms |
3156 KB |
Output is correct |
60 |
Correct |
22 ms |
3224 KB |
Output is correct |
61 |
Correct |
9 ms |
3284 KB |
Output is correct |
62 |
Correct |
9 ms |
3284 KB |
Output is correct |
63 |
Correct |
8 ms |
3284 KB |
Output is correct |
64 |
Correct |
9 ms |
3156 KB |
Output is correct |
65 |
Correct |
10 ms |
3232 KB |
Output is correct |
66 |
Correct |
10 ms |
3156 KB |
Output is correct |
67 |
Correct |
14 ms |
3236 KB |
Output is correct |
68 |
Correct |
6 ms |
3540 KB |
Output is correct |
69 |
Correct |
7 ms |
3412 KB |
Output is correct |
70 |
Correct |
8 ms |
3412 KB |
Output is correct |
71 |
Correct |
10 ms |
3284 KB |
Output is correct |
72 |
Correct |
17 ms |
3244 KB |
Output is correct |
73 |
Correct |
20 ms |
3228 KB |
Output is correct |
74 |
Correct |
8 ms |
3284 KB |
Output is correct |
75 |
Correct |
7 ms |
3284 KB |
Output is correct |
76 |
Correct |
7 ms |
3284 KB |
Output is correct |
77 |
Correct |
7 ms |
3156 KB |
Output is correct |
78 |
Correct |
7 ms |
3284 KB |
Output is correct |
79 |
Correct |
7 ms |
3276 KB |
Output is correct |
80 |
Correct |
8 ms |
3268 KB |
Output is correct |
81 |
Correct |
8 ms |
3300 KB |
Output is correct |
82 |
Correct |
8 ms |
3284 KB |
Output is correct |
83 |
Correct |
9 ms |
3156 KB |
Output is correct |
84 |
Correct |
9 ms |
3284 KB |
Output is correct |
85 |
Correct |
11 ms |
3284 KB |
Output is correct |
86 |
Correct |
9 ms |
3156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
3 ms |
2788 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
2832 KB |
Output is correct |
10 |
Correct |
3 ms |
2776 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
3 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
3 ms |
2756 KB |
Output is correct |
24 |
Correct |
3 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 |
2 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 |
2772 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 |
2 ms |
2772 KB |
Output is correct |
42 |
Correct |
2 ms |
2772 KB |
Output is correct |
43 |
Correct |
3 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2772 KB |
Output is correct |
45 |
Correct |
3 ms |
2772 KB |
Output is correct |
46 |
Correct |
10 ms |
3232 KB |
Output is correct |
47 |
Correct |
11 ms |
3156 KB |
Output is correct |
48 |
Correct |
10 ms |
3232 KB |
Output is correct |
49 |
Correct |
6 ms |
3540 KB |
Output is correct |
50 |
Correct |
6 ms |
3540 KB |
Output is correct |
51 |
Correct |
6 ms |
3596 KB |
Output is correct |
52 |
Correct |
6 ms |
3412 KB |
Output is correct |
53 |
Correct |
7 ms |
3396 KB |
Output is correct |
54 |
Correct |
7 ms |
3384 KB |
Output is correct |
55 |
Correct |
8 ms |
3412 KB |
Output is correct |
56 |
Correct |
8 ms |
3384 KB |
Output is correct |
57 |
Correct |
15 ms |
3228 KB |
Output is correct |
58 |
Correct |
17 ms |
3248 KB |
Output is correct |
59 |
Correct |
18 ms |
3156 KB |
Output is correct |
60 |
Correct |
22 ms |
3224 KB |
Output is correct |
61 |
Correct |
9 ms |
3284 KB |
Output is correct |
62 |
Correct |
9 ms |
3284 KB |
Output is correct |
63 |
Correct |
8 ms |
3284 KB |
Output is correct |
64 |
Correct |
9 ms |
3156 KB |
Output is correct |
65 |
Correct |
10 ms |
3232 KB |
Output is correct |
66 |
Correct |
10 ms |
3156 KB |
Output is correct |
67 |
Correct |
14 ms |
3236 KB |
Output is correct |
68 |
Correct |
6 ms |
3540 KB |
Output is correct |
69 |
Correct |
7 ms |
3412 KB |
Output is correct |
70 |
Correct |
8 ms |
3412 KB |
Output is correct |
71 |
Correct |
10 ms |
3284 KB |
Output is correct |
72 |
Correct |
17 ms |
3244 KB |
Output is correct |
73 |
Correct |
20 ms |
3228 KB |
Output is correct |
74 |
Correct |
8 ms |
3284 KB |
Output is correct |
75 |
Correct |
7 ms |
3284 KB |
Output is correct |
76 |
Correct |
7 ms |
3284 KB |
Output is correct |
77 |
Correct |
7 ms |
3156 KB |
Output is correct |
78 |
Correct |
7 ms |
3284 KB |
Output is correct |
79 |
Correct |
7 ms |
3276 KB |
Output is correct |
80 |
Correct |
8 ms |
3268 KB |
Output is correct |
81 |
Correct |
8 ms |
3300 KB |
Output is correct |
82 |
Correct |
8 ms |
3284 KB |
Output is correct |
83 |
Correct |
9 ms |
3156 KB |
Output is correct |
84 |
Correct |
9 ms |
3284 KB |
Output is correct |
85 |
Correct |
11 ms |
3284 KB |
Output is correct |
86 |
Correct |
9 ms |
3156 KB |
Output is correct |
87 |
Correct |
36 ms |
4100 KB |
Output is correct |
88 |
Correct |
86 ms |
6536 KB |
Output is correct |
89 |
Correct |
441 ms |
15976 KB |
Output is correct |
90 |
Correct |
406 ms |
16008 KB |
Output is correct |
91 |
Correct |
470 ms |
16000 KB |
Output is correct |
92 |
Correct |
192 ms |
25276 KB |
Output is correct |
93 |
Correct |
216 ms |
25204 KB |
Output is correct |
94 |
Correct |
181 ms |
25256 KB |
Output is correct |
95 |
Correct |
285 ms |
20904 KB |
Output is correct |
96 |
Correct |
260 ms |
20912 KB |
Output is correct |
97 |
Correct |
257 ms |
20920 KB |
Output is correct |
98 |
Correct |
291 ms |
20940 KB |
Output is correct |
99 |
Correct |
266 ms |
19972 KB |
Output is correct |
100 |
Correct |
872 ms |
16720 KB |
Output is correct |
101 |
Correct |
885 ms |
16588 KB |
Output is correct |
102 |
Correct |
846 ms |
16544 KB |
Output is correct |
103 |
Correct |
855 ms |
16516 KB |
Output is correct |
104 |
Correct |
356 ms |
19828 KB |
Output is correct |
105 |
Correct |
368 ms |
19960 KB |
Output is correct |
106 |
Correct |
334 ms |
19916 KB |
Output is correct |
107 |
Correct |
454 ms |
15904 KB |
Output is correct |
108 |
Correct |
394 ms |
16076 KB |
Output is correct |
109 |
Correct |
474 ms |
16020 KB |
Output is correct |
110 |
Correct |
174 ms |
25360 KB |
Output is correct |
111 |
Correct |
303 ms |
20924 KB |
Output is correct |
112 |
Correct |
258 ms |
20972 KB |
Output is correct |
113 |
Correct |
281 ms |
19828 KB |
Output is correct |
114 |
Correct |
836 ms |
16576 KB |
Output is correct |
115 |
Correct |
891 ms |
16564 KB |
Output is correct |
116 |
Correct |
376 ms |
19888 KB |
Output is correct |
117 |
Correct |
259 ms |
17864 KB |
Output is correct |
118 |
Correct |
288 ms |
17172 KB |
Output is correct |
119 |
Correct |
252 ms |
16920 KB |
Output is correct |
120 |
Correct |
313 ms |
17884 KB |
Output is correct |
121 |
Correct |
243 ms |
17208 KB |
Output is correct |
122 |
Correct |
267 ms |
16976 KB |
Output is correct |
123 |
Correct |
348 ms |
17944 KB |
Output is correct |
124 |
Correct |
371 ms |
17412 KB |
Output is correct |
125 |
Correct |
347 ms |
16976 KB |
Output is correct |
126 |
Correct |
345 ms |
17980 KB |
Output is correct |
127 |
Correct |
370 ms |
17284 KB |
Output is correct |
128 |
Correct |
334 ms |
17008 KB |
Output is correct |