#include <bits/stdc++.h>
#define se second
#define fs first
#define mp make_pair
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const int N = 1e5 + 7;
const int Mod = 1e9 + 7;
const int szBL = 320;
const ll INF = 1e9 + 160907;
const int BASE = 137;
int n, m;
int a[N];
vector<int> ke[N];
vector<pair<int,int>> vec[N];
pair<int,int> qr[N];
struct fenwick_Tree {
int m;
int fen[N];
void init (int n) {
m = n;
rep (i, 1, m) fen[i] = 0;
}
void update (int pos, int val) {
for (; pos <= m; pos += pos & -pos) fen[pos] += val;
}
int get (int pos) {
int res = 0;
for (;pos > 0; pos -= pos & -pos) res += fen[pos];
return res;
}
}fen;
int cid[N], Head[N], par[N], high[N], nChild[N], numC = 1, in[N];
void pdfs (int u, int p) {
par[u] = p;
nChild[u] = 1;
high[u] = high[p] + 1;
iter (&v, ke[u]) if (v != p){
pdfs(v, u);
nChild[u] += nChild[v];
}
}
void hld (int u, int p) {
static int time_dfs = 0;
if (numC != cid[p]) Head[u] = u;
else Head[u] = Head[p];
in[u] = ++time_dfs;
cid[u] = numC;
int mxV = -1;
// cout << u <<" "<<cid[u] <<" "<<Head[u]<<"\n";
iter (&v, ke[u]) {
if (v != p && (v == -1 || nChild[v] > nChild[mxV])) {
mxV = v;
}
}
if (mxV != -1) hld (mxV, u);
iter (&v, ke[u]) {
if (v == p || v == mxV) continue;
++numC;
hld(v, u);
}
}
ll update (int u, int col) {
// cout << u <<" "<<col<<": \n";
static vector<pair<int,int>> chains; chains.clear();
for (; u != 0; u = par[Head[u]]) chains.push_back({cid[u], high[u] - high[Head[u]] + 1});
reverse(ALL(chains));
static vector<pair<int,int>> del; del.clear();
static vector<int> vals; vals.clear();
iter (&ch, chains) {
vector<pair<int,int>> &cur = vec[ch.fs];
int X = ch.se;
int pre = 0;
while (!cur.empty()) {
pair<int,int> &u = cur.back();
if (pre + u.se > X) {
u.se = u.se - (X - pre);
vals.push_back(u.fs);
del.push_back({u.fs, X - pre});
break;
}
else {
del.push_back(u);
vals.push_back(u.fs);
pre += u.se;
cur.pop_back();
}
}
cur.push_back({col, X});
}
// iter (&id, del) cout << id.fs<<","<<id.se << " ";
// cout<<"\n";
// iter (&id, vec[1]) cout << id.fs<<","<<id.se<<" "; cout <<"\n";
int ntot;
sort (ALL(vals));
vals.resize(ntot = unique(ALL(vals)) - vals.begin());
fen.init(ntot);
ll res = 0;
iter (&cur, del) {
int pos = lower_bound(ALL(vals), cur.fs) - vals.begin() + 1;
res += 1LL * (fen.get(ntot) - fen.get(pos)) * cur.se;
fen.update(pos, cur.se);
}
return res;
}
void solution () {
cin >> n;
rep (i, 1, n) cin >> a[i];
rep (i, 1, n - 1) {
int u, v;
cin >> u >> v;
qr[i] = {u, v};
ke[u].push_back(v);
ke[v].push_back(u);
}
pdfs(1, 0);
hld(1, 0);
vec[1].push_back({a[1], 1});
rep (i, 1, n - 1) {
int u, v;
tie(u, v) = qr[i];
cout << update (v, a[v]) <<"\n";
}
}
#define file(name) freopen(name".inp", "r", stdin); \
freopen(name".out", "w", stdout);
int main () {
// file("c");
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll num_Test = 1;
// cin >> num_Test;
while(num_Test--)
solution();
}
/*
nếu mình nghĩ sẽ thay đổi định nghĩa, kiểu dữ liệu của hàm hay mảng j thì mình phải nghĩ xem nó sẽ ảnh hưởng đến các phần nào
nghĩ xem mình có thể làm gì, có khả năng làm những gì với giới hạn bài toán, đặc điểm bài toans
nếu struggle, xem lại các dữ liệu bài toán xem mình đang xét trường hợp này có phù hợp với các dữ liệu bài toán cung cấp không ?
1
7 3
0010010
110
1
10 4
00xx000xx0x
11 11 1
1101
L R
L R
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5172 KB |
Output is correct |
6 |
Correct |
3 ms |
5212 KB |
Output is correct |
7 |
Correct |
3 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5212 KB |
Output is correct |
9 |
Correct |
3 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5176 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5116 KB |
Output is correct |
15 |
Correct |
3 ms |
5212 KB |
Output is correct |
16 |
Correct |
3 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
3 ms |
5212 KB |
Output is correct |
19 |
Correct |
2 ms |
5212 KB |
Output is correct |
20 |
Correct |
3 ms |
5180 KB |
Output is correct |
21 |
Correct |
2 ms |
5256 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5212 KB |
Output is correct |
26 |
Correct |
4 ms |
5212 KB |
Output is correct |
27 |
Correct |
4 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5176 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5212 KB |
Output is correct |
31 |
Correct |
3 ms |
5212 KB |
Output is correct |
32 |
Correct |
2 ms |
5212 KB |
Output is correct |
33 |
Correct |
2 ms |
5212 KB |
Output is correct |
34 |
Correct |
2 ms |
5212 KB |
Output is correct |
35 |
Correct |
2 ms |
5212 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
3 ms |
5212 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
3 ms |
5212 KB |
Output is correct |
40 |
Correct |
4 ms |
5212 KB |
Output is correct |
41 |
Correct |
2 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
5212 KB |
Output is correct |
43 |
Correct |
2 ms |
5212 KB |
Output is correct |
44 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5172 KB |
Output is correct |
6 |
Correct |
3 ms |
5212 KB |
Output is correct |
7 |
Correct |
3 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5212 KB |
Output is correct |
9 |
Correct |
3 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5176 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5116 KB |
Output is correct |
15 |
Correct |
3 ms |
5212 KB |
Output is correct |
16 |
Correct |
3 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
3 ms |
5212 KB |
Output is correct |
19 |
Correct |
2 ms |
5212 KB |
Output is correct |
20 |
Correct |
3 ms |
5180 KB |
Output is correct |
21 |
Correct |
2 ms |
5256 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5212 KB |
Output is correct |
26 |
Correct |
4 ms |
5212 KB |
Output is correct |
27 |
Correct |
4 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5176 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5212 KB |
Output is correct |
31 |
Correct |
3 ms |
5212 KB |
Output is correct |
32 |
Correct |
2 ms |
5212 KB |
Output is correct |
33 |
Correct |
2 ms |
5212 KB |
Output is correct |
34 |
Correct |
2 ms |
5212 KB |
Output is correct |
35 |
Correct |
2 ms |
5212 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
3 ms |
5212 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
3 ms |
5212 KB |
Output is correct |
40 |
Correct |
4 ms |
5212 KB |
Output is correct |
41 |
Correct |
2 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
5212 KB |
Output is correct |
43 |
Correct |
2 ms |
5212 KB |
Output is correct |
44 |
Correct |
2 ms |
5212 KB |
Output is correct |
45 |
Correct |
3 ms |
5212 KB |
Output is correct |
46 |
Correct |
7 ms |
5468 KB |
Output is correct |
47 |
Correct |
5 ms |
5468 KB |
Output is correct |
48 |
Correct |
6 ms |
5468 KB |
Output is correct |
49 |
Correct |
4 ms |
5756 KB |
Output is correct |
50 |
Correct |
4 ms |
5724 KB |
Output is correct |
51 |
Correct |
4 ms |
5724 KB |
Output is correct |
52 |
Correct |
5 ms |
5752 KB |
Output is correct |
53 |
Correct |
6 ms |
5728 KB |
Output is correct |
54 |
Correct |
6 ms |
5692 KB |
Output is correct |
55 |
Correct |
4 ms |
5724 KB |
Output is correct |
56 |
Correct |
3 ms |
5724 KB |
Output is correct |
57 |
Correct |
4 ms |
5496 KB |
Output is correct |
58 |
Correct |
5 ms |
5468 KB |
Output is correct |
59 |
Correct |
5 ms |
5468 KB |
Output is correct |
60 |
Correct |
6 ms |
5508 KB |
Output is correct |
61 |
Correct |
4 ms |
5724 KB |
Output is correct |
62 |
Correct |
4 ms |
5724 KB |
Output is correct |
63 |
Correct |
4 ms |
5724 KB |
Output is correct |
64 |
Correct |
4 ms |
5440 KB |
Output is correct |
65 |
Correct |
7 ms |
5468 KB |
Output is correct |
66 |
Correct |
5 ms |
5440 KB |
Output is correct |
67 |
Correct |
5 ms |
5556 KB |
Output is correct |
68 |
Correct |
5 ms |
5520 KB |
Output is correct |
69 |
Correct |
4 ms |
5724 KB |
Output is correct |
70 |
Correct |
5 ms |
5692 KB |
Output is correct |
71 |
Correct |
3 ms |
5468 KB |
Output is correct |
72 |
Correct |
4 ms |
5440 KB |
Output is correct |
73 |
Correct |
6 ms |
5540 KB |
Output is correct |
74 |
Correct |
4 ms |
5468 KB |
Output is correct |
75 |
Correct |
6 ms |
5580 KB |
Output is correct |
76 |
Correct |
4 ms |
5468 KB |
Output is correct |
77 |
Correct |
4 ms |
5468 KB |
Output is correct |
78 |
Correct |
3 ms |
5604 KB |
Output is correct |
79 |
Correct |
4 ms |
5416 KB |
Output is correct |
80 |
Correct |
4 ms |
5468 KB |
Output is correct |
81 |
Correct |
4 ms |
5436 KB |
Output is correct |
82 |
Correct |
5 ms |
5496 KB |
Output is correct |
83 |
Correct |
4 ms |
5468 KB |
Output is correct |
84 |
Correct |
4 ms |
5516 KB |
Output is correct |
85 |
Correct |
4 ms |
5468 KB |
Output is correct |
86 |
Correct |
4 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5172 KB |
Output is correct |
6 |
Correct |
3 ms |
5212 KB |
Output is correct |
7 |
Correct |
3 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5212 KB |
Output is correct |
9 |
Correct |
3 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5176 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5116 KB |
Output is correct |
15 |
Correct |
3 ms |
5212 KB |
Output is correct |
16 |
Correct |
3 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
3 ms |
5212 KB |
Output is correct |
19 |
Correct |
2 ms |
5212 KB |
Output is correct |
20 |
Correct |
3 ms |
5180 KB |
Output is correct |
21 |
Correct |
2 ms |
5256 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5212 KB |
Output is correct |
26 |
Correct |
4 ms |
5212 KB |
Output is correct |
27 |
Correct |
4 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5176 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5212 KB |
Output is correct |
31 |
Correct |
3 ms |
5212 KB |
Output is correct |
32 |
Correct |
2 ms |
5212 KB |
Output is correct |
33 |
Correct |
2 ms |
5212 KB |
Output is correct |
34 |
Correct |
2 ms |
5212 KB |
Output is correct |
35 |
Correct |
2 ms |
5212 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
3 ms |
5212 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
3 ms |
5212 KB |
Output is correct |
40 |
Correct |
4 ms |
5212 KB |
Output is correct |
41 |
Correct |
2 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
5212 KB |
Output is correct |
43 |
Correct |
2 ms |
5212 KB |
Output is correct |
44 |
Correct |
2 ms |
5212 KB |
Output is correct |
45 |
Correct |
3 ms |
5212 KB |
Output is correct |
46 |
Correct |
7 ms |
5468 KB |
Output is correct |
47 |
Correct |
5 ms |
5468 KB |
Output is correct |
48 |
Correct |
6 ms |
5468 KB |
Output is correct |
49 |
Correct |
4 ms |
5756 KB |
Output is correct |
50 |
Correct |
4 ms |
5724 KB |
Output is correct |
51 |
Correct |
4 ms |
5724 KB |
Output is correct |
52 |
Correct |
5 ms |
5752 KB |
Output is correct |
53 |
Correct |
6 ms |
5728 KB |
Output is correct |
54 |
Correct |
6 ms |
5692 KB |
Output is correct |
55 |
Correct |
4 ms |
5724 KB |
Output is correct |
56 |
Correct |
3 ms |
5724 KB |
Output is correct |
57 |
Correct |
4 ms |
5496 KB |
Output is correct |
58 |
Correct |
5 ms |
5468 KB |
Output is correct |
59 |
Correct |
5 ms |
5468 KB |
Output is correct |
60 |
Correct |
6 ms |
5508 KB |
Output is correct |
61 |
Correct |
4 ms |
5724 KB |
Output is correct |
62 |
Correct |
4 ms |
5724 KB |
Output is correct |
63 |
Correct |
4 ms |
5724 KB |
Output is correct |
64 |
Correct |
4 ms |
5440 KB |
Output is correct |
65 |
Correct |
7 ms |
5468 KB |
Output is correct |
66 |
Correct |
5 ms |
5440 KB |
Output is correct |
67 |
Correct |
5 ms |
5556 KB |
Output is correct |
68 |
Correct |
5 ms |
5520 KB |
Output is correct |
69 |
Correct |
4 ms |
5724 KB |
Output is correct |
70 |
Correct |
5 ms |
5692 KB |
Output is correct |
71 |
Correct |
3 ms |
5468 KB |
Output is correct |
72 |
Correct |
4 ms |
5440 KB |
Output is correct |
73 |
Correct |
6 ms |
5540 KB |
Output is correct |
74 |
Correct |
4 ms |
5468 KB |
Output is correct |
75 |
Correct |
6 ms |
5580 KB |
Output is correct |
76 |
Correct |
4 ms |
5468 KB |
Output is correct |
77 |
Correct |
4 ms |
5468 KB |
Output is correct |
78 |
Correct |
3 ms |
5604 KB |
Output is correct |
79 |
Correct |
4 ms |
5416 KB |
Output is correct |
80 |
Correct |
4 ms |
5468 KB |
Output is correct |
81 |
Correct |
4 ms |
5436 KB |
Output is correct |
82 |
Correct |
5 ms |
5496 KB |
Output is correct |
83 |
Correct |
4 ms |
5468 KB |
Output is correct |
84 |
Correct |
4 ms |
5516 KB |
Output is correct |
85 |
Correct |
4 ms |
5468 KB |
Output is correct |
86 |
Correct |
4 ms |
5468 KB |
Output is correct |
87 |
Correct |
11 ms |
6236 KB |
Output is correct |
88 |
Correct |
30 ms |
8416 KB |
Output is correct |
89 |
Correct |
125 ms |
15872 KB |
Output is correct |
90 |
Correct |
103 ms |
15940 KB |
Output is correct |
91 |
Correct |
111 ms |
15956 KB |
Output is correct |
92 |
Correct |
74 ms |
20352 KB |
Output is correct |
93 |
Correct |
50 ms |
20220 KB |
Output is correct |
94 |
Correct |
48 ms |
20308 KB |
Output is correct |
95 |
Correct |
53 ms |
19916 KB |
Output is correct |
96 |
Correct |
60 ms |
20252 KB |
Output is correct |
97 |
Correct |
64 ms |
20172 KB |
Output is correct |
98 |
Correct |
59 ms |
20172 KB |
Output is correct |
99 |
Correct |
48 ms |
18828 KB |
Output is correct |
100 |
Correct |
132 ms |
15176 KB |
Output is correct |
101 |
Correct |
135 ms |
15384 KB |
Output is correct |
102 |
Correct |
143 ms |
15440 KB |
Output is correct |
103 |
Correct |
140 ms |
15384 KB |
Output is correct |
104 |
Correct |
99 ms |
18700 KB |
Output is correct |
105 |
Correct |
104 ms |
18772 KB |
Output is correct |
106 |
Correct |
72 ms |
18812 KB |
Output is correct |
107 |
Correct |
85 ms |
15060 KB |
Output is correct |
108 |
Correct |
125 ms |
15224 KB |
Output is correct |
109 |
Correct |
102 ms |
15392 KB |
Output is correct |
110 |
Correct |
50 ms |
19540 KB |
Output is correct |
111 |
Correct |
57 ms |
19916 KB |
Output is correct |
112 |
Correct |
78 ms |
19612 KB |
Output is correct |
113 |
Correct |
55 ms |
18260 KB |
Output is correct |
114 |
Correct |
124 ms |
15152 KB |
Output is correct |
115 |
Correct |
162 ms |
14776 KB |
Output is correct |
116 |
Correct |
77 ms |
18168 KB |
Output is correct |
117 |
Correct |
58 ms |
16984 KB |
Output is correct |
118 |
Correct |
61 ms |
16352 KB |
Output is correct |
119 |
Correct |
58 ms |
16252 KB |
Output is correct |
120 |
Correct |
61 ms |
16432 KB |
Output is correct |
121 |
Correct |
52 ms |
16204 KB |
Output is correct |
122 |
Correct |
71 ms |
15896 KB |
Output is correct |
123 |
Correct |
78 ms |
17092 KB |
Output is correct |
124 |
Correct |
78 ms |
16444 KB |
Output is correct |
125 |
Correct |
60 ms |
16212 KB |
Output is correct |
126 |
Correct |
61 ms |
16724 KB |
Output is correct |
127 |
Correct |
84 ms |
16180 KB |
Output is correct |
128 |
Correct |
93 ms |
15872 KB |
Output is correct |