#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;
}
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);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
70776 KB |
Output is correct |
2 |
Correct |
54 ms |
70784 KB |
Output is correct |
3 |
Correct |
55 ms |
70780 KB |
Output is correct |
4 |
Correct |
58 ms |
70904 KB |
Output is correct |
5 |
Correct |
77 ms |
71032 KB |
Output is correct |
6 |
Correct |
67 ms |
70904 KB |
Output is correct |
7 |
Correct |
64 ms |
70904 KB |
Output is correct |
8 |
Correct |
61 ms |
70784 KB |
Output is correct |
9 |
Correct |
61 ms |
70904 KB |
Output is correct |
10 |
Correct |
61 ms |
70912 KB |
Output is correct |
11 |
Correct |
72 ms |
71032 KB |
Output is correct |
12 |
Correct |
67 ms |
70904 KB |
Output is correct |
13 |
Correct |
64 ms |
70904 KB |
Output is correct |
14 |
Correct |
65 ms |
71032 KB |
Output is correct |
15 |
Correct |
66 ms |
70904 KB |
Output is correct |
16 |
Correct |
67 ms |
70904 KB |
Output is correct |
17 |
Correct |
64 ms |
70904 KB |
Output is correct |
18 |
Correct |
66 ms |
70904 KB |
Output is correct |
19 |
Correct |
64 ms |
71020 KB |
Output is correct |
20 |
Correct |
65 ms |
70904 KB |
Output is correct |
21 |
Correct |
66 ms |
70904 KB |
Output is correct |
22 |
Correct |
69 ms |
71032 KB |
Output is correct |
23 |
Correct |
67 ms |
71040 KB |
Output is correct |
24 |
Correct |
71 ms |
70904 KB |
Output is correct |
25 |
Correct |
64 ms |
70912 KB |
Output is correct |
26 |
Correct |
65 ms |
70904 KB |
Output is correct |
27 |
Correct |
67 ms |
70904 KB |
Output is correct |
28 |
Correct |
66 ms |
71024 KB |
Output is correct |
29 |
Correct |
64 ms |
70904 KB |
Output is correct |
30 |
Correct |
66 ms |
70904 KB |
Output is correct |
31 |
Correct |
66 ms |
70912 KB |
Output is correct |
32 |
Correct |
74 ms |
70908 KB |
Output is correct |
33 |
Correct |
65 ms |
71036 KB |
Output is correct |
34 |
Correct |
66 ms |
70904 KB |
Output is correct |
35 |
Correct |
64 ms |
70912 KB |
Output is correct |
36 |
Correct |
66 ms |
71032 KB |
Output is correct |
37 |
Correct |
65 ms |
70904 KB |
Output is correct |
38 |
Correct |
68 ms |
71072 KB |
Output is correct |
39 |
Correct |
65 ms |
70904 KB |
Output is correct |
40 |
Correct |
65 ms |
70904 KB |
Output is correct |
41 |
Correct |
64 ms |
70904 KB |
Output is correct |
42 |
Correct |
66 ms |
70908 KB |
Output is correct |
43 |
Correct |
67 ms |
70904 KB |
Output is correct |
44 |
Correct |
65 ms |
70904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
70776 KB |
Output is correct |
2 |
Correct |
54 ms |
70784 KB |
Output is correct |
3 |
Correct |
55 ms |
70780 KB |
Output is correct |
4 |
Correct |
58 ms |
70904 KB |
Output is correct |
5 |
Correct |
77 ms |
71032 KB |
Output is correct |
6 |
Correct |
67 ms |
70904 KB |
Output is correct |
7 |
Correct |
64 ms |
70904 KB |
Output is correct |
8 |
Correct |
61 ms |
70784 KB |
Output is correct |
9 |
Correct |
61 ms |
70904 KB |
Output is correct |
10 |
Correct |
61 ms |
70912 KB |
Output is correct |
11 |
Correct |
72 ms |
71032 KB |
Output is correct |
12 |
Correct |
67 ms |
70904 KB |
Output is correct |
13 |
Correct |
64 ms |
70904 KB |
Output is correct |
14 |
Correct |
65 ms |
71032 KB |
Output is correct |
15 |
Correct |
66 ms |
70904 KB |
Output is correct |
16 |
Correct |
67 ms |
70904 KB |
Output is correct |
17 |
Correct |
64 ms |
70904 KB |
Output is correct |
18 |
Correct |
66 ms |
70904 KB |
Output is correct |
19 |
Correct |
64 ms |
71020 KB |
Output is correct |
20 |
Correct |
65 ms |
70904 KB |
Output is correct |
21 |
Correct |
66 ms |
70904 KB |
Output is correct |
22 |
Correct |
69 ms |
71032 KB |
Output is correct |
23 |
Correct |
67 ms |
71040 KB |
Output is correct |
24 |
Correct |
71 ms |
70904 KB |
Output is correct |
25 |
Correct |
64 ms |
70912 KB |
Output is correct |
26 |
Correct |
65 ms |
70904 KB |
Output is correct |
27 |
Correct |
67 ms |
70904 KB |
Output is correct |
28 |
Correct |
66 ms |
71024 KB |
Output is correct |
29 |
Correct |
64 ms |
70904 KB |
Output is correct |
30 |
Correct |
66 ms |
70904 KB |
Output is correct |
31 |
Correct |
66 ms |
70912 KB |
Output is correct |
32 |
Correct |
74 ms |
70908 KB |
Output is correct |
33 |
Correct |
65 ms |
71036 KB |
Output is correct |
34 |
Correct |
66 ms |
70904 KB |
Output is correct |
35 |
Correct |
64 ms |
70912 KB |
Output is correct |
36 |
Correct |
66 ms |
71032 KB |
Output is correct |
37 |
Correct |
65 ms |
70904 KB |
Output is correct |
38 |
Correct |
68 ms |
71072 KB |
Output is correct |
39 |
Correct |
65 ms |
70904 KB |
Output is correct |
40 |
Correct |
65 ms |
70904 KB |
Output is correct |
41 |
Correct |
64 ms |
70904 KB |
Output is correct |
42 |
Correct |
66 ms |
70908 KB |
Output is correct |
43 |
Correct |
67 ms |
70904 KB |
Output is correct |
44 |
Correct |
65 ms |
70904 KB |
Output is correct |
45 |
Correct |
80 ms |
71160 KB |
Output is correct |
46 |
Correct |
161 ms |
72172 KB |
Output is correct |
47 |
Correct |
178 ms |
72184 KB |
Output is correct |
48 |
Correct |
173 ms |
72056 KB |
Output is correct |
49 |
Correct |
142 ms |
71416 KB |
Output is correct |
50 |
Correct |
145 ms |
71416 KB |
Output is correct |
51 |
Correct |
151 ms |
71416 KB |
Output is correct |
52 |
Correct |
154 ms |
72312 KB |
Output is correct |
53 |
Correct |
159 ms |
72312 KB |
Output is correct |
54 |
Correct |
148 ms |
72312 KB |
Output is correct |
55 |
Correct |
160 ms |
72312 KB |
Output is correct |
56 |
Correct |
151 ms |
72312 KB |
Output is correct |
57 |
Correct |
156 ms |
72056 KB |
Output is correct |
58 |
Correct |
158 ms |
72056 KB |
Output is correct |
59 |
Correct |
158 ms |
72032 KB |
Output is correct |
60 |
Correct |
162 ms |
72056 KB |
Output is correct |
61 |
Correct |
152 ms |
72260 KB |
Output is correct |
62 |
Correct |
163 ms |
72568 KB |
Output is correct |
63 |
Correct |
149 ms |
72312 KB |
Output is correct |
64 |
Correct |
163 ms |
72184 KB |
Output is correct |
65 |
Correct |
160 ms |
72160 KB |
Output is correct |
66 |
Correct |
162 ms |
72184 KB |
Output is correct |
67 |
Correct |
156 ms |
72088 KB |
Output is correct |
68 |
Correct |
142 ms |
71416 KB |
Output is correct |
69 |
Correct |
158 ms |
72312 KB |
Output is correct |
70 |
Correct |
149 ms |
72312 KB |
Output is correct |
71 |
Correct |
161 ms |
72200 KB |
Output is correct |
72 |
Correct |
158 ms |
72056 KB |
Output is correct |
73 |
Correct |
170 ms |
72056 KB |
Output is correct |
74 |
Correct |
151 ms |
72288 KB |
Output is correct |
75 |
Correct |
155 ms |
72160 KB |
Output is correct |
76 |
Correct |
157 ms |
72184 KB |
Output is correct |
77 |
Correct |
156 ms |
72056 KB |
Output is correct |
78 |
Correct |
149 ms |
72184 KB |
Output is correct |
79 |
Correct |
150 ms |
72184 KB |
Output is correct |
80 |
Correct |
158 ms |
72188 KB |
Output is correct |
81 |
Correct |
150 ms |
72184 KB |
Output is correct |
82 |
Correct |
153 ms |
72184 KB |
Output is correct |
83 |
Correct |
157 ms |
72064 KB |
Output is correct |
84 |
Correct |
159 ms |
72312 KB |
Output is correct |
85 |
Correct |
155 ms |
72184 KB |
Output is correct |
86 |
Correct |
166 ms |
72056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
70776 KB |
Output is correct |
2 |
Correct |
54 ms |
70784 KB |
Output is correct |
3 |
Correct |
55 ms |
70780 KB |
Output is correct |
4 |
Correct |
58 ms |
70904 KB |
Output is correct |
5 |
Correct |
77 ms |
71032 KB |
Output is correct |
6 |
Correct |
67 ms |
70904 KB |
Output is correct |
7 |
Correct |
64 ms |
70904 KB |
Output is correct |
8 |
Correct |
61 ms |
70784 KB |
Output is correct |
9 |
Correct |
61 ms |
70904 KB |
Output is correct |
10 |
Correct |
61 ms |
70912 KB |
Output is correct |
11 |
Correct |
72 ms |
71032 KB |
Output is correct |
12 |
Correct |
67 ms |
70904 KB |
Output is correct |
13 |
Correct |
64 ms |
70904 KB |
Output is correct |
14 |
Correct |
65 ms |
71032 KB |
Output is correct |
15 |
Correct |
66 ms |
70904 KB |
Output is correct |
16 |
Correct |
67 ms |
70904 KB |
Output is correct |
17 |
Correct |
64 ms |
70904 KB |
Output is correct |
18 |
Correct |
66 ms |
70904 KB |
Output is correct |
19 |
Correct |
64 ms |
71020 KB |
Output is correct |
20 |
Correct |
65 ms |
70904 KB |
Output is correct |
21 |
Correct |
66 ms |
70904 KB |
Output is correct |
22 |
Correct |
69 ms |
71032 KB |
Output is correct |
23 |
Correct |
67 ms |
71040 KB |
Output is correct |
24 |
Correct |
71 ms |
70904 KB |
Output is correct |
25 |
Correct |
64 ms |
70912 KB |
Output is correct |
26 |
Correct |
65 ms |
70904 KB |
Output is correct |
27 |
Correct |
67 ms |
70904 KB |
Output is correct |
28 |
Correct |
66 ms |
71024 KB |
Output is correct |
29 |
Correct |
64 ms |
70904 KB |
Output is correct |
30 |
Correct |
66 ms |
70904 KB |
Output is correct |
31 |
Correct |
66 ms |
70912 KB |
Output is correct |
32 |
Correct |
74 ms |
70908 KB |
Output is correct |
33 |
Correct |
65 ms |
71036 KB |
Output is correct |
34 |
Correct |
66 ms |
70904 KB |
Output is correct |
35 |
Correct |
64 ms |
70912 KB |
Output is correct |
36 |
Correct |
66 ms |
71032 KB |
Output is correct |
37 |
Correct |
65 ms |
70904 KB |
Output is correct |
38 |
Correct |
68 ms |
71072 KB |
Output is correct |
39 |
Correct |
65 ms |
70904 KB |
Output is correct |
40 |
Correct |
65 ms |
70904 KB |
Output is correct |
41 |
Correct |
64 ms |
70904 KB |
Output is correct |
42 |
Correct |
66 ms |
70908 KB |
Output is correct |
43 |
Correct |
67 ms |
70904 KB |
Output is correct |
44 |
Correct |
65 ms |
70904 KB |
Output is correct |
45 |
Correct |
80 ms |
71160 KB |
Output is correct |
46 |
Correct |
161 ms |
72172 KB |
Output is correct |
47 |
Correct |
178 ms |
72184 KB |
Output is correct |
48 |
Correct |
173 ms |
72056 KB |
Output is correct |
49 |
Correct |
142 ms |
71416 KB |
Output is correct |
50 |
Correct |
145 ms |
71416 KB |
Output is correct |
51 |
Correct |
151 ms |
71416 KB |
Output is correct |
52 |
Correct |
154 ms |
72312 KB |
Output is correct |
53 |
Correct |
159 ms |
72312 KB |
Output is correct |
54 |
Correct |
148 ms |
72312 KB |
Output is correct |
55 |
Correct |
160 ms |
72312 KB |
Output is correct |
56 |
Correct |
151 ms |
72312 KB |
Output is correct |
57 |
Correct |
156 ms |
72056 KB |
Output is correct |
58 |
Correct |
158 ms |
72056 KB |
Output is correct |
59 |
Correct |
158 ms |
72032 KB |
Output is correct |
60 |
Correct |
162 ms |
72056 KB |
Output is correct |
61 |
Correct |
152 ms |
72260 KB |
Output is correct |
62 |
Correct |
163 ms |
72568 KB |
Output is correct |
63 |
Correct |
149 ms |
72312 KB |
Output is correct |
64 |
Correct |
163 ms |
72184 KB |
Output is correct |
65 |
Correct |
160 ms |
72160 KB |
Output is correct |
66 |
Correct |
162 ms |
72184 KB |
Output is correct |
67 |
Correct |
156 ms |
72088 KB |
Output is correct |
68 |
Correct |
142 ms |
71416 KB |
Output is correct |
69 |
Correct |
158 ms |
72312 KB |
Output is correct |
70 |
Correct |
149 ms |
72312 KB |
Output is correct |
71 |
Correct |
161 ms |
72200 KB |
Output is correct |
72 |
Correct |
158 ms |
72056 KB |
Output is correct |
73 |
Correct |
170 ms |
72056 KB |
Output is correct |
74 |
Correct |
151 ms |
72288 KB |
Output is correct |
75 |
Correct |
155 ms |
72160 KB |
Output is correct |
76 |
Correct |
157 ms |
72184 KB |
Output is correct |
77 |
Correct |
156 ms |
72056 KB |
Output is correct |
78 |
Correct |
149 ms |
72184 KB |
Output is correct |
79 |
Correct |
150 ms |
72184 KB |
Output is correct |
80 |
Correct |
158 ms |
72188 KB |
Output is correct |
81 |
Correct |
150 ms |
72184 KB |
Output is correct |
82 |
Correct |
153 ms |
72184 KB |
Output is correct |
83 |
Correct |
157 ms |
72064 KB |
Output is correct |
84 |
Correct |
159 ms |
72312 KB |
Output is correct |
85 |
Correct |
155 ms |
72184 KB |
Output is correct |
86 |
Correct |
166 ms |
72056 KB |
Output is correct |
87 |
Correct |
332 ms |
74108 KB |
Output is correct |
88 |
Correct |
842 ms |
80684 KB |
Output is correct |
89 |
Execution timed out |
2091 ms |
97520 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |