#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;
int mx = -1;
for( int v : g[u] ) if( v != p ) {
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;
}
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]];
}
sort( coord.begin(), coord.end() );
coord.resize( unique( coord.begin(), coord.end() ) - coord.begin() );
vector<long long> t( coord.size() + 10, 0 );
for( pii i : ar ) {
//printf("%d %d\n",i.x,i.y);
long long sum = 0;
int x = lower_bound( coord.begin(), coord.end(), i.x ) - coord.begin() + 1;
for( int j = x-1 ; j > 0 ; j -= j & -j ) sum += t[j];
ret += sum * i.y;
for( int j = x ; j < ( int )coord.size() + 10 ; j += j & -j ) t[j] += 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:83:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
construction.cpp:84: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:86: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 |
53 ms |
69992 KB |
Output is correct |
2 |
Correct |
56 ms |
70016 KB |
Output is correct |
3 |
Correct |
58 ms |
70016 KB |
Output is correct |
4 |
Correct |
56 ms |
70136 KB |
Output is correct |
5 |
Correct |
59 ms |
70272 KB |
Output is correct |
6 |
Correct |
62 ms |
70196 KB |
Output is correct |
7 |
Correct |
58 ms |
70144 KB |
Output is correct |
8 |
Correct |
66 ms |
70136 KB |
Output is correct |
9 |
Correct |
62 ms |
70144 KB |
Output is correct |
10 |
Correct |
55 ms |
70144 KB |
Output is correct |
11 |
Correct |
56 ms |
70136 KB |
Output is correct |
12 |
Correct |
63 ms |
70136 KB |
Output is correct |
13 |
Correct |
57 ms |
70144 KB |
Output is correct |
14 |
Correct |
66 ms |
70272 KB |
Output is correct |
15 |
Correct |
60 ms |
70232 KB |
Output is correct |
16 |
Correct |
55 ms |
70264 KB |
Output is correct |
17 |
Correct |
56 ms |
70264 KB |
Output is correct |
18 |
Correct |
57 ms |
70144 KB |
Output is correct |
19 |
Correct |
65 ms |
70148 KB |
Output is correct |
20 |
Correct |
55 ms |
70368 KB |
Output is correct |
21 |
Correct |
54 ms |
70144 KB |
Output is correct |
22 |
Correct |
54 ms |
70140 KB |
Output is correct |
23 |
Correct |
54 ms |
70136 KB |
Output is correct |
24 |
Correct |
55 ms |
70136 KB |
Output is correct |
25 |
Correct |
66 ms |
70136 KB |
Output is correct |
26 |
Correct |
54 ms |
70016 KB |
Output is correct |
27 |
Correct |
53 ms |
70136 KB |
Output is correct |
28 |
Correct |
56 ms |
70136 KB |
Output is correct |
29 |
Correct |
57 ms |
70136 KB |
Output is correct |
30 |
Correct |
55 ms |
70136 KB |
Output is correct |
31 |
Correct |
64 ms |
70112 KB |
Output is correct |
32 |
Correct |
57 ms |
70264 KB |
Output is correct |
33 |
Correct |
55 ms |
70264 KB |
Output is correct |
34 |
Correct |
55 ms |
70144 KB |
Output is correct |
35 |
Correct |
55 ms |
70140 KB |
Output is correct |
36 |
Correct |
56 ms |
70144 KB |
Output is correct |
37 |
Correct |
54 ms |
70144 KB |
Output is correct |
38 |
Correct |
56 ms |
70144 KB |
Output is correct |
39 |
Correct |
54 ms |
70264 KB |
Output is correct |
40 |
Correct |
54 ms |
70228 KB |
Output is correct |
41 |
Correct |
56 ms |
70136 KB |
Output is correct |
42 |
Correct |
55 ms |
70272 KB |
Output is correct |
43 |
Correct |
55 ms |
70136 KB |
Output is correct |
44 |
Correct |
55 ms |
70136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
69992 KB |
Output is correct |
2 |
Correct |
56 ms |
70016 KB |
Output is correct |
3 |
Correct |
58 ms |
70016 KB |
Output is correct |
4 |
Correct |
56 ms |
70136 KB |
Output is correct |
5 |
Correct |
59 ms |
70272 KB |
Output is correct |
6 |
Correct |
62 ms |
70196 KB |
Output is correct |
7 |
Correct |
58 ms |
70144 KB |
Output is correct |
8 |
Correct |
66 ms |
70136 KB |
Output is correct |
9 |
Correct |
62 ms |
70144 KB |
Output is correct |
10 |
Correct |
55 ms |
70144 KB |
Output is correct |
11 |
Correct |
56 ms |
70136 KB |
Output is correct |
12 |
Correct |
63 ms |
70136 KB |
Output is correct |
13 |
Correct |
57 ms |
70144 KB |
Output is correct |
14 |
Correct |
66 ms |
70272 KB |
Output is correct |
15 |
Correct |
60 ms |
70232 KB |
Output is correct |
16 |
Correct |
55 ms |
70264 KB |
Output is correct |
17 |
Correct |
56 ms |
70264 KB |
Output is correct |
18 |
Correct |
57 ms |
70144 KB |
Output is correct |
19 |
Correct |
65 ms |
70148 KB |
Output is correct |
20 |
Correct |
55 ms |
70368 KB |
Output is correct |
21 |
Correct |
54 ms |
70144 KB |
Output is correct |
22 |
Correct |
54 ms |
70140 KB |
Output is correct |
23 |
Correct |
54 ms |
70136 KB |
Output is correct |
24 |
Correct |
55 ms |
70136 KB |
Output is correct |
25 |
Correct |
66 ms |
70136 KB |
Output is correct |
26 |
Correct |
54 ms |
70016 KB |
Output is correct |
27 |
Correct |
53 ms |
70136 KB |
Output is correct |
28 |
Correct |
56 ms |
70136 KB |
Output is correct |
29 |
Correct |
57 ms |
70136 KB |
Output is correct |
30 |
Correct |
55 ms |
70136 KB |
Output is correct |
31 |
Correct |
64 ms |
70112 KB |
Output is correct |
32 |
Correct |
57 ms |
70264 KB |
Output is correct |
33 |
Correct |
55 ms |
70264 KB |
Output is correct |
34 |
Correct |
55 ms |
70144 KB |
Output is correct |
35 |
Correct |
55 ms |
70140 KB |
Output is correct |
36 |
Correct |
56 ms |
70144 KB |
Output is correct |
37 |
Correct |
54 ms |
70144 KB |
Output is correct |
38 |
Correct |
56 ms |
70144 KB |
Output is correct |
39 |
Correct |
54 ms |
70264 KB |
Output is correct |
40 |
Correct |
54 ms |
70228 KB |
Output is correct |
41 |
Correct |
56 ms |
70136 KB |
Output is correct |
42 |
Correct |
55 ms |
70272 KB |
Output is correct |
43 |
Correct |
55 ms |
70136 KB |
Output is correct |
44 |
Correct |
55 ms |
70136 KB |
Output is correct |
45 |
Correct |
57 ms |
70440 KB |
Output is correct |
46 |
Correct |
60 ms |
71288 KB |
Output is correct |
47 |
Correct |
62 ms |
71288 KB |
Output is correct |
48 |
Correct |
63 ms |
71288 KB |
Output is correct |
49 |
Correct |
61 ms |
70556 KB |
Output is correct |
50 |
Correct |
54 ms |
70648 KB |
Output is correct |
51 |
Correct |
57 ms |
70776 KB |
Output is correct |
52 |
Correct |
59 ms |
71544 KB |
Output is correct |
53 |
Correct |
59 ms |
71548 KB |
Output is correct |
54 |
Correct |
59 ms |
71544 KB |
Output is correct |
55 |
Correct |
59 ms |
71544 KB |
Output is correct |
56 |
Correct |
60 ms |
71416 KB |
Output is correct |
57 |
Correct |
67 ms |
71288 KB |
Output is correct |
58 |
Correct |
67 ms |
71288 KB |
Output is correct |
59 |
Correct |
70 ms |
71288 KB |
Output is correct |
60 |
Correct |
66 ms |
71288 KB |
Output is correct |
61 |
Correct |
61 ms |
71416 KB |
Output is correct |
62 |
Correct |
61 ms |
71424 KB |
Output is correct |
63 |
Correct |
59 ms |
71416 KB |
Output is correct |
64 |
Correct |
62 ms |
71416 KB |
Output is correct |
65 |
Correct |
63 ms |
71288 KB |
Output is correct |
66 |
Correct |
62 ms |
71288 KB |
Output is correct |
67 |
Correct |
63 ms |
71296 KB |
Output is correct |
68 |
Correct |
54 ms |
70648 KB |
Output is correct |
69 |
Correct |
58 ms |
71576 KB |
Output is correct |
70 |
Correct |
59 ms |
71544 KB |
Output is correct |
71 |
Correct |
68 ms |
71416 KB |
Output is correct |
72 |
Correct |
66 ms |
71288 KB |
Output is correct |
73 |
Correct |
70 ms |
71416 KB |
Output is correct |
74 |
Correct |
61 ms |
71544 KB |
Output is correct |
75 |
Correct |
59 ms |
71416 KB |
Output is correct |
76 |
Correct |
61 ms |
71416 KB |
Output is correct |
77 |
Correct |
60 ms |
71416 KB |
Output is correct |
78 |
Correct |
60 ms |
71416 KB |
Output is correct |
79 |
Correct |
65 ms |
71416 KB |
Output is correct |
80 |
Correct |
60 ms |
71296 KB |
Output is correct |
81 |
Correct |
61 ms |
71416 KB |
Output is correct |
82 |
Correct |
60 ms |
71416 KB |
Output is correct |
83 |
Correct |
60 ms |
71288 KB |
Output is correct |
84 |
Correct |
61 ms |
71416 KB |
Output is correct |
85 |
Correct |
60 ms |
71416 KB |
Output is correct |
86 |
Correct |
60 ms |
71288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
69992 KB |
Output is correct |
2 |
Correct |
56 ms |
70016 KB |
Output is correct |
3 |
Correct |
58 ms |
70016 KB |
Output is correct |
4 |
Correct |
56 ms |
70136 KB |
Output is correct |
5 |
Correct |
59 ms |
70272 KB |
Output is correct |
6 |
Correct |
62 ms |
70196 KB |
Output is correct |
7 |
Correct |
58 ms |
70144 KB |
Output is correct |
8 |
Correct |
66 ms |
70136 KB |
Output is correct |
9 |
Correct |
62 ms |
70144 KB |
Output is correct |
10 |
Correct |
55 ms |
70144 KB |
Output is correct |
11 |
Correct |
56 ms |
70136 KB |
Output is correct |
12 |
Correct |
63 ms |
70136 KB |
Output is correct |
13 |
Correct |
57 ms |
70144 KB |
Output is correct |
14 |
Correct |
66 ms |
70272 KB |
Output is correct |
15 |
Correct |
60 ms |
70232 KB |
Output is correct |
16 |
Correct |
55 ms |
70264 KB |
Output is correct |
17 |
Correct |
56 ms |
70264 KB |
Output is correct |
18 |
Correct |
57 ms |
70144 KB |
Output is correct |
19 |
Correct |
65 ms |
70148 KB |
Output is correct |
20 |
Correct |
55 ms |
70368 KB |
Output is correct |
21 |
Correct |
54 ms |
70144 KB |
Output is correct |
22 |
Correct |
54 ms |
70140 KB |
Output is correct |
23 |
Correct |
54 ms |
70136 KB |
Output is correct |
24 |
Correct |
55 ms |
70136 KB |
Output is correct |
25 |
Correct |
66 ms |
70136 KB |
Output is correct |
26 |
Correct |
54 ms |
70016 KB |
Output is correct |
27 |
Correct |
53 ms |
70136 KB |
Output is correct |
28 |
Correct |
56 ms |
70136 KB |
Output is correct |
29 |
Correct |
57 ms |
70136 KB |
Output is correct |
30 |
Correct |
55 ms |
70136 KB |
Output is correct |
31 |
Correct |
64 ms |
70112 KB |
Output is correct |
32 |
Correct |
57 ms |
70264 KB |
Output is correct |
33 |
Correct |
55 ms |
70264 KB |
Output is correct |
34 |
Correct |
55 ms |
70144 KB |
Output is correct |
35 |
Correct |
55 ms |
70140 KB |
Output is correct |
36 |
Correct |
56 ms |
70144 KB |
Output is correct |
37 |
Correct |
54 ms |
70144 KB |
Output is correct |
38 |
Correct |
56 ms |
70144 KB |
Output is correct |
39 |
Correct |
54 ms |
70264 KB |
Output is correct |
40 |
Correct |
54 ms |
70228 KB |
Output is correct |
41 |
Correct |
56 ms |
70136 KB |
Output is correct |
42 |
Correct |
55 ms |
70272 KB |
Output is correct |
43 |
Correct |
55 ms |
70136 KB |
Output is correct |
44 |
Correct |
55 ms |
70136 KB |
Output is correct |
45 |
Correct |
57 ms |
70440 KB |
Output is correct |
46 |
Correct |
60 ms |
71288 KB |
Output is correct |
47 |
Correct |
62 ms |
71288 KB |
Output is correct |
48 |
Correct |
63 ms |
71288 KB |
Output is correct |
49 |
Correct |
61 ms |
70556 KB |
Output is correct |
50 |
Correct |
54 ms |
70648 KB |
Output is correct |
51 |
Correct |
57 ms |
70776 KB |
Output is correct |
52 |
Correct |
59 ms |
71544 KB |
Output is correct |
53 |
Correct |
59 ms |
71548 KB |
Output is correct |
54 |
Correct |
59 ms |
71544 KB |
Output is correct |
55 |
Correct |
59 ms |
71544 KB |
Output is correct |
56 |
Correct |
60 ms |
71416 KB |
Output is correct |
57 |
Correct |
67 ms |
71288 KB |
Output is correct |
58 |
Correct |
67 ms |
71288 KB |
Output is correct |
59 |
Correct |
70 ms |
71288 KB |
Output is correct |
60 |
Correct |
66 ms |
71288 KB |
Output is correct |
61 |
Correct |
61 ms |
71416 KB |
Output is correct |
62 |
Correct |
61 ms |
71424 KB |
Output is correct |
63 |
Correct |
59 ms |
71416 KB |
Output is correct |
64 |
Correct |
62 ms |
71416 KB |
Output is correct |
65 |
Correct |
63 ms |
71288 KB |
Output is correct |
66 |
Correct |
62 ms |
71288 KB |
Output is correct |
67 |
Correct |
63 ms |
71296 KB |
Output is correct |
68 |
Correct |
54 ms |
70648 KB |
Output is correct |
69 |
Correct |
58 ms |
71576 KB |
Output is correct |
70 |
Correct |
59 ms |
71544 KB |
Output is correct |
71 |
Correct |
68 ms |
71416 KB |
Output is correct |
72 |
Correct |
66 ms |
71288 KB |
Output is correct |
73 |
Correct |
70 ms |
71416 KB |
Output is correct |
74 |
Correct |
61 ms |
71544 KB |
Output is correct |
75 |
Correct |
59 ms |
71416 KB |
Output is correct |
76 |
Correct |
61 ms |
71416 KB |
Output is correct |
77 |
Correct |
60 ms |
71416 KB |
Output is correct |
78 |
Correct |
60 ms |
71416 KB |
Output is correct |
79 |
Correct |
65 ms |
71416 KB |
Output is correct |
80 |
Correct |
60 ms |
71296 KB |
Output is correct |
81 |
Correct |
61 ms |
71416 KB |
Output is correct |
82 |
Correct |
60 ms |
71416 KB |
Output is correct |
83 |
Correct |
60 ms |
71288 KB |
Output is correct |
84 |
Correct |
61 ms |
71416 KB |
Output is correct |
85 |
Correct |
60 ms |
71416 KB |
Output is correct |
86 |
Correct |
60 ms |
71288 KB |
Output is correct |
87 |
Correct |
81 ms |
73336 KB |
Output is correct |
88 |
Correct |
130 ms |
79860 KB |
Output is correct |
89 |
Correct |
386 ms |
102704 KB |
Output is correct |
90 |
Correct |
368 ms |
104552 KB |
Output is correct |
91 |
Correct |
382 ms |
104680 KB |
Output is correct |
92 |
Correct |
158 ms |
86380 KB |
Output is correct |
93 |
Correct |
165 ms |
86380 KB |
Output is correct |
94 |
Correct |
157 ms |
86376 KB |
Output is correct |
95 |
Correct |
203 ms |
109676 KB |
Output is correct |
96 |
Correct |
227 ms |
110184 KB |
Output is correct |
97 |
Correct |
223 ms |
110060 KB |
Output is correct |
98 |
Correct |
220 ms |
110056 KB |
Output is correct |
99 |
Correct |
225 ms |
108620 KB |
Output is correct |
100 |
Correct |
413 ms |
96232 KB |
Output is correct |
101 |
Correct |
484 ms |
96664 KB |
Output is correct |
102 |
Correct |
469 ms |
96512 KB |
Output is correct |
103 |
Correct |
476 ms |
96512 KB |
Output is correct |
104 |
Correct |
223 ms |
108392 KB |
Output is correct |
105 |
Correct |
230 ms |
108524 KB |
Output is correct |
106 |
Correct |
218 ms |
108524 KB |
Output is correct |
107 |
Correct |
368 ms |
103896 KB |
Output is correct |
108 |
Correct |
369 ms |
104048 KB |
Output is correct |
109 |
Correct |
375 ms |
104300 KB |
Output is correct |
110 |
Correct |
149 ms |
85864 KB |
Output is correct |
111 |
Correct |
203 ms |
109676 KB |
Output is correct |
112 |
Correct |
213 ms |
109548 KB |
Output is correct |
113 |
Correct |
210 ms |
107884 KB |
Output is correct |
114 |
Correct |
412 ms |
96232 KB |
Output is correct |
115 |
Correct |
479 ms |
95956 KB |
Output is correct |
116 |
Correct |
225 ms |
107864 KB |
Output is correct |
117 |
Correct |
222 ms |
106348 KB |
Output is correct |
118 |
Correct |
229 ms |
105580 KB |
Output is correct |
119 |
Correct |
233 ms |
105196 KB |
Output is correct |
120 |
Correct |
218 ms |
105832 KB |
Output is correct |
121 |
Correct |
217 ms |
105192 KB |
Output is correct |
122 |
Correct |
226 ms |
104936 KB |
Output is correct |
123 |
Correct |
231 ms |
106216 KB |
Output is correct |
124 |
Correct |
235 ms |
105580 KB |
Output is correct |
125 |
Correct |
256 ms |
105344 KB |
Output is correct |
126 |
Correct |
230 ms |
105968 KB |
Output is correct |
127 |
Correct |
230 ms |
105260 KB |
Output is correct |
128 |
Correct |
235 ms |
104936 KB |
Output is correct |