#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int)x.size()
#define FOR(x, y, z) for(int x = (y); x < (z); x++)
#define ROF(x, z, y) for(int x = (y-1); x >= (z); x--)
#define F0R(x, z) FOR(x, 0, z)
#define R0F(x, z) ROF(x, 0, z)
#define trav(x, y) for(auto&x:y)
using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;}
template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const char nl = '\n';
const int mxN = 1e5 + 10;
const int MOD = 1e9 + 7;
const long long infLL = 1e18;
const int inf = 1e9;
int n, a[mxN], par[mxN], heavy[mxN], depth[mxN], root[mxN], treePos[mxN];
pii e[mxN];
vi adj[mxN];
vpii vals[mxN];
typedef int node;
struct BIT{
vector<node> bit;
vpii ops;
int n;
void init(int x){
n=x+1;
bit.assign(n + 1,0);
}
node sum(int r){
node ret = 0;
for(r++; r ; r -= r & -r){
ret += bit[r];
}
return ret;
}
node sum(int l, int r){
return sum(r) - sum(l-1);
}
void add(int idx, node delta){
ops.pb({idx, delta});
for(idx++; idx < n; idx += idx & -idx){
bit[idx] += delta;
}
}
void clear(){
trav(x, ops){
add(x.f, -x.s);
}
ops.clear();
}
};
BIT B;
int dfs(int u, int p){
int sz = 1, mxSub = 0;
trav(v, adj[u]){
if(v == p)
continue;
par[v] = u;
depth[v] = depth[u] + 1;
int d = dfs(v, u);
if(ckmax(mxSub, d)) heavy[u] = v;
sz += d;
}
return sz;
}
ll query(int v){
int r = root[v], tot = depth[v]-depth[r]+1;
vpii tmp;
R0F(i, siz(vals[r])){
auto cur = vals[r][i];
if(cur.s >= tot){
tmp.pb({cur.f, tot});
break;
}
else{
tot -= cur.s;
tmp.pb(cur);
}
}
reverse(all(tmp));
ll res = 0;
trav(x, tmp){
res += (ll)x.s * B.sum(x.f-1);
B.add(x.f, x.s);
}
return res;
}
ll queryAns(int v){
ll ans = 0;
//cout << v << endl;
B.clear();
for(; v != -1; v = par[root[v]]) ans+= query(v);
return ans;
}
void upd(int v, int val){
int r = root[v], tot = depth[v] - depth[r] + 1;
//cout << depth[v] << ' ' << depth[r] << endl;
int tmp=tot;
while(vals[r].back().s < tot) {
tot -= vals[r].back().s;
vals[r].pop_back();
}
assert(vals[r].size());
vals[r].back().s -= tot;
if(vals[r].back().s == 0) vals[r].pop_back();
vals[r].pb({val, tmp});
}
void updAns(int v, int val){
for(; v != -1; v = par[root[v]]) upd(v, val);
}
void compress(){
map<int, int> m;
F0R(i, n) m[a[i]]=0;
int xx = 0;
trav(xd, m) xd.s = xx ++;
F0R(i, n) a[i] = m[a[i]];
//F0R(i, n) cout << a[i] << endl;
}
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0);
B.init(mxN);
cin >> n;
F0R(i, n) cin >> a[i];
compress();
F0R(i, n-1){
cin >> e[i].f >> e[i].s;
e[i].f--; e[i].s--;
adj[e[i].f].pb(e[i].s);
adj[e[i].s].pb(e[i].f);
}
//cout << "BRUH" << endl;
F0R(i, n) heavy[i] = -1;
par[0] = -1; depth[0] = 0;
dfs(0, -1);
F0R(i, n){
if(par[i] == -1 || heavy[par[i]] != i){
for(int j = i; j != -1; j = heavy[j]){
root[j] = i;
vals[i].pb({0, 1});
}
}
}
//cout << "BRUH" << endl;
//F0R(i, n){
// cout << "root[" << i << "] =" << root[i] << endl;
//}
//cout<< "HELLO" << endl;
updAns(0, a[0]);
F0R(i, n-1){
cout << queryAns(e[i].f) << nl;
//cout << "HELLO" << endl;
updAns(e[i].s, a[e[i].s]);
}
//cout << "BRUH" << endl;
//cout << "HI" << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5504 KB |
Output is correct |
2 |
Correct |
4 ms |
5504 KB |
Output is correct |
3 |
Correct |
5 ms |
5504 KB |
Output is correct |
4 |
Correct |
4 ms |
5504 KB |
Output is correct |
5 |
Correct |
6 ms |
5504 KB |
Output is correct |
6 |
Correct |
5 ms |
5504 KB |
Output is correct |
7 |
Correct |
5 ms |
5504 KB |
Output is correct |
8 |
Correct |
4 ms |
5504 KB |
Output is correct |
9 |
Correct |
5 ms |
5504 KB |
Output is correct |
10 |
Correct |
5 ms |
5504 KB |
Output is correct |
11 |
Correct |
5 ms |
5504 KB |
Output is correct |
12 |
Correct |
5 ms |
5504 KB |
Output is correct |
13 |
Correct |
4 ms |
5504 KB |
Output is correct |
14 |
Correct |
5 ms |
5504 KB |
Output is correct |
15 |
Correct |
5 ms |
5504 KB |
Output is correct |
16 |
Correct |
5 ms |
5504 KB |
Output is correct |
17 |
Correct |
5 ms |
5504 KB |
Output is correct |
18 |
Correct |
5 ms |
5520 KB |
Output is correct |
19 |
Correct |
4 ms |
5504 KB |
Output is correct |
20 |
Correct |
5 ms |
5504 KB |
Output is correct |
21 |
Correct |
4 ms |
5504 KB |
Output is correct |
22 |
Correct |
4 ms |
5504 KB |
Output is correct |
23 |
Correct |
5 ms |
5504 KB |
Output is correct |
24 |
Correct |
5 ms |
5504 KB |
Output is correct |
25 |
Correct |
5 ms |
5504 KB |
Output is correct |
26 |
Correct |
5 ms |
5504 KB |
Output is correct |
27 |
Correct |
4 ms |
5504 KB |
Output is correct |
28 |
Correct |
4 ms |
5504 KB |
Output is correct |
29 |
Correct |
5 ms |
5504 KB |
Output is correct |
30 |
Correct |
5 ms |
5504 KB |
Output is correct |
31 |
Correct |
5 ms |
5504 KB |
Output is correct |
32 |
Correct |
5 ms |
5504 KB |
Output is correct |
33 |
Correct |
5 ms |
5504 KB |
Output is correct |
34 |
Correct |
5 ms |
5504 KB |
Output is correct |
35 |
Correct |
5 ms |
5504 KB |
Output is correct |
36 |
Correct |
4 ms |
5504 KB |
Output is correct |
37 |
Correct |
4 ms |
5504 KB |
Output is correct |
38 |
Correct |
5 ms |
5504 KB |
Output is correct |
39 |
Correct |
5 ms |
5504 KB |
Output is correct |
40 |
Correct |
5 ms |
5504 KB |
Output is correct |
41 |
Correct |
5 ms |
5504 KB |
Output is correct |
42 |
Correct |
5 ms |
5504 KB |
Output is correct |
43 |
Correct |
4 ms |
5504 KB |
Output is correct |
44 |
Correct |
5 ms |
5504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5504 KB |
Output is correct |
2 |
Correct |
4 ms |
5504 KB |
Output is correct |
3 |
Correct |
5 ms |
5504 KB |
Output is correct |
4 |
Correct |
4 ms |
5504 KB |
Output is correct |
5 |
Correct |
6 ms |
5504 KB |
Output is correct |
6 |
Correct |
5 ms |
5504 KB |
Output is correct |
7 |
Correct |
5 ms |
5504 KB |
Output is correct |
8 |
Correct |
4 ms |
5504 KB |
Output is correct |
9 |
Correct |
5 ms |
5504 KB |
Output is correct |
10 |
Correct |
5 ms |
5504 KB |
Output is correct |
11 |
Correct |
5 ms |
5504 KB |
Output is correct |
12 |
Correct |
5 ms |
5504 KB |
Output is correct |
13 |
Correct |
4 ms |
5504 KB |
Output is correct |
14 |
Correct |
5 ms |
5504 KB |
Output is correct |
15 |
Correct |
5 ms |
5504 KB |
Output is correct |
16 |
Correct |
5 ms |
5504 KB |
Output is correct |
17 |
Correct |
5 ms |
5504 KB |
Output is correct |
18 |
Correct |
5 ms |
5520 KB |
Output is correct |
19 |
Correct |
4 ms |
5504 KB |
Output is correct |
20 |
Correct |
5 ms |
5504 KB |
Output is correct |
21 |
Correct |
4 ms |
5504 KB |
Output is correct |
22 |
Correct |
4 ms |
5504 KB |
Output is correct |
23 |
Correct |
5 ms |
5504 KB |
Output is correct |
24 |
Correct |
5 ms |
5504 KB |
Output is correct |
25 |
Correct |
5 ms |
5504 KB |
Output is correct |
26 |
Correct |
5 ms |
5504 KB |
Output is correct |
27 |
Correct |
4 ms |
5504 KB |
Output is correct |
28 |
Correct |
4 ms |
5504 KB |
Output is correct |
29 |
Correct |
5 ms |
5504 KB |
Output is correct |
30 |
Correct |
5 ms |
5504 KB |
Output is correct |
31 |
Correct |
5 ms |
5504 KB |
Output is correct |
32 |
Correct |
5 ms |
5504 KB |
Output is correct |
33 |
Correct |
5 ms |
5504 KB |
Output is correct |
34 |
Correct |
5 ms |
5504 KB |
Output is correct |
35 |
Correct |
5 ms |
5504 KB |
Output is correct |
36 |
Correct |
4 ms |
5504 KB |
Output is correct |
37 |
Correct |
4 ms |
5504 KB |
Output is correct |
38 |
Correct |
5 ms |
5504 KB |
Output is correct |
39 |
Correct |
5 ms |
5504 KB |
Output is correct |
40 |
Correct |
5 ms |
5504 KB |
Output is correct |
41 |
Correct |
5 ms |
5504 KB |
Output is correct |
42 |
Correct |
5 ms |
5504 KB |
Output is correct |
43 |
Correct |
4 ms |
5504 KB |
Output is correct |
44 |
Correct |
5 ms |
5504 KB |
Output is correct |
45 |
Correct |
6 ms |
5632 KB |
Output is correct |
46 |
Correct |
11 ms |
5888 KB |
Output is correct |
47 |
Correct |
11 ms |
5888 KB |
Output is correct |
48 |
Correct |
11 ms |
5888 KB |
Output is correct |
49 |
Correct |
9 ms |
6144 KB |
Output is correct |
50 |
Correct |
8 ms |
6144 KB |
Output is correct |
51 |
Correct |
9 ms |
6144 KB |
Output is correct |
52 |
Correct |
10 ms |
6016 KB |
Output is correct |
53 |
Correct |
9 ms |
6016 KB |
Output is correct |
54 |
Correct |
10 ms |
6016 KB |
Output is correct |
55 |
Correct |
9 ms |
6176 KB |
Output is correct |
56 |
Correct |
9 ms |
6016 KB |
Output is correct |
57 |
Correct |
13 ms |
5888 KB |
Output is correct |
58 |
Correct |
16 ms |
5888 KB |
Output is correct |
59 |
Correct |
14 ms |
5888 KB |
Output is correct |
60 |
Correct |
16 ms |
6016 KB |
Output is correct |
61 |
Correct |
9 ms |
6016 KB |
Output is correct |
62 |
Correct |
9 ms |
6016 KB |
Output is correct |
63 |
Correct |
9 ms |
6016 KB |
Output is correct |
64 |
Correct |
9 ms |
5888 KB |
Output is correct |
65 |
Correct |
12 ms |
5888 KB |
Output is correct |
66 |
Correct |
10 ms |
5888 KB |
Output is correct |
67 |
Correct |
11 ms |
5888 KB |
Output is correct |
68 |
Correct |
8 ms |
6144 KB |
Output is correct |
69 |
Correct |
9 ms |
6016 KB |
Output is correct |
70 |
Correct |
9 ms |
6016 KB |
Output is correct |
71 |
Correct |
8 ms |
6016 KB |
Output is correct |
72 |
Correct |
14 ms |
5888 KB |
Output is correct |
73 |
Correct |
13 ms |
5888 KB |
Output is correct |
74 |
Correct |
9 ms |
6016 KB |
Output is correct |
75 |
Correct |
9 ms |
6016 KB |
Output is correct |
76 |
Correct |
9 ms |
5920 KB |
Output is correct |
77 |
Correct |
9 ms |
5920 KB |
Output is correct |
78 |
Correct |
9 ms |
5888 KB |
Output is correct |
79 |
Correct |
8 ms |
5888 KB |
Output is correct |
80 |
Correct |
8 ms |
5888 KB |
Output is correct |
81 |
Correct |
10 ms |
6016 KB |
Output is correct |
82 |
Correct |
9 ms |
5888 KB |
Output is correct |
83 |
Correct |
9 ms |
5992 KB |
Output is correct |
84 |
Correct |
8 ms |
5888 KB |
Output is correct |
85 |
Correct |
11 ms |
5888 KB |
Output is correct |
86 |
Correct |
8 ms |
5888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5504 KB |
Output is correct |
2 |
Correct |
4 ms |
5504 KB |
Output is correct |
3 |
Correct |
5 ms |
5504 KB |
Output is correct |
4 |
Correct |
4 ms |
5504 KB |
Output is correct |
5 |
Correct |
6 ms |
5504 KB |
Output is correct |
6 |
Correct |
5 ms |
5504 KB |
Output is correct |
7 |
Correct |
5 ms |
5504 KB |
Output is correct |
8 |
Correct |
4 ms |
5504 KB |
Output is correct |
9 |
Correct |
5 ms |
5504 KB |
Output is correct |
10 |
Correct |
5 ms |
5504 KB |
Output is correct |
11 |
Correct |
5 ms |
5504 KB |
Output is correct |
12 |
Correct |
5 ms |
5504 KB |
Output is correct |
13 |
Correct |
4 ms |
5504 KB |
Output is correct |
14 |
Correct |
5 ms |
5504 KB |
Output is correct |
15 |
Correct |
5 ms |
5504 KB |
Output is correct |
16 |
Correct |
5 ms |
5504 KB |
Output is correct |
17 |
Correct |
5 ms |
5504 KB |
Output is correct |
18 |
Correct |
5 ms |
5520 KB |
Output is correct |
19 |
Correct |
4 ms |
5504 KB |
Output is correct |
20 |
Correct |
5 ms |
5504 KB |
Output is correct |
21 |
Correct |
4 ms |
5504 KB |
Output is correct |
22 |
Correct |
4 ms |
5504 KB |
Output is correct |
23 |
Correct |
5 ms |
5504 KB |
Output is correct |
24 |
Correct |
5 ms |
5504 KB |
Output is correct |
25 |
Correct |
5 ms |
5504 KB |
Output is correct |
26 |
Correct |
5 ms |
5504 KB |
Output is correct |
27 |
Correct |
4 ms |
5504 KB |
Output is correct |
28 |
Correct |
4 ms |
5504 KB |
Output is correct |
29 |
Correct |
5 ms |
5504 KB |
Output is correct |
30 |
Correct |
5 ms |
5504 KB |
Output is correct |
31 |
Correct |
5 ms |
5504 KB |
Output is correct |
32 |
Correct |
5 ms |
5504 KB |
Output is correct |
33 |
Correct |
5 ms |
5504 KB |
Output is correct |
34 |
Correct |
5 ms |
5504 KB |
Output is correct |
35 |
Correct |
5 ms |
5504 KB |
Output is correct |
36 |
Correct |
4 ms |
5504 KB |
Output is correct |
37 |
Correct |
4 ms |
5504 KB |
Output is correct |
38 |
Correct |
5 ms |
5504 KB |
Output is correct |
39 |
Correct |
5 ms |
5504 KB |
Output is correct |
40 |
Correct |
5 ms |
5504 KB |
Output is correct |
41 |
Correct |
5 ms |
5504 KB |
Output is correct |
42 |
Correct |
5 ms |
5504 KB |
Output is correct |
43 |
Correct |
4 ms |
5504 KB |
Output is correct |
44 |
Correct |
5 ms |
5504 KB |
Output is correct |
45 |
Correct |
6 ms |
5632 KB |
Output is correct |
46 |
Correct |
11 ms |
5888 KB |
Output is correct |
47 |
Correct |
11 ms |
5888 KB |
Output is correct |
48 |
Correct |
11 ms |
5888 KB |
Output is correct |
49 |
Correct |
9 ms |
6144 KB |
Output is correct |
50 |
Correct |
8 ms |
6144 KB |
Output is correct |
51 |
Correct |
9 ms |
6144 KB |
Output is correct |
52 |
Correct |
10 ms |
6016 KB |
Output is correct |
53 |
Correct |
9 ms |
6016 KB |
Output is correct |
54 |
Correct |
10 ms |
6016 KB |
Output is correct |
55 |
Correct |
9 ms |
6176 KB |
Output is correct |
56 |
Correct |
9 ms |
6016 KB |
Output is correct |
57 |
Correct |
13 ms |
5888 KB |
Output is correct |
58 |
Correct |
16 ms |
5888 KB |
Output is correct |
59 |
Correct |
14 ms |
5888 KB |
Output is correct |
60 |
Correct |
16 ms |
6016 KB |
Output is correct |
61 |
Correct |
9 ms |
6016 KB |
Output is correct |
62 |
Correct |
9 ms |
6016 KB |
Output is correct |
63 |
Correct |
9 ms |
6016 KB |
Output is correct |
64 |
Correct |
9 ms |
5888 KB |
Output is correct |
65 |
Correct |
12 ms |
5888 KB |
Output is correct |
66 |
Correct |
10 ms |
5888 KB |
Output is correct |
67 |
Correct |
11 ms |
5888 KB |
Output is correct |
68 |
Correct |
8 ms |
6144 KB |
Output is correct |
69 |
Correct |
9 ms |
6016 KB |
Output is correct |
70 |
Correct |
9 ms |
6016 KB |
Output is correct |
71 |
Correct |
8 ms |
6016 KB |
Output is correct |
72 |
Correct |
14 ms |
5888 KB |
Output is correct |
73 |
Correct |
13 ms |
5888 KB |
Output is correct |
74 |
Correct |
9 ms |
6016 KB |
Output is correct |
75 |
Correct |
9 ms |
6016 KB |
Output is correct |
76 |
Correct |
9 ms |
5920 KB |
Output is correct |
77 |
Correct |
9 ms |
5920 KB |
Output is correct |
78 |
Correct |
9 ms |
5888 KB |
Output is correct |
79 |
Correct |
8 ms |
5888 KB |
Output is correct |
80 |
Correct |
8 ms |
5888 KB |
Output is correct |
81 |
Correct |
10 ms |
6016 KB |
Output is correct |
82 |
Correct |
9 ms |
5888 KB |
Output is correct |
83 |
Correct |
9 ms |
5992 KB |
Output is correct |
84 |
Correct |
8 ms |
5888 KB |
Output is correct |
85 |
Correct |
11 ms |
5888 KB |
Output is correct |
86 |
Correct |
8 ms |
5888 KB |
Output is correct |
87 |
Correct |
26 ms |
6528 KB |
Output is correct |
88 |
Correct |
75 ms |
8568 KB |
Output is correct |
89 |
Correct |
348 ms |
15736 KB |
Output is correct |
90 |
Correct |
323 ms |
15736 KB |
Output is correct |
91 |
Correct |
309 ms |
15736 KB |
Output is correct |
92 |
Correct |
172 ms |
23800 KB |
Output is correct |
93 |
Correct |
164 ms |
23800 KB |
Output is correct |
94 |
Correct |
161 ms |
23800 KB |
Output is correct |
95 |
Correct |
167 ms |
20464 KB |
Output is correct |
96 |
Correct |
222 ms |
20848 KB |
Output is correct |
97 |
Correct |
177 ms |
20848 KB |
Output is correct |
98 |
Correct |
178 ms |
20848 KB |
Output is correct |
99 |
Correct |
181 ms |
19960 KB |
Output is correct |
100 |
Correct |
429 ms |
15096 KB |
Output is correct |
101 |
Correct |
461 ms |
15480 KB |
Output is correct |
102 |
Correct |
473 ms |
15376 KB |
Output is correct |
103 |
Correct |
468 ms |
15352 KB |
Output is correct |
104 |
Correct |
189 ms |
19704 KB |
Output is correct |
105 |
Correct |
190 ms |
19704 KB |
Output is correct |
106 |
Correct |
206 ms |
19832 KB |
Output is correct |
107 |
Correct |
234 ms |
15000 KB |
Output is correct |
108 |
Correct |
238 ms |
15228 KB |
Output is correct |
109 |
Correct |
289 ms |
15480 KB |
Output is correct |
110 |
Correct |
106 ms |
21788 KB |
Output is correct |
111 |
Correct |
171 ms |
20468 KB |
Output is correct |
112 |
Correct |
119 ms |
20084 KB |
Output is correct |
113 |
Correct |
125 ms |
19056 KB |
Output is correct |
114 |
Correct |
444 ms |
15096 KB |
Output is correct |
115 |
Correct |
420 ms |
14716 KB |
Output is correct |
116 |
Correct |
130 ms |
19056 KB |
Output is correct |
117 |
Correct |
184 ms |
17528 KB |
Output is correct |
118 |
Correct |
180 ms |
17012 KB |
Output is correct |
119 |
Correct |
180 ms |
16636 KB |
Output is correct |
120 |
Correct |
121 ms |
17008 KB |
Output is correct |
121 |
Correct |
131 ms |
16500 KB |
Output is correct |
122 |
Correct |
123 ms |
16116 KB |
Output is correct |
123 |
Correct |
193 ms |
17528 KB |
Output is correct |
124 |
Correct |
191 ms |
17012 KB |
Output is correct |
125 |
Correct |
201 ms |
16856 KB |
Output is correct |
126 |
Correct |
134 ms |
17140 KB |
Output is correct |
127 |
Correct |
137 ms |
16376 KB |
Output is correct |
128 |
Correct |
150 ms |
16124 KB |
Output is correct |