#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 1e5 + 10;
int par[N], rot[N], sz[N], hv[N], c[N], pos[N], n;
long long t[N];
deque<pii> col[N];
vector<pii> qq;
vector<int> g[N];
int dfs( int u, int p ) {
sz[u] = 1, hv[u] = -1, par[u] = p;
for( int v : g[u] ) if( v != p ) {
int mx = -1;
int now = dfs( v, u );
sz[u] += now;
if( now > mx ) mx = now, hv[u] = v;
}
return sz[u];
}
void hld() {
for( int i = 1, idx = 0; i <= n ; i++ ) if( hv[par[i]] != i )
for( int j = i ; j != -1 ; j = hv[j] )
rot[j] = i, pos[j] = ++idx;
}
void up( int idx, long long val ) { for( int i = idx ; i < N ; i += ( i & -i ) ) t[i] += val; }
long long que( int idx, long long ret = 0 ) {
for( int i = idx ; i > 0 ; i -= i & -i ) ret += t[i];
return ret;
}
long long query( int u ) {
long long ret = 0;
vector<int> coord;
vector<pii> ar;
//printf("U:%d\n",u);
while( u ) {
//printf("%d\n",u);
vector<pii> all;
int sz = pos[u] - pos[rot[u]] + 1;
for( pii i : col[rot[u]] ) {
if( i.y >= sz ) {
all.emplace_back( i.x, sz );
break;
}
else all.emplace_back( i ), sz -= i.y;
}
reverse( all.begin(), all.end() );
for( pii i : all ) ar.emplace_back( i ), coord.emplace_back( i.x );
u = par[rot[u]];
}
memset( t, 0, sizeof t );
sort( coord.begin(), coord.end() );
coord.resize( unique( coord.begin(), coord.end() ) - coord.begin() );
for( pii i : ar ) {
//printf("%d %d\n",ar[i].x,ar[i].y);
int x = lower_bound( coord.begin(), coord.end(), i.x ) - coord.begin() + 1;
ret += que( x - 1 ) * i.y; up( x, i.y );
}
return ret;
}
void update( int u, int c ) {
while( u ) {
int sz = pos[u] - pos[rot[u]] + 1;
while( !col[rot[u]].empty() ) {
if( col[rot[u]].front().y <= sz ) sz -= col[rot[u]].front().y, col[rot[u]].pop_front();
else {
col[rot[u]].front().y -= sz;
break;
}
}
col[rot[u]].emplace_front( c, pos[u] - pos[rot[u]] + 1 );
u = par[rot[u]];
}
}
int main()
{
scanf("%d",&n);
for( int i = 1 ; i <= n ; i++ ) scanf("%d",&c[i]);
for( int i = 1, s, t ; i < n ; i++ ) {
scanf("%d %d",&s,&t);
g[s].emplace_back( t ), g[t].emplace_back( s );
qq.emplace_back( pii( s, t ) );
}
dfs( 1, 0 ), hld();
col[1].emplace_back( pii( c[1], 1 ) );
for( pii i : qq ) {
printf("%lld\n",query( i.x ) );
update( i.y, c[i.y] );
}
return 0;
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
construction.cpp:87:42: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for( int i = 1 ; i <= n ; i++ ) scanf("%d",&c[i]);
~~~~~^~~~~~~~~~~~
construction.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&s,&t);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
70776 KB |
Output is correct |
2 |
Correct |
52 ms |
70852 KB |
Output is correct |
3 |
Correct |
54 ms |
70776 KB |
Output is correct |
4 |
Correct |
62 ms |
70904 KB |
Output is correct |
5 |
Correct |
66 ms |
70904 KB |
Output is correct |
6 |
Correct |
66 ms |
70904 KB |
Output is correct |
7 |
Correct |
65 ms |
71032 KB |
Output is correct |
8 |
Correct |
62 ms |
70904 KB |
Output is correct |
9 |
Correct |
61 ms |
70904 KB |
Output is correct |
10 |
Correct |
61 ms |
70776 KB |
Output is correct |
11 |
Correct |
74 ms |
70904 KB |
Output is correct |
12 |
Correct |
76 ms |
71160 KB |
Output is correct |
13 |
Correct |
75 ms |
71032 KB |
Output is correct |
14 |
Correct |
73 ms |
71032 KB |
Output is correct |
15 |
Correct |
65 ms |
70904 KB |
Output is correct |
16 |
Correct |
71 ms |
70904 KB |
Output is correct |
17 |
Correct |
67 ms |
71032 KB |
Output is correct |
18 |
Correct |
64 ms |
70904 KB |
Output is correct |
19 |
Correct |
76 ms |
70904 KB |
Output is correct |
20 |
Correct |
75 ms |
70904 KB |
Output is correct |
21 |
Correct |
74 ms |
70912 KB |
Output is correct |
22 |
Correct |
66 ms |
70904 KB |
Output is correct |
23 |
Correct |
66 ms |
71032 KB |
Output is correct |
24 |
Correct |
65 ms |
70904 KB |
Output is correct |
25 |
Correct |
65 ms |
70912 KB |
Output is correct |
26 |
Correct |
60 ms |
70776 KB |
Output is correct |
27 |
Correct |
78 ms |
70904 KB |
Output is correct |
28 |
Correct |
75 ms |
70912 KB |
Output is correct |
29 |
Correct |
74 ms |
70912 KB |
Output is correct |
30 |
Correct |
65 ms |
70904 KB |
Output is correct |
31 |
Correct |
65 ms |
70912 KB |
Output is correct |
32 |
Correct |
76 ms |
71032 KB |
Output is correct |
33 |
Correct |
69 ms |
71032 KB |
Output is correct |
34 |
Correct |
66 ms |
70912 KB |
Output is correct |
35 |
Correct |
67 ms |
70904 KB |
Output is correct |
36 |
Correct |
71 ms |
70904 KB |
Output is correct |
37 |
Correct |
67 ms |
71032 KB |
Output is correct |
38 |
Correct |
79 ms |
71128 KB |
Output is correct |
39 |
Correct |
71 ms |
71032 KB |
Output is correct |
40 |
Correct |
69 ms |
70904 KB |
Output is correct |
41 |
Correct |
70 ms |
71008 KB |
Output is correct |
42 |
Correct |
70 ms |
71032 KB |
Output is correct |
43 |
Correct |
68 ms |
70904 KB |
Output is correct |
44 |
Correct |
69 ms |
71032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
70776 KB |
Output is correct |
2 |
Correct |
52 ms |
70852 KB |
Output is correct |
3 |
Correct |
54 ms |
70776 KB |
Output is correct |
4 |
Correct |
62 ms |
70904 KB |
Output is correct |
5 |
Correct |
66 ms |
70904 KB |
Output is correct |
6 |
Correct |
66 ms |
70904 KB |
Output is correct |
7 |
Correct |
65 ms |
71032 KB |
Output is correct |
8 |
Correct |
62 ms |
70904 KB |
Output is correct |
9 |
Correct |
61 ms |
70904 KB |
Output is correct |
10 |
Correct |
61 ms |
70776 KB |
Output is correct |
11 |
Correct |
74 ms |
70904 KB |
Output is correct |
12 |
Correct |
76 ms |
71160 KB |
Output is correct |
13 |
Correct |
75 ms |
71032 KB |
Output is correct |
14 |
Correct |
73 ms |
71032 KB |
Output is correct |
15 |
Correct |
65 ms |
70904 KB |
Output is correct |
16 |
Correct |
71 ms |
70904 KB |
Output is correct |
17 |
Correct |
67 ms |
71032 KB |
Output is correct |
18 |
Correct |
64 ms |
70904 KB |
Output is correct |
19 |
Correct |
76 ms |
70904 KB |
Output is correct |
20 |
Correct |
75 ms |
70904 KB |
Output is correct |
21 |
Correct |
74 ms |
70912 KB |
Output is correct |
22 |
Correct |
66 ms |
70904 KB |
Output is correct |
23 |
Correct |
66 ms |
71032 KB |
Output is correct |
24 |
Correct |
65 ms |
70904 KB |
Output is correct |
25 |
Correct |
65 ms |
70912 KB |
Output is correct |
26 |
Correct |
60 ms |
70776 KB |
Output is correct |
27 |
Correct |
78 ms |
70904 KB |
Output is correct |
28 |
Correct |
75 ms |
70912 KB |
Output is correct |
29 |
Correct |
74 ms |
70912 KB |
Output is correct |
30 |
Correct |
65 ms |
70904 KB |
Output is correct |
31 |
Correct |
65 ms |
70912 KB |
Output is correct |
32 |
Correct |
76 ms |
71032 KB |
Output is correct |
33 |
Correct |
69 ms |
71032 KB |
Output is correct |
34 |
Correct |
66 ms |
70912 KB |
Output is correct |
35 |
Correct |
67 ms |
70904 KB |
Output is correct |
36 |
Correct |
71 ms |
70904 KB |
Output is correct |
37 |
Correct |
67 ms |
71032 KB |
Output is correct |
38 |
Correct |
79 ms |
71128 KB |
Output is correct |
39 |
Correct |
71 ms |
71032 KB |
Output is correct |
40 |
Correct |
69 ms |
70904 KB |
Output is correct |
41 |
Correct |
70 ms |
71008 KB |
Output is correct |
42 |
Correct |
70 ms |
71032 KB |
Output is correct |
43 |
Correct |
68 ms |
70904 KB |
Output is correct |
44 |
Correct |
69 ms |
71032 KB |
Output is correct |
45 |
Correct |
81 ms |
71036 KB |
Output is correct |
46 |
Correct |
169 ms |
72056 KB |
Output is correct |
47 |
Correct |
160 ms |
72184 KB |
Output is correct |
48 |
Correct |
168 ms |
72156 KB |
Output is correct |
49 |
Correct |
141 ms |
71288 KB |
Output is correct |
50 |
Correct |
144 ms |
71352 KB |
Output is correct |
51 |
Correct |
144 ms |
71296 KB |
Output is correct |
52 |
Correct |
789 ms |
72420 KB |
Output is correct |
53 |
Correct |
792 ms |
72196 KB |
Output is correct |
54 |
Correct |
774 ms |
72184 KB |
Output is correct |
55 |
Correct |
767 ms |
72396 KB |
Output is correct |
56 |
Correct |
710 ms |
72184 KB |
Output is correct |
57 |
Correct |
161 ms |
72056 KB |
Output is correct |
58 |
Correct |
159 ms |
72104 KB |
Output is correct |
59 |
Correct |
161 ms |
72056 KB |
Output is correct |
60 |
Correct |
160 ms |
72056 KB |
Output is correct |
61 |
Correct |
770 ms |
72416 KB |
Output is correct |
62 |
Correct |
745 ms |
72312 KB |
Output is correct |
63 |
Correct |
753 ms |
72292 KB |
Output is correct |
64 |
Correct |
173 ms |
72116 KB |
Output is correct |
65 |
Correct |
156 ms |
72184 KB |
Output is correct |
66 |
Correct |
156 ms |
72056 KB |
Output is correct |
67 |
Correct |
159 ms |
72056 KB |
Output is correct |
68 |
Correct |
158 ms |
71288 KB |
Output is correct |
69 |
Correct |
809 ms |
72188 KB |
Output is correct |
70 |
Correct |
760 ms |
72312 KB |
Output is correct |
71 |
Correct |
679 ms |
72312 KB |
Output is correct |
72 |
Correct |
169 ms |
72056 KB |
Output is correct |
73 |
Correct |
167 ms |
72192 KB |
Output is correct |
74 |
Correct |
759 ms |
72312 KB |
Output is correct |
75 |
Correct |
433 ms |
72184 KB |
Output is correct |
76 |
Correct |
361 ms |
72300 KB |
Output is correct |
77 |
Correct |
347 ms |
72056 KB |
Output is correct |
78 |
Correct |
456 ms |
72276 KB |
Output is correct |
79 |
Correct |
341 ms |
72184 KB |
Output is correct |
80 |
Correct |
315 ms |
72184 KB |
Output is correct |
81 |
Correct |
464 ms |
72184 KB |
Output is correct |
82 |
Correct |
374 ms |
72312 KB |
Output is correct |
83 |
Correct |
315 ms |
72184 KB |
Output is correct |
84 |
Correct |
465 ms |
72312 KB |
Output is correct |
85 |
Correct |
363 ms |
72192 KB |
Output is correct |
86 |
Correct |
318 ms |
72296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
70776 KB |
Output is correct |
2 |
Correct |
52 ms |
70852 KB |
Output is correct |
3 |
Correct |
54 ms |
70776 KB |
Output is correct |
4 |
Correct |
62 ms |
70904 KB |
Output is correct |
5 |
Correct |
66 ms |
70904 KB |
Output is correct |
6 |
Correct |
66 ms |
70904 KB |
Output is correct |
7 |
Correct |
65 ms |
71032 KB |
Output is correct |
8 |
Correct |
62 ms |
70904 KB |
Output is correct |
9 |
Correct |
61 ms |
70904 KB |
Output is correct |
10 |
Correct |
61 ms |
70776 KB |
Output is correct |
11 |
Correct |
74 ms |
70904 KB |
Output is correct |
12 |
Correct |
76 ms |
71160 KB |
Output is correct |
13 |
Correct |
75 ms |
71032 KB |
Output is correct |
14 |
Correct |
73 ms |
71032 KB |
Output is correct |
15 |
Correct |
65 ms |
70904 KB |
Output is correct |
16 |
Correct |
71 ms |
70904 KB |
Output is correct |
17 |
Correct |
67 ms |
71032 KB |
Output is correct |
18 |
Correct |
64 ms |
70904 KB |
Output is correct |
19 |
Correct |
76 ms |
70904 KB |
Output is correct |
20 |
Correct |
75 ms |
70904 KB |
Output is correct |
21 |
Correct |
74 ms |
70912 KB |
Output is correct |
22 |
Correct |
66 ms |
70904 KB |
Output is correct |
23 |
Correct |
66 ms |
71032 KB |
Output is correct |
24 |
Correct |
65 ms |
70904 KB |
Output is correct |
25 |
Correct |
65 ms |
70912 KB |
Output is correct |
26 |
Correct |
60 ms |
70776 KB |
Output is correct |
27 |
Correct |
78 ms |
70904 KB |
Output is correct |
28 |
Correct |
75 ms |
70912 KB |
Output is correct |
29 |
Correct |
74 ms |
70912 KB |
Output is correct |
30 |
Correct |
65 ms |
70904 KB |
Output is correct |
31 |
Correct |
65 ms |
70912 KB |
Output is correct |
32 |
Correct |
76 ms |
71032 KB |
Output is correct |
33 |
Correct |
69 ms |
71032 KB |
Output is correct |
34 |
Correct |
66 ms |
70912 KB |
Output is correct |
35 |
Correct |
67 ms |
70904 KB |
Output is correct |
36 |
Correct |
71 ms |
70904 KB |
Output is correct |
37 |
Correct |
67 ms |
71032 KB |
Output is correct |
38 |
Correct |
79 ms |
71128 KB |
Output is correct |
39 |
Correct |
71 ms |
71032 KB |
Output is correct |
40 |
Correct |
69 ms |
70904 KB |
Output is correct |
41 |
Correct |
70 ms |
71008 KB |
Output is correct |
42 |
Correct |
70 ms |
71032 KB |
Output is correct |
43 |
Correct |
68 ms |
70904 KB |
Output is correct |
44 |
Correct |
69 ms |
71032 KB |
Output is correct |
45 |
Correct |
81 ms |
71036 KB |
Output is correct |
46 |
Correct |
169 ms |
72056 KB |
Output is correct |
47 |
Correct |
160 ms |
72184 KB |
Output is correct |
48 |
Correct |
168 ms |
72156 KB |
Output is correct |
49 |
Correct |
141 ms |
71288 KB |
Output is correct |
50 |
Correct |
144 ms |
71352 KB |
Output is correct |
51 |
Correct |
144 ms |
71296 KB |
Output is correct |
52 |
Correct |
789 ms |
72420 KB |
Output is correct |
53 |
Correct |
792 ms |
72196 KB |
Output is correct |
54 |
Correct |
774 ms |
72184 KB |
Output is correct |
55 |
Correct |
767 ms |
72396 KB |
Output is correct |
56 |
Correct |
710 ms |
72184 KB |
Output is correct |
57 |
Correct |
161 ms |
72056 KB |
Output is correct |
58 |
Correct |
159 ms |
72104 KB |
Output is correct |
59 |
Correct |
161 ms |
72056 KB |
Output is correct |
60 |
Correct |
160 ms |
72056 KB |
Output is correct |
61 |
Correct |
770 ms |
72416 KB |
Output is correct |
62 |
Correct |
745 ms |
72312 KB |
Output is correct |
63 |
Correct |
753 ms |
72292 KB |
Output is correct |
64 |
Correct |
173 ms |
72116 KB |
Output is correct |
65 |
Correct |
156 ms |
72184 KB |
Output is correct |
66 |
Correct |
156 ms |
72056 KB |
Output is correct |
67 |
Correct |
159 ms |
72056 KB |
Output is correct |
68 |
Correct |
158 ms |
71288 KB |
Output is correct |
69 |
Correct |
809 ms |
72188 KB |
Output is correct |
70 |
Correct |
760 ms |
72312 KB |
Output is correct |
71 |
Correct |
679 ms |
72312 KB |
Output is correct |
72 |
Correct |
169 ms |
72056 KB |
Output is correct |
73 |
Correct |
167 ms |
72192 KB |
Output is correct |
74 |
Correct |
759 ms |
72312 KB |
Output is correct |
75 |
Correct |
433 ms |
72184 KB |
Output is correct |
76 |
Correct |
361 ms |
72300 KB |
Output is correct |
77 |
Correct |
347 ms |
72056 KB |
Output is correct |
78 |
Correct |
456 ms |
72276 KB |
Output is correct |
79 |
Correct |
341 ms |
72184 KB |
Output is correct |
80 |
Correct |
315 ms |
72184 KB |
Output is correct |
81 |
Correct |
464 ms |
72184 KB |
Output is correct |
82 |
Correct |
374 ms |
72312 KB |
Output is correct |
83 |
Correct |
315 ms |
72184 KB |
Output is correct |
84 |
Correct |
465 ms |
72312 KB |
Output is correct |
85 |
Correct |
363 ms |
72192 KB |
Output is correct |
86 |
Correct |
318 ms |
72296 KB |
Output is correct |
87 |
Correct |
318 ms |
74104 KB |
Output is correct |
88 |
Correct |
877 ms |
80628 KB |
Output is correct |
89 |
Execution timed out |
2081 ms |
100952 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |