#include<bits/stdc++.h>
#define Task "test"
#define ll long long
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int N = int(1e5) + 2;
typedef pair<int, int> pii;
int sz[N], head[N], ind[N], pos[N], nChain = 0, nBase = 0;
int n, _a[N], _b[N], c[N], t[N], p[N], bitsz;
vector<int> a[N];
deque<pii> dq[N];
void DFS(int u) {
sz[u] = 1;
for(int v: a[u]) DFS(v), sz[u] += sz[v];
}
void HLD(int u) {
if(!head[nChain]) head[nChain] = u;
ind[u] = nChain, pos[u] = ++nBase;
int big = 0;
for(int v: a[u])
if(sz[big] < sz[v]) big = v;
if(big) HLD(big);
for(int v: a[u]) {
if(v != big) ++nChain, HLD(v);
}
}
void Rs(int n) {bitsz = n; fill(t, t + bitsz + 1, 0);}
void Update(int x, int val) {for(; x <= bitsz; x += (x & -x)) t[x] += val;}
int Get(int x) {int res = 0; for(; x > 0; x -= (x & -x)) res += t[x]; return res;}
ll Solve(int par, int u) {
ll res = 0;
// Count par
int uch = ind[par], cnt;
vector<pii> val, tmp;
vector<int> idx;
while(1) {
cnt = pos[par] - pos[head[uch]] + 1;
tmp.clear();
for(int i = 0; i < dq[uch].size(); ++i) {
if(dq[uch][i].se <= cnt) {
tmp.pb(dq[uch][i]);
cnt -= dq[uch][i].se;
} else {
tmp.pb(dq[uch][i].fi, cnt);
break;
}
}
for(int i = int(tmp.size()) - 1; i >= 0; --i) idx.pb(tmp[i].fi), val.pb(tmp[i]);
if(!uch) break;
par = p[head[uch]], uch = ind[par];
}
sort(idx.begin(), idx.end());
idx.erase(unique(idx.begin(), idx.end()), idx.end());
reverse(val.begin(), val.end());
int cur = 0; Rs(idx.size());
for(pii p: val) {
p.fi = lower_bound(idx.begin(), idx.end(), p.fi) - idx.begin() + 1;
res += 1ll * p.se * (cur - Get(p.fi));
cur += p.se, Update(p.fi, p.se);
}
// Update u
uch = ind[u]; cur = c[u];
while(1) {
cnt = pos[u] - pos[head[uch]] + 1;
while(dq[uch].size() && cnt) {
if(dq[uch][0].se <= cnt) {
cnt -= dq[uch][0].se;
dq[uch].pop_front();
} else {
dq[uch][0].se -= cnt;
break;
}
}
dq[uch].push_front(mp(cur, pos[u] - pos[head[uch]] + 1));
if(!uch) break;
u = p[head[uch]], uch = ind[u];
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
if(fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout);
}
cin >> n;
for(int i = 1; i <= n; ++i) cin >> c[i];
for(int i = 1; i < n; ++i) {
cin >> _a[i] >> _b[i];
a[_a[i]].pb(_b[i]);
p[_b[i]] = _a[i];
}
DFS(1); HLD(1);
for(int i = 1; i < n; ++i) cout << Solve(_a[i], _b[i]) << '\n';
}
Compilation message
construction.cpp: In function 'long long int Solve(int, int)':
construction.cpp:49:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < dq[uch].size(); ++i) {
~~^~~~~~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(Task".inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(Task".out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
70264 KB |
Output is correct |
2 |
Correct |
74 ms |
70264 KB |
Output is correct |
3 |
Correct |
79 ms |
70084 KB |
Output is correct |
4 |
Correct |
81 ms |
70176 KB |
Output is correct |
5 |
Correct |
80 ms |
70256 KB |
Output is correct |
6 |
Correct |
79 ms |
70140 KB |
Output is correct |
7 |
Correct |
81 ms |
70264 KB |
Output is correct |
8 |
Correct |
76 ms |
70088 KB |
Output is correct |
9 |
Correct |
74 ms |
70140 KB |
Output is correct |
10 |
Correct |
75 ms |
70264 KB |
Output is correct |
11 |
Correct |
81 ms |
70264 KB |
Output is correct |
12 |
Correct |
80 ms |
70352 KB |
Output is correct |
13 |
Correct |
81 ms |
70264 KB |
Output is correct |
14 |
Correct |
78 ms |
70140 KB |
Output is correct |
15 |
Correct |
81 ms |
70264 KB |
Output is correct |
16 |
Correct |
80 ms |
70136 KB |
Output is correct |
17 |
Correct |
83 ms |
70268 KB |
Output is correct |
18 |
Correct |
80 ms |
70264 KB |
Output is correct |
19 |
Correct |
80 ms |
70264 KB |
Output is correct |
20 |
Correct |
81 ms |
70264 KB |
Output is correct |
21 |
Correct |
80 ms |
70264 KB |
Output is correct |
22 |
Correct |
80 ms |
70264 KB |
Output is correct |
23 |
Correct |
79 ms |
70256 KB |
Output is correct |
24 |
Correct |
79 ms |
70188 KB |
Output is correct |
25 |
Correct |
81 ms |
70136 KB |
Output is correct |
26 |
Correct |
74 ms |
70136 KB |
Output is correct |
27 |
Correct |
84 ms |
70348 KB |
Output is correct |
28 |
Correct |
81 ms |
70244 KB |
Output is correct |
29 |
Correct |
89 ms |
70344 KB |
Output is correct |
30 |
Correct |
89 ms |
70256 KB |
Output is correct |
31 |
Correct |
98 ms |
70392 KB |
Output is correct |
32 |
Correct |
85 ms |
70396 KB |
Output is correct |
33 |
Correct |
79 ms |
70136 KB |
Output is correct |
34 |
Correct |
89 ms |
70244 KB |
Output is correct |
35 |
Correct |
99 ms |
70136 KB |
Output is correct |
36 |
Correct |
79 ms |
70288 KB |
Output is correct |
37 |
Correct |
79 ms |
70140 KB |
Output is correct |
38 |
Correct |
80 ms |
70136 KB |
Output is correct |
39 |
Correct |
78 ms |
70136 KB |
Output is correct |
40 |
Correct |
79 ms |
70252 KB |
Output is correct |
41 |
Correct |
80 ms |
70272 KB |
Output is correct |
42 |
Correct |
80 ms |
70252 KB |
Output is correct |
43 |
Correct |
98 ms |
70140 KB |
Output is correct |
44 |
Correct |
84 ms |
70212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
70264 KB |
Output is correct |
2 |
Correct |
74 ms |
70264 KB |
Output is correct |
3 |
Correct |
79 ms |
70084 KB |
Output is correct |
4 |
Correct |
81 ms |
70176 KB |
Output is correct |
5 |
Correct |
80 ms |
70256 KB |
Output is correct |
6 |
Correct |
79 ms |
70140 KB |
Output is correct |
7 |
Correct |
81 ms |
70264 KB |
Output is correct |
8 |
Correct |
76 ms |
70088 KB |
Output is correct |
9 |
Correct |
74 ms |
70140 KB |
Output is correct |
10 |
Correct |
75 ms |
70264 KB |
Output is correct |
11 |
Correct |
81 ms |
70264 KB |
Output is correct |
12 |
Correct |
80 ms |
70352 KB |
Output is correct |
13 |
Correct |
81 ms |
70264 KB |
Output is correct |
14 |
Correct |
78 ms |
70140 KB |
Output is correct |
15 |
Correct |
81 ms |
70264 KB |
Output is correct |
16 |
Correct |
80 ms |
70136 KB |
Output is correct |
17 |
Correct |
83 ms |
70268 KB |
Output is correct |
18 |
Correct |
80 ms |
70264 KB |
Output is correct |
19 |
Correct |
80 ms |
70264 KB |
Output is correct |
20 |
Correct |
81 ms |
70264 KB |
Output is correct |
21 |
Correct |
80 ms |
70264 KB |
Output is correct |
22 |
Correct |
80 ms |
70264 KB |
Output is correct |
23 |
Correct |
79 ms |
70256 KB |
Output is correct |
24 |
Correct |
79 ms |
70188 KB |
Output is correct |
25 |
Correct |
81 ms |
70136 KB |
Output is correct |
26 |
Correct |
74 ms |
70136 KB |
Output is correct |
27 |
Correct |
84 ms |
70348 KB |
Output is correct |
28 |
Correct |
81 ms |
70244 KB |
Output is correct |
29 |
Correct |
89 ms |
70344 KB |
Output is correct |
30 |
Correct |
89 ms |
70256 KB |
Output is correct |
31 |
Correct |
98 ms |
70392 KB |
Output is correct |
32 |
Correct |
85 ms |
70396 KB |
Output is correct |
33 |
Correct |
79 ms |
70136 KB |
Output is correct |
34 |
Correct |
89 ms |
70244 KB |
Output is correct |
35 |
Correct |
99 ms |
70136 KB |
Output is correct |
36 |
Correct |
79 ms |
70288 KB |
Output is correct |
37 |
Correct |
79 ms |
70140 KB |
Output is correct |
38 |
Correct |
80 ms |
70136 KB |
Output is correct |
39 |
Correct |
78 ms |
70136 KB |
Output is correct |
40 |
Correct |
79 ms |
70252 KB |
Output is correct |
41 |
Correct |
80 ms |
70272 KB |
Output is correct |
42 |
Correct |
80 ms |
70252 KB |
Output is correct |
43 |
Correct |
98 ms |
70140 KB |
Output is correct |
44 |
Correct |
84 ms |
70212 KB |
Output is correct |
45 |
Correct |
80 ms |
70392 KB |
Output is correct |
46 |
Correct |
88 ms |
71416 KB |
Output is correct |
47 |
Correct |
96 ms |
71416 KB |
Output is correct |
48 |
Correct |
102 ms |
71472 KB |
Output is correct |
49 |
Correct |
78 ms |
70520 KB |
Output is correct |
50 |
Correct |
77 ms |
70520 KB |
Output is correct |
51 |
Correct |
78 ms |
70620 KB |
Output is correct |
52 |
Correct |
83 ms |
71544 KB |
Output is correct |
53 |
Correct |
86 ms |
71560 KB |
Output is correct |
54 |
Correct |
84 ms |
71520 KB |
Output is correct |
55 |
Correct |
83 ms |
71544 KB |
Output is correct |
56 |
Correct |
84 ms |
71448 KB |
Output is correct |
57 |
Correct |
88 ms |
71416 KB |
Output is correct |
58 |
Correct |
98 ms |
71372 KB |
Output is correct |
59 |
Correct |
108 ms |
71288 KB |
Output is correct |
60 |
Correct |
90 ms |
71292 KB |
Output is correct |
61 |
Correct |
93 ms |
71544 KB |
Output is correct |
62 |
Correct |
105 ms |
71416 KB |
Output is correct |
63 |
Correct |
83 ms |
71416 KB |
Output is correct |
64 |
Correct |
85 ms |
71312 KB |
Output is correct |
65 |
Correct |
95 ms |
71396 KB |
Output is correct |
66 |
Correct |
88 ms |
71416 KB |
Output is correct |
67 |
Correct |
99 ms |
71384 KB |
Output is correct |
68 |
Correct |
77 ms |
70520 KB |
Output is correct |
69 |
Correct |
84 ms |
71416 KB |
Output is correct |
70 |
Correct |
83 ms |
71484 KB |
Output is correct |
71 |
Correct |
84 ms |
71356 KB |
Output is correct |
72 |
Correct |
87 ms |
71296 KB |
Output is correct |
73 |
Correct |
96 ms |
71416 KB |
Output is correct |
74 |
Correct |
84 ms |
71508 KB |
Output is correct |
75 |
Correct |
83 ms |
71416 KB |
Output is correct |
76 |
Correct |
85 ms |
71388 KB |
Output is correct |
77 |
Correct |
85 ms |
71416 KB |
Output is correct |
78 |
Correct |
85 ms |
71408 KB |
Output is correct |
79 |
Correct |
84 ms |
71444 KB |
Output is correct |
80 |
Correct |
84 ms |
71288 KB |
Output is correct |
81 |
Correct |
83 ms |
71416 KB |
Output is correct |
82 |
Correct |
84 ms |
71416 KB |
Output is correct |
83 |
Correct |
85 ms |
71460 KB |
Output is correct |
84 |
Correct |
84 ms |
71352 KB |
Output is correct |
85 |
Correct |
86 ms |
71408 KB |
Output is correct |
86 |
Correct |
85 ms |
71348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
70264 KB |
Output is correct |
2 |
Correct |
74 ms |
70264 KB |
Output is correct |
3 |
Correct |
79 ms |
70084 KB |
Output is correct |
4 |
Correct |
81 ms |
70176 KB |
Output is correct |
5 |
Correct |
80 ms |
70256 KB |
Output is correct |
6 |
Correct |
79 ms |
70140 KB |
Output is correct |
7 |
Correct |
81 ms |
70264 KB |
Output is correct |
8 |
Correct |
76 ms |
70088 KB |
Output is correct |
9 |
Correct |
74 ms |
70140 KB |
Output is correct |
10 |
Correct |
75 ms |
70264 KB |
Output is correct |
11 |
Correct |
81 ms |
70264 KB |
Output is correct |
12 |
Correct |
80 ms |
70352 KB |
Output is correct |
13 |
Correct |
81 ms |
70264 KB |
Output is correct |
14 |
Correct |
78 ms |
70140 KB |
Output is correct |
15 |
Correct |
81 ms |
70264 KB |
Output is correct |
16 |
Correct |
80 ms |
70136 KB |
Output is correct |
17 |
Correct |
83 ms |
70268 KB |
Output is correct |
18 |
Correct |
80 ms |
70264 KB |
Output is correct |
19 |
Correct |
80 ms |
70264 KB |
Output is correct |
20 |
Correct |
81 ms |
70264 KB |
Output is correct |
21 |
Correct |
80 ms |
70264 KB |
Output is correct |
22 |
Correct |
80 ms |
70264 KB |
Output is correct |
23 |
Correct |
79 ms |
70256 KB |
Output is correct |
24 |
Correct |
79 ms |
70188 KB |
Output is correct |
25 |
Correct |
81 ms |
70136 KB |
Output is correct |
26 |
Correct |
74 ms |
70136 KB |
Output is correct |
27 |
Correct |
84 ms |
70348 KB |
Output is correct |
28 |
Correct |
81 ms |
70244 KB |
Output is correct |
29 |
Correct |
89 ms |
70344 KB |
Output is correct |
30 |
Correct |
89 ms |
70256 KB |
Output is correct |
31 |
Correct |
98 ms |
70392 KB |
Output is correct |
32 |
Correct |
85 ms |
70396 KB |
Output is correct |
33 |
Correct |
79 ms |
70136 KB |
Output is correct |
34 |
Correct |
89 ms |
70244 KB |
Output is correct |
35 |
Correct |
99 ms |
70136 KB |
Output is correct |
36 |
Correct |
79 ms |
70288 KB |
Output is correct |
37 |
Correct |
79 ms |
70140 KB |
Output is correct |
38 |
Correct |
80 ms |
70136 KB |
Output is correct |
39 |
Correct |
78 ms |
70136 KB |
Output is correct |
40 |
Correct |
79 ms |
70252 KB |
Output is correct |
41 |
Correct |
80 ms |
70272 KB |
Output is correct |
42 |
Correct |
80 ms |
70252 KB |
Output is correct |
43 |
Correct |
98 ms |
70140 KB |
Output is correct |
44 |
Correct |
84 ms |
70212 KB |
Output is correct |
45 |
Correct |
80 ms |
70392 KB |
Output is correct |
46 |
Correct |
88 ms |
71416 KB |
Output is correct |
47 |
Correct |
96 ms |
71416 KB |
Output is correct |
48 |
Correct |
102 ms |
71472 KB |
Output is correct |
49 |
Correct |
78 ms |
70520 KB |
Output is correct |
50 |
Correct |
77 ms |
70520 KB |
Output is correct |
51 |
Correct |
78 ms |
70620 KB |
Output is correct |
52 |
Correct |
83 ms |
71544 KB |
Output is correct |
53 |
Correct |
86 ms |
71560 KB |
Output is correct |
54 |
Correct |
84 ms |
71520 KB |
Output is correct |
55 |
Correct |
83 ms |
71544 KB |
Output is correct |
56 |
Correct |
84 ms |
71448 KB |
Output is correct |
57 |
Correct |
88 ms |
71416 KB |
Output is correct |
58 |
Correct |
98 ms |
71372 KB |
Output is correct |
59 |
Correct |
108 ms |
71288 KB |
Output is correct |
60 |
Correct |
90 ms |
71292 KB |
Output is correct |
61 |
Correct |
93 ms |
71544 KB |
Output is correct |
62 |
Correct |
105 ms |
71416 KB |
Output is correct |
63 |
Correct |
83 ms |
71416 KB |
Output is correct |
64 |
Correct |
85 ms |
71312 KB |
Output is correct |
65 |
Correct |
95 ms |
71396 KB |
Output is correct |
66 |
Correct |
88 ms |
71416 KB |
Output is correct |
67 |
Correct |
99 ms |
71384 KB |
Output is correct |
68 |
Correct |
77 ms |
70520 KB |
Output is correct |
69 |
Correct |
84 ms |
71416 KB |
Output is correct |
70 |
Correct |
83 ms |
71484 KB |
Output is correct |
71 |
Correct |
84 ms |
71356 KB |
Output is correct |
72 |
Correct |
87 ms |
71296 KB |
Output is correct |
73 |
Correct |
96 ms |
71416 KB |
Output is correct |
74 |
Correct |
84 ms |
71508 KB |
Output is correct |
75 |
Correct |
83 ms |
71416 KB |
Output is correct |
76 |
Correct |
85 ms |
71388 KB |
Output is correct |
77 |
Correct |
85 ms |
71416 KB |
Output is correct |
78 |
Correct |
85 ms |
71408 KB |
Output is correct |
79 |
Correct |
84 ms |
71444 KB |
Output is correct |
80 |
Correct |
84 ms |
71288 KB |
Output is correct |
81 |
Correct |
83 ms |
71416 KB |
Output is correct |
82 |
Correct |
84 ms |
71416 KB |
Output is correct |
83 |
Correct |
85 ms |
71460 KB |
Output is correct |
84 |
Correct |
84 ms |
71352 KB |
Output is correct |
85 |
Correct |
86 ms |
71408 KB |
Output is correct |
86 |
Correct |
85 ms |
71348 KB |
Output is correct |
87 |
Correct |
103 ms |
73380 KB |
Output is correct |
88 |
Correct |
164 ms |
79952 KB |
Output is correct |
89 |
Correct |
426 ms |
103068 KB |
Output is correct |
90 |
Correct |
442 ms |
102764 KB |
Output is correct |
91 |
Correct |
417 ms |
102884 KB |
Output is correct |
92 |
Correct |
183 ms |
82808 KB |
Output is correct |
93 |
Correct |
180 ms |
82900 KB |
Output is correct |
94 |
Correct |
187 ms |
82888 KB |
Output is correct |
95 |
Correct |
229 ms |
106996 KB |
Output is correct |
96 |
Correct |
271 ms |
107244 KB |
Output is correct |
97 |
Correct |
246 ms |
107364 KB |
Output is correct |
98 |
Correct |
237 ms |
107180 KB |
Output is correct |
99 |
Correct |
262 ms |
105228 KB |
Output is correct |
100 |
Correct |
449 ms |
95004 KB |
Output is correct |
101 |
Correct |
532 ms |
95224 KB |
Output is correct |
102 |
Correct |
527 ms |
95188 KB |
Output is correct |
103 |
Correct |
520 ms |
95224 KB |
Output is correct |
104 |
Correct |
309 ms |
105080 KB |
Output is correct |
105 |
Correct |
312 ms |
105080 KB |
Output is correct |
106 |
Correct |
251 ms |
105216 KB |
Output is correct |
107 |
Correct |
384 ms |
101956 KB |
Output is correct |
108 |
Correct |
407 ms |
102232 KB |
Output is correct |
109 |
Correct |
420 ms |
102452 KB |
Output is correct |
110 |
Correct |
184 ms |
82332 KB |
Output is correct |
111 |
Correct |
232 ms |
106864 KB |
Output is correct |
112 |
Correct |
258 ms |
106356 KB |
Output is correct |
113 |
Correct |
263 ms |
104696 KB |
Output is correct |
114 |
Correct |
450 ms |
95056 KB |
Output is correct |
115 |
Correct |
516 ms |
94788 KB |
Output is correct |
116 |
Correct |
260 ms |
104560 KB |
Output is correct |
117 |
Correct |
235 ms |
103844 KB |
Output is correct |
118 |
Correct |
278 ms |
103288 KB |
Output is correct |
119 |
Correct |
237 ms |
103036 KB |
Output is correct |
120 |
Correct |
228 ms |
103348 KB |
Output is correct |
121 |
Correct |
245 ms |
102904 KB |
Output is correct |
122 |
Correct |
268 ms |
102720 KB |
Output is correct |
123 |
Correct |
322 ms |
103724 KB |
Output is correct |
124 |
Correct |
309 ms |
103248 KB |
Output is correct |
125 |
Correct |
295 ms |
103160 KB |
Output is correct |
126 |
Correct |
272 ms |
103436 KB |
Output is correct |
127 |
Correct |
263 ms |
102960 KB |
Output is correct |
128 |
Correct |
278 ms |
102948 KB |
Output is correct |