#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;
const int maxN = 100'000;
using vi = vector<int>;
using ll = long long;
using vll = vector<ll>;
using vvi = vector<vi>;
using pii = pair<int, int>; //liveliness, count
using vpii = vector<pii>;
int N;
vi C(1+maxN);
vi A(maxN), B(maxN);
vi parent(1+maxN, 0);
vi childlist[1+maxN];
vi subtree(1+maxN, 1);
vi chain(1+maxN, 0);
int chain_ct = 0;
vi chain_head(1+maxN, 0);
deque<pii> chain_list[1+maxN];
vi depth(1+maxN, 0);
void dfs(int u)
{
int main_child = -1;
for(int v: childlist[u])
{
depth[v] = 1 + depth[u];
dfs(v);
subtree[u] += subtree[v];
if(main_child == -1 || subtree[v] > subtree[main_child])
main_child = v;
}
if(main_child == -1)
{
chain_ct++;
chain[u] = chain_ct;
}
else
chain[u] = chain[main_child];
chain_head[ chain[u] ] = u;
}
deque<pii> lst;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
for(int i = 1; i <= N; i++) cin >> C[i];
for(int j = 1; j <= N-1; j++)
{
cin >> A[j] >> B[j];
parent[B[j]] = A[j];
childlist[A[j]].push_back(B[j]);
}
dfs(1);
// for(int i = 1; i <= N; i++) cerr << chain[i] << ' ';
// cerr << '\n';
chain_list[ chain[1] ].push_back(make_pair(C[1], 1));
for(int j = 1; j <= N-1; j++)
{
// cerr << "\n\n\n";
// cerr << "j = " << j << ": \n";
lst.clear();
// cerr << A[j] << " -> " << B[j] << '\n';
vpii query_markers;
for(int c = A[j]; c != 0; c = parent[ chain_head[ chain[ c ] ] ])
{
// cerr << "querying vertex: " << c << ", " << depth[c] - depth[ chain_head[ chain[c] ] ] + 1 << "\n";
query_markers.push_back(make_pair(chain[c], depth[c] - depth[ chain_head[ chain[c] ] ] + 1));
}
// reverse(query_markers.begin(), query_markers.end());
// for(int c = chain_head[ chain[A[j]] ]; c != 0; c = chain_head[ chain[ parent[c] ] ])
// {
// query_list.push_back(chain[c]);
// // cerr << "getting from node " << c << "\n";
// // for(pii l: chain_list[chain[c]])
// // lst.push_front(l);
// }
reverse(query_markers.begin(), query_markers.end());
for(auto q: query_markers)
{
int f = q.second;
int cf = 0;
for(pii l: chain_list[q.first])
{
int r_cf = min(f-cf, l.second);
cf += r_cf;
// lst.push_back(l);
lst.push_back(make_pair(l.first, r_cf));
if(cf == f) break;
}
}
// reverse(lst.begin(), lst.end());
int ls = int(lst.size());
// for(pii q: lst) cerr << q.first << ' ' << q.second << " | ";
// cerr << '\n';
vi I(ls);
for(int i = 0; i < ls; i++)
{
I[i] = i;
}
sort(I.begin(), I.end(), [] (int u, int v)
{
if(lst[u].first != lst[v].first) return lst[u].first > lst[v].first;
else return u > v;
});
ll ans = 0;
vll BIT(1 + ls, 0);
for(int i:I)
{
for(int q = i+1; q >= 1; q -= q&-q)
ans += BIT[q] * lst[i].second;
for(int q = i+1; q <= ls; q += q&-q)
BIT[q] += lst[i].second;
}
cout << ans << '\n';
chain_list[chain[B[j]]].push_back(make_pair(C[B[j]], 1));
deque<pii> update_chains;
for(int c = B[j]; c != 0; c = parent[ chain_head[ chain[c] ] ])
{
// cerr << "updating vertex " << c << '\n';
update_chains.push_front(make_pair(chain[c], depth[c] - depth[ chain_head[ chain[c] ] ] + 1));
}
for(pii u: update_chains)
{
// cerr << "u = " << u.first << ' ' << u.second << '\n';
int origin_remain = u.second;
int remain = u.second;
int c = u.first;
while(remain > 0)
{
int rx = chain_list[c][0].second;
int r = min(remain, rx);
chain_list[c][0].second -= r;
remain -= r;
if(chain_list[c][0].second == 0)
chain_list[c].pop_front();
}
chain_list[c].push_front(make_pair(C[B[j]], origin_remain));
// cerr << "pushing " << origin_remain << " of " << C[B[j]] << " at the head of chain " << c << '\n';
}
// for(int c = B[j]; c != 0; c = parent[ chain_head[ chain[c] ] ])
// {
// cerr << "updating " << chain[c] << " = " << C[B[j]] << ", " << depth[c] - depth[ chain_head[ chain[c] ] ] + 1 << '\n';
// chain_list[chain[c]].clear();
// chain_list[chain[c]].push_back(make_pair(C[B[j]], depth[c] - depth[ chain_head[ chain[c] ] ] + 1));
// }
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
73028 KB |
Output is correct |
2 |
Correct |
39 ms |
73036 KB |
Output is correct |
3 |
Correct |
40 ms |
73036 KB |
Output is correct |
4 |
Correct |
41 ms |
73132 KB |
Output is correct |
5 |
Correct |
40 ms |
73036 KB |
Output is correct |
6 |
Correct |
40 ms |
73036 KB |
Output is correct |
7 |
Correct |
39 ms |
73084 KB |
Output is correct |
8 |
Correct |
42 ms |
73104 KB |
Output is correct |
9 |
Correct |
42 ms |
73160 KB |
Output is correct |
10 |
Correct |
45 ms |
73144 KB |
Output is correct |
11 |
Correct |
39 ms |
73160 KB |
Output is correct |
12 |
Correct |
41 ms |
73216 KB |
Output is correct |
13 |
Correct |
40 ms |
73028 KB |
Output is correct |
14 |
Correct |
40 ms |
73164 KB |
Output is correct |
15 |
Correct |
40 ms |
73108 KB |
Output is correct |
16 |
Correct |
40 ms |
73036 KB |
Output is correct |
17 |
Correct |
39 ms |
73036 KB |
Output is correct |
18 |
Correct |
40 ms |
73036 KB |
Output is correct |
19 |
Correct |
40 ms |
73076 KB |
Output is correct |
20 |
Correct |
40 ms |
73068 KB |
Output is correct |
21 |
Correct |
40 ms |
73048 KB |
Output is correct |
22 |
Correct |
40 ms |
73108 KB |
Output is correct |
23 |
Correct |
41 ms |
73032 KB |
Output is correct |
24 |
Correct |
41 ms |
73052 KB |
Output is correct |
25 |
Correct |
42 ms |
73120 KB |
Output is correct |
26 |
Correct |
44 ms |
73064 KB |
Output is correct |
27 |
Correct |
41 ms |
73140 KB |
Output is correct |
28 |
Correct |
40 ms |
73044 KB |
Output is correct |
29 |
Correct |
44 ms |
73076 KB |
Output is correct |
30 |
Correct |
40 ms |
73036 KB |
Output is correct |
31 |
Correct |
44 ms |
73036 KB |
Output is correct |
32 |
Correct |
40 ms |
73076 KB |
Output is correct |
33 |
Correct |
40 ms |
73028 KB |
Output is correct |
34 |
Correct |
40 ms |
73032 KB |
Output is correct |
35 |
Correct |
39 ms |
73148 KB |
Output is correct |
36 |
Correct |
40 ms |
73024 KB |
Output is correct |
37 |
Correct |
40 ms |
73076 KB |
Output is correct |
38 |
Correct |
40 ms |
73028 KB |
Output is correct |
39 |
Correct |
40 ms |
73116 KB |
Output is correct |
40 |
Correct |
44 ms |
73028 KB |
Output is correct |
41 |
Correct |
40 ms |
73036 KB |
Output is correct |
42 |
Correct |
40 ms |
73140 KB |
Output is correct |
43 |
Correct |
40 ms |
73140 KB |
Output is correct |
44 |
Correct |
42 ms |
73028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
73028 KB |
Output is correct |
2 |
Correct |
39 ms |
73036 KB |
Output is correct |
3 |
Correct |
40 ms |
73036 KB |
Output is correct |
4 |
Correct |
41 ms |
73132 KB |
Output is correct |
5 |
Correct |
40 ms |
73036 KB |
Output is correct |
6 |
Correct |
40 ms |
73036 KB |
Output is correct |
7 |
Correct |
39 ms |
73084 KB |
Output is correct |
8 |
Correct |
42 ms |
73104 KB |
Output is correct |
9 |
Correct |
42 ms |
73160 KB |
Output is correct |
10 |
Correct |
45 ms |
73144 KB |
Output is correct |
11 |
Correct |
39 ms |
73160 KB |
Output is correct |
12 |
Correct |
41 ms |
73216 KB |
Output is correct |
13 |
Correct |
40 ms |
73028 KB |
Output is correct |
14 |
Correct |
40 ms |
73164 KB |
Output is correct |
15 |
Correct |
40 ms |
73108 KB |
Output is correct |
16 |
Correct |
40 ms |
73036 KB |
Output is correct |
17 |
Correct |
39 ms |
73036 KB |
Output is correct |
18 |
Correct |
40 ms |
73036 KB |
Output is correct |
19 |
Correct |
40 ms |
73076 KB |
Output is correct |
20 |
Correct |
40 ms |
73068 KB |
Output is correct |
21 |
Correct |
40 ms |
73048 KB |
Output is correct |
22 |
Correct |
40 ms |
73108 KB |
Output is correct |
23 |
Correct |
41 ms |
73032 KB |
Output is correct |
24 |
Correct |
41 ms |
73052 KB |
Output is correct |
25 |
Correct |
42 ms |
73120 KB |
Output is correct |
26 |
Correct |
44 ms |
73064 KB |
Output is correct |
27 |
Correct |
41 ms |
73140 KB |
Output is correct |
28 |
Correct |
40 ms |
73044 KB |
Output is correct |
29 |
Correct |
44 ms |
73076 KB |
Output is correct |
30 |
Correct |
40 ms |
73036 KB |
Output is correct |
31 |
Correct |
44 ms |
73036 KB |
Output is correct |
32 |
Correct |
40 ms |
73076 KB |
Output is correct |
33 |
Correct |
40 ms |
73028 KB |
Output is correct |
34 |
Correct |
40 ms |
73032 KB |
Output is correct |
35 |
Correct |
39 ms |
73148 KB |
Output is correct |
36 |
Correct |
40 ms |
73024 KB |
Output is correct |
37 |
Correct |
40 ms |
73076 KB |
Output is correct |
38 |
Correct |
40 ms |
73028 KB |
Output is correct |
39 |
Correct |
40 ms |
73116 KB |
Output is correct |
40 |
Correct |
44 ms |
73028 KB |
Output is correct |
41 |
Correct |
40 ms |
73036 KB |
Output is correct |
42 |
Correct |
40 ms |
73140 KB |
Output is correct |
43 |
Correct |
40 ms |
73140 KB |
Output is correct |
44 |
Correct |
42 ms |
73028 KB |
Output is correct |
45 |
Correct |
42 ms |
73028 KB |
Output is correct |
46 |
Correct |
44 ms |
73112 KB |
Output is correct |
47 |
Correct |
44 ms |
73184 KB |
Output is correct |
48 |
Correct |
45 ms |
73216 KB |
Output is correct |
49 |
Correct |
42 ms |
73544 KB |
Output is correct |
50 |
Correct |
42 ms |
73548 KB |
Output is correct |
51 |
Correct |
42 ms |
73592 KB |
Output is correct |
52 |
Correct |
43 ms |
73412 KB |
Output is correct |
53 |
Correct |
43 ms |
73348 KB |
Output is correct |
54 |
Correct |
44 ms |
73412 KB |
Output is correct |
55 |
Correct |
43 ms |
73412 KB |
Output is correct |
56 |
Correct |
45 ms |
73348 KB |
Output is correct |
57 |
Correct |
48 ms |
73140 KB |
Output is correct |
58 |
Correct |
46 ms |
73164 KB |
Output is correct |
59 |
Correct |
46 ms |
73268 KB |
Output is correct |
60 |
Correct |
46 ms |
73216 KB |
Output is correct |
61 |
Correct |
43 ms |
73396 KB |
Output is correct |
62 |
Correct |
43 ms |
73324 KB |
Output is correct |
63 |
Correct |
43 ms |
73332 KB |
Output is correct |
64 |
Correct |
44 ms |
73224 KB |
Output is correct |
65 |
Correct |
44 ms |
73152 KB |
Output is correct |
66 |
Correct |
45 ms |
73172 KB |
Output is correct |
67 |
Correct |
46 ms |
73156 KB |
Output is correct |
68 |
Correct |
47 ms |
73596 KB |
Output is correct |
69 |
Correct |
43 ms |
73412 KB |
Output is correct |
70 |
Correct |
43 ms |
73348 KB |
Output is correct |
71 |
Correct |
46 ms |
73308 KB |
Output is correct |
72 |
Correct |
44 ms |
73220 KB |
Output is correct |
73 |
Correct |
46 ms |
73248 KB |
Output is correct |
74 |
Correct |
44 ms |
73292 KB |
Output is correct |
75 |
Correct |
43 ms |
73280 KB |
Output is correct |
76 |
Correct |
44 ms |
73292 KB |
Output is correct |
77 |
Correct |
45 ms |
73156 KB |
Output is correct |
78 |
Correct |
44 ms |
73220 KB |
Output is correct |
79 |
Correct |
43 ms |
73168 KB |
Output is correct |
80 |
Correct |
43 ms |
73292 KB |
Output is correct |
81 |
Correct |
43 ms |
73304 KB |
Output is correct |
82 |
Correct |
43 ms |
73284 KB |
Output is correct |
83 |
Correct |
43 ms |
73160 KB |
Output is correct |
84 |
Correct |
43 ms |
73284 KB |
Output is correct |
85 |
Correct |
46 ms |
73180 KB |
Output is correct |
86 |
Correct |
46 ms |
73228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
73028 KB |
Output is correct |
2 |
Correct |
39 ms |
73036 KB |
Output is correct |
3 |
Correct |
40 ms |
73036 KB |
Output is correct |
4 |
Correct |
41 ms |
73132 KB |
Output is correct |
5 |
Correct |
40 ms |
73036 KB |
Output is correct |
6 |
Correct |
40 ms |
73036 KB |
Output is correct |
7 |
Correct |
39 ms |
73084 KB |
Output is correct |
8 |
Correct |
42 ms |
73104 KB |
Output is correct |
9 |
Correct |
42 ms |
73160 KB |
Output is correct |
10 |
Correct |
45 ms |
73144 KB |
Output is correct |
11 |
Correct |
39 ms |
73160 KB |
Output is correct |
12 |
Correct |
41 ms |
73216 KB |
Output is correct |
13 |
Correct |
40 ms |
73028 KB |
Output is correct |
14 |
Correct |
40 ms |
73164 KB |
Output is correct |
15 |
Correct |
40 ms |
73108 KB |
Output is correct |
16 |
Correct |
40 ms |
73036 KB |
Output is correct |
17 |
Correct |
39 ms |
73036 KB |
Output is correct |
18 |
Correct |
40 ms |
73036 KB |
Output is correct |
19 |
Correct |
40 ms |
73076 KB |
Output is correct |
20 |
Correct |
40 ms |
73068 KB |
Output is correct |
21 |
Correct |
40 ms |
73048 KB |
Output is correct |
22 |
Correct |
40 ms |
73108 KB |
Output is correct |
23 |
Correct |
41 ms |
73032 KB |
Output is correct |
24 |
Correct |
41 ms |
73052 KB |
Output is correct |
25 |
Correct |
42 ms |
73120 KB |
Output is correct |
26 |
Correct |
44 ms |
73064 KB |
Output is correct |
27 |
Correct |
41 ms |
73140 KB |
Output is correct |
28 |
Correct |
40 ms |
73044 KB |
Output is correct |
29 |
Correct |
44 ms |
73076 KB |
Output is correct |
30 |
Correct |
40 ms |
73036 KB |
Output is correct |
31 |
Correct |
44 ms |
73036 KB |
Output is correct |
32 |
Correct |
40 ms |
73076 KB |
Output is correct |
33 |
Correct |
40 ms |
73028 KB |
Output is correct |
34 |
Correct |
40 ms |
73032 KB |
Output is correct |
35 |
Correct |
39 ms |
73148 KB |
Output is correct |
36 |
Correct |
40 ms |
73024 KB |
Output is correct |
37 |
Correct |
40 ms |
73076 KB |
Output is correct |
38 |
Correct |
40 ms |
73028 KB |
Output is correct |
39 |
Correct |
40 ms |
73116 KB |
Output is correct |
40 |
Correct |
44 ms |
73028 KB |
Output is correct |
41 |
Correct |
40 ms |
73036 KB |
Output is correct |
42 |
Correct |
40 ms |
73140 KB |
Output is correct |
43 |
Correct |
40 ms |
73140 KB |
Output is correct |
44 |
Correct |
42 ms |
73028 KB |
Output is correct |
45 |
Correct |
42 ms |
73028 KB |
Output is correct |
46 |
Correct |
44 ms |
73112 KB |
Output is correct |
47 |
Correct |
44 ms |
73184 KB |
Output is correct |
48 |
Correct |
45 ms |
73216 KB |
Output is correct |
49 |
Correct |
42 ms |
73544 KB |
Output is correct |
50 |
Correct |
42 ms |
73548 KB |
Output is correct |
51 |
Correct |
42 ms |
73592 KB |
Output is correct |
52 |
Correct |
43 ms |
73412 KB |
Output is correct |
53 |
Correct |
43 ms |
73348 KB |
Output is correct |
54 |
Correct |
44 ms |
73412 KB |
Output is correct |
55 |
Correct |
43 ms |
73412 KB |
Output is correct |
56 |
Correct |
45 ms |
73348 KB |
Output is correct |
57 |
Correct |
48 ms |
73140 KB |
Output is correct |
58 |
Correct |
46 ms |
73164 KB |
Output is correct |
59 |
Correct |
46 ms |
73268 KB |
Output is correct |
60 |
Correct |
46 ms |
73216 KB |
Output is correct |
61 |
Correct |
43 ms |
73396 KB |
Output is correct |
62 |
Correct |
43 ms |
73324 KB |
Output is correct |
63 |
Correct |
43 ms |
73332 KB |
Output is correct |
64 |
Correct |
44 ms |
73224 KB |
Output is correct |
65 |
Correct |
44 ms |
73152 KB |
Output is correct |
66 |
Correct |
45 ms |
73172 KB |
Output is correct |
67 |
Correct |
46 ms |
73156 KB |
Output is correct |
68 |
Correct |
47 ms |
73596 KB |
Output is correct |
69 |
Correct |
43 ms |
73412 KB |
Output is correct |
70 |
Correct |
43 ms |
73348 KB |
Output is correct |
71 |
Correct |
46 ms |
73308 KB |
Output is correct |
72 |
Correct |
44 ms |
73220 KB |
Output is correct |
73 |
Correct |
46 ms |
73248 KB |
Output is correct |
74 |
Correct |
44 ms |
73292 KB |
Output is correct |
75 |
Correct |
43 ms |
73280 KB |
Output is correct |
76 |
Correct |
44 ms |
73292 KB |
Output is correct |
77 |
Correct |
45 ms |
73156 KB |
Output is correct |
78 |
Correct |
44 ms |
73220 KB |
Output is correct |
79 |
Correct |
43 ms |
73168 KB |
Output is correct |
80 |
Correct |
43 ms |
73292 KB |
Output is correct |
81 |
Correct |
43 ms |
73304 KB |
Output is correct |
82 |
Correct |
43 ms |
73284 KB |
Output is correct |
83 |
Correct |
43 ms |
73160 KB |
Output is correct |
84 |
Correct |
43 ms |
73284 KB |
Output is correct |
85 |
Correct |
46 ms |
73180 KB |
Output is correct |
86 |
Correct |
46 ms |
73228 KB |
Output is correct |
87 |
Correct |
52 ms |
73420 KB |
Output is correct |
88 |
Correct |
82 ms |
74188 KB |
Output is correct |
89 |
Correct |
182 ms |
77092 KB |
Output is correct |
90 |
Correct |
183 ms |
77124 KB |
Output is correct |
91 |
Correct |
193 ms |
77132 KB |
Output is correct |
92 |
Correct |
112 ms |
86340 KB |
Output is correct |
93 |
Correct |
112 ms |
86380 KB |
Output is correct |
94 |
Correct |
121 ms |
86340 KB |
Output is correct |
95 |
Correct |
120 ms |
81988 KB |
Output is correct |
96 |
Correct |
128 ms |
82260 KB |
Output is correct |
97 |
Correct |
123 ms |
82220 KB |
Output is correct |
98 |
Correct |
123 ms |
82244 KB |
Output is correct |
99 |
Correct |
116 ms |
81200 KB |
Output is correct |
100 |
Correct |
213 ms |
77252 KB |
Output is correct |
101 |
Correct |
250 ms |
77524 KB |
Output is correct |
102 |
Correct |
259 ms |
77588 KB |
Output is correct |
103 |
Correct |
250 ms |
77460 KB |
Output is correct |
104 |
Correct |
121 ms |
80952 KB |
Output is correct |
105 |
Correct |
118 ms |
81020 KB |
Output is correct |
106 |
Correct |
135 ms |
80936 KB |
Output is correct |
107 |
Correct |
172 ms |
76220 KB |
Output is correct |
108 |
Correct |
177 ms |
76364 KB |
Output is correct |
109 |
Correct |
184 ms |
76652 KB |
Output is correct |
110 |
Correct |
101 ms |
85772 KB |
Output is correct |
111 |
Correct |
115 ms |
81924 KB |
Output is correct |
112 |
Correct |
121 ms |
81700 KB |
Output is correct |
113 |
Correct |
106 ms |
80360 KB |
Output is correct |
114 |
Correct |
230 ms |
77324 KB |
Output is correct |
115 |
Correct |
247 ms |
76896 KB |
Output is correct |
116 |
Correct |
114 ms |
80324 KB |
Output is correct |
117 |
Correct |
116 ms |
78908 KB |
Output is correct |
118 |
Correct |
114 ms |
78052 KB |
Output is correct |
119 |
Correct |
119 ms |
77720 KB |
Output is correct |
120 |
Correct |
108 ms |
78276 KB |
Output is correct |
121 |
Correct |
112 ms |
77764 KB |
Output is correct |
122 |
Correct |
112 ms |
77488 KB |
Output is correct |
123 |
Correct |
125 ms |
78788 KB |
Output is correct |
124 |
Correct |
123 ms |
78048 KB |
Output is correct |
125 |
Correct |
123 ms |
77768 KB |
Output is correct |
126 |
Correct |
119 ms |
78672 KB |
Output is correct |
127 |
Correct |
123 ms |
77764 KB |
Output is correct |
128 |
Correct |
121 ms |
77508 KB |
Output is correct |