/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 2e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
int n, a[N], b[N], c[N], h[N], par[N], head[N], subt[N], fen[N];
int tim, tin[N], tout[N];
vector<int> comp, adj[N];
vector<pll> g[N], rbk;
void upd(int ind, int val) {
if(ind == 0) return;
rbk.pb({ind, val});
while(ind < N) {
fen[ind] += val; ind += (ind&(-ind));
}
}
void rolbak() {
while(size(rbk) > 0) {
upd(rbk.back().F, -rbk.back().S);
rbk.pop_back();
rbk.pop_back();
}
}
int qry(int ind) {
int res = 0;
while(ind > 0) {
res += fen[ind]; ind -= (ind&(-ind));
}
return res;
}
void init(int v, int p=0) {
par[v] = p, subt[v] = 1, h[v] = h[p] + 1;
for(int u : adj[v]) {
if(u == p) continue;
init(u, v); subt[v] += subt[u];
}
}
void dfs(int v, int p) {
head[v] = p, tin[v] = ++tim;
int mx = 0;
for(int u : adj[v]) {
if(u == par[v]) continue;
mx = (subt[mx]>subt[u] ? mx : u);
}
for(int u : adj[v]) {
if(u==par[v] || u==mx) continue;
dfs(u, u);
}
if(mx != 0) dfs(mx, p);
tout[v] = tim+1;
}
ll query(ll v) {
if(v == 0) return 0;
ll res = 0, u = head[v], num = h[v]-h[head[v]]+1;
vector<pll> tmp;
for(int i=size(g[u])-1; num>0&&i>=0; i--) {
tmp.pb({g[u][i].F, min(g[u][i].S, num)});
num -= min(g[u][i].S, num);
}
reverse(all(tmp));
for(auto it : tmp) {
res += (it.S*qry(it.F-1));
upd(it.F, it.S);
}
return res+query(par[u]);
}
void update(int v, ll val, int flg=0) {
if(v == 0) return;
if(flg == 1) {
g[head[v]].clear();
g[head[v]].pb({val, h[v]-h[head[v]]+1});
update(par[head[v]], val);
return;
}
ll u = head[v], num = h[v]-h[head[v]]+1;
while(size(g[u])>0 && num>0) {
if(num>=g[u].back().S) {
num -= g[u].back().S;
g[u].pop_back();
} else {
g[u].back().S -= num; num=0;
}
}
g[u].pb({val, h[v]-h[u]+1});
update(par[u], val);
}
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>n;
for(int i=1; i<=n; i++) {
cin>>c[i]; comp.pb(c[i]);
}
sort(all(comp)); comp.resize(unique(all(comp)) - comp.begin());
for(int i=1; i<=n; i++) {
c[i] = lower_bound(all(comp), c[i]) - comp.begin() + 1;
}
vector<pii> Q;
for(int v,u,i=1; i<n; i++) {
cin>>v>>u;
adj[v].pb(u); adj[u].pb(v);
Q.pb({v, u});
}
init(1);
dfs(1, 1);
g[1].pb({c[1], 1});
for(auto it : Q) {
if(h[it.F] > h[it.S]) swap(it.F, it.S);
cout<<query(it.F)<<endl;
update(it.S, c[it.S], 1);
rolbak();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
16984 KB |
Output is correct |
3 |
Correct |
4 ms |
17040 KB |
Output is correct |
4 |
Correct |
5 ms |
16988 KB |
Output is correct |
5 |
Correct |
4 ms |
17056 KB |
Output is correct |
6 |
Correct |
6 ms |
16988 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
6 ms |
17244 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
5 ms |
16988 KB |
Output is correct |
11 |
Correct |
4 ms |
16988 KB |
Output is correct |
12 |
Correct |
5 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
17084 KB |
Output is correct |
14 |
Correct |
5 ms |
17112 KB |
Output is correct |
15 |
Correct |
5 ms |
16988 KB |
Output is correct |
16 |
Correct |
5 ms |
16988 KB |
Output is correct |
17 |
Correct |
5 ms |
16988 KB |
Output is correct |
18 |
Correct |
4 ms |
16988 KB |
Output is correct |
19 |
Correct |
4 ms |
16988 KB |
Output is correct |
20 |
Correct |
4 ms |
16988 KB |
Output is correct |
21 |
Correct |
4 ms |
16988 KB |
Output is correct |
22 |
Correct |
5 ms |
16984 KB |
Output is correct |
23 |
Correct |
5 ms |
16988 KB |
Output is correct |
24 |
Correct |
5 ms |
16984 KB |
Output is correct |
25 |
Correct |
5 ms |
16988 KB |
Output is correct |
26 |
Correct |
4 ms |
16988 KB |
Output is correct |
27 |
Correct |
4 ms |
17052 KB |
Output is correct |
28 |
Correct |
4 ms |
16984 KB |
Output is correct |
29 |
Correct |
4 ms |
16984 KB |
Output is correct |
30 |
Correct |
5 ms |
16984 KB |
Output is correct |
31 |
Correct |
6 ms |
16984 KB |
Output is correct |
32 |
Correct |
4 ms |
16988 KB |
Output is correct |
33 |
Correct |
4 ms |
16988 KB |
Output is correct |
34 |
Correct |
4 ms |
16988 KB |
Output is correct |
35 |
Correct |
5 ms |
16988 KB |
Output is correct |
36 |
Correct |
4 ms |
16984 KB |
Output is correct |
37 |
Correct |
4 ms |
16988 KB |
Output is correct |
38 |
Correct |
5 ms |
17088 KB |
Output is correct |
39 |
Correct |
5 ms |
16988 KB |
Output is correct |
40 |
Correct |
4 ms |
16988 KB |
Output is correct |
41 |
Correct |
5 ms |
16988 KB |
Output is correct |
42 |
Correct |
5 ms |
16988 KB |
Output is correct |
43 |
Correct |
4 ms |
17052 KB |
Output is correct |
44 |
Correct |
4 ms |
16988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
16984 KB |
Output is correct |
3 |
Correct |
4 ms |
17040 KB |
Output is correct |
4 |
Correct |
5 ms |
16988 KB |
Output is correct |
5 |
Correct |
4 ms |
17056 KB |
Output is correct |
6 |
Correct |
6 ms |
16988 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
6 ms |
17244 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
5 ms |
16988 KB |
Output is correct |
11 |
Correct |
4 ms |
16988 KB |
Output is correct |
12 |
Correct |
5 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
17084 KB |
Output is correct |
14 |
Correct |
5 ms |
17112 KB |
Output is correct |
15 |
Correct |
5 ms |
16988 KB |
Output is correct |
16 |
Correct |
5 ms |
16988 KB |
Output is correct |
17 |
Correct |
5 ms |
16988 KB |
Output is correct |
18 |
Correct |
4 ms |
16988 KB |
Output is correct |
19 |
Correct |
4 ms |
16988 KB |
Output is correct |
20 |
Correct |
4 ms |
16988 KB |
Output is correct |
21 |
Correct |
4 ms |
16988 KB |
Output is correct |
22 |
Correct |
5 ms |
16984 KB |
Output is correct |
23 |
Correct |
5 ms |
16988 KB |
Output is correct |
24 |
Correct |
5 ms |
16984 KB |
Output is correct |
25 |
Correct |
5 ms |
16988 KB |
Output is correct |
26 |
Correct |
4 ms |
16988 KB |
Output is correct |
27 |
Correct |
4 ms |
17052 KB |
Output is correct |
28 |
Correct |
4 ms |
16984 KB |
Output is correct |
29 |
Correct |
4 ms |
16984 KB |
Output is correct |
30 |
Correct |
5 ms |
16984 KB |
Output is correct |
31 |
Correct |
6 ms |
16984 KB |
Output is correct |
32 |
Correct |
4 ms |
16988 KB |
Output is correct |
33 |
Correct |
4 ms |
16988 KB |
Output is correct |
34 |
Correct |
4 ms |
16988 KB |
Output is correct |
35 |
Correct |
5 ms |
16988 KB |
Output is correct |
36 |
Correct |
4 ms |
16984 KB |
Output is correct |
37 |
Correct |
4 ms |
16988 KB |
Output is correct |
38 |
Correct |
5 ms |
17088 KB |
Output is correct |
39 |
Correct |
5 ms |
16988 KB |
Output is correct |
40 |
Correct |
4 ms |
16988 KB |
Output is correct |
41 |
Correct |
5 ms |
16988 KB |
Output is correct |
42 |
Correct |
5 ms |
16988 KB |
Output is correct |
43 |
Correct |
4 ms |
17052 KB |
Output is correct |
44 |
Correct |
4 ms |
16988 KB |
Output is correct |
45 |
Correct |
5 ms |
16988 KB |
Output is correct |
46 |
Correct |
9 ms |
17492 KB |
Output is correct |
47 |
Correct |
8 ms |
17496 KB |
Output is correct |
48 |
Correct |
8 ms |
17384 KB |
Output is correct |
49 |
Correct |
8 ms |
17500 KB |
Output is correct |
50 |
Correct |
7 ms |
17496 KB |
Output is correct |
51 |
Correct |
7 ms |
17500 KB |
Output is correct |
52 |
Correct |
7 ms |
17500 KB |
Output is correct |
53 |
Correct |
8 ms |
17500 KB |
Output is correct |
54 |
Correct |
7 ms |
17500 KB |
Output is correct |
55 |
Correct |
8 ms |
17568 KB |
Output is correct |
56 |
Correct |
7 ms |
17500 KB |
Output is correct |
57 |
Correct |
9 ms |
17240 KB |
Output is correct |
58 |
Correct |
11 ms |
17316 KB |
Output is correct |
59 |
Correct |
11 ms |
17244 KB |
Output is correct |
60 |
Correct |
10 ms |
17244 KB |
Output is correct |
61 |
Correct |
7 ms |
17500 KB |
Output is correct |
62 |
Correct |
7 ms |
17500 KB |
Output is correct |
63 |
Correct |
6 ms |
17508 KB |
Output is correct |
64 |
Correct |
10 ms |
17360 KB |
Output is correct |
65 |
Correct |
8 ms |
17244 KB |
Output is correct |
66 |
Correct |
11 ms |
17244 KB |
Output is correct |
67 |
Correct |
8 ms |
17244 KB |
Output is correct |
68 |
Correct |
6 ms |
17500 KB |
Output is correct |
69 |
Correct |
6 ms |
17500 KB |
Output is correct |
70 |
Correct |
7 ms |
17500 KB |
Output is correct |
71 |
Correct |
6 ms |
17496 KB |
Output is correct |
72 |
Correct |
9 ms |
17240 KB |
Output is correct |
73 |
Correct |
11 ms |
17244 KB |
Output is correct |
74 |
Correct |
6 ms |
17500 KB |
Output is correct |
75 |
Correct |
7 ms |
17308 KB |
Output is correct |
76 |
Correct |
7 ms |
17240 KB |
Output is correct |
77 |
Correct |
7 ms |
17244 KB |
Output is correct |
78 |
Correct |
6 ms |
17244 KB |
Output is correct |
79 |
Correct |
8 ms |
17244 KB |
Output is correct |
80 |
Correct |
7 ms |
17244 KB |
Output is correct |
81 |
Correct |
8 ms |
17240 KB |
Output is correct |
82 |
Correct |
8 ms |
17500 KB |
Output is correct |
83 |
Correct |
7 ms |
17240 KB |
Output is correct |
84 |
Correct |
6 ms |
17240 KB |
Output is correct |
85 |
Correct |
7 ms |
17240 KB |
Output is correct |
86 |
Correct |
7 ms |
17244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
16984 KB |
Output is correct |
3 |
Correct |
4 ms |
17040 KB |
Output is correct |
4 |
Correct |
5 ms |
16988 KB |
Output is correct |
5 |
Correct |
4 ms |
17056 KB |
Output is correct |
6 |
Correct |
6 ms |
16988 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
6 ms |
17244 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
5 ms |
16988 KB |
Output is correct |
11 |
Correct |
4 ms |
16988 KB |
Output is correct |
12 |
Correct |
5 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
17084 KB |
Output is correct |
14 |
Correct |
5 ms |
17112 KB |
Output is correct |
15 |
Correct |
5 ms |
16988 KB |
Output is correct |
16 |
Correct |
5 ms |
16988 KB |
Output is correct |
17 |
Correct |
5 ms |
16988 KB |
Output is correct |
18 |
Correct |
4 ms |
16988 KB |
Output is correct |
19 |
Correct |
4 ms |
16988 KB |
Output is correct |
20 |
Correct |
4 ms |
16988 KB |
Output is correct |
21 |
Correct |
4 ms |
16988 KB |
Output is correct |
22 |
Correct |
5 ms |
16984 KB |
Output is correct |
23 |
Correct |
5 ms |
16988 KB |
Output is correct |
24 |
Correct |
5 ms |
16984 KB |
Output is correct |
25 |
Correct |
5 ms |
16988 KB |
Output is correct |
26 |
Correct |
4 ms |
16988 KB |
Output is correct |
27 |
Correct |
4 ms |
17052 KB |
Output is correct |
28 |
Correct |
4 ms |
16984 KB |
Output is correct |
29 |
Correct |
4 ms |
16984 KB |
Output is correct |
30 |
Correct |
5 ms |
16984 KB |
Output is correct |
31 |
Correct |
6 ms |
16984 KB |
Output is correct |
32 |
Correct |
4 ms |
16988 KB |
Output is correct |
33 |
Correct |
4 ms |
16988 KB |
Output is correct |
34 |
Correct |
4 ms |
16988 KB |
Output is correct |
35 |
Correct |
5 ms |
16988 KB |
Output is correct |
36 |
Correct |
4 ms |
16984 KB |
Output is correct |
37 |
Correct |
4 ms |
16988 KB |
Output is correct |
38 |
Correct |
5 ms |
17088 KB |
Output is correct |
39 |
Correct |
5 ms |
16988 KB |
Output is correct |
40 |
Correct |
4 ms |
16988 KB |
Output is correct |
41 |
Correct |
5 ms |
16988 KB |
Output is correct |
42 |
Correct |
5 ms |
16988 KB |
Output is correct |
43 |
Correct |
4 ms |
17052 KB |
Output is correct |
44 |
Correct |
4 ms |
16988 KB |
Output is correct |
45 |
Correct |
5 ms |
16988 KB |
Output is correct |
46 |
Correct |
9 ms |
17492 KB |
Output is correct |
47 |
Correct |
8 ms |
17496 KB |
Output is correct |
48 |
Correct |
8 ms |
17384 KB |
Output is correct |
49 |
Correct |
8 ms |
17500 KB |
Output is correct |
50 |
Correct |
7 ms |
17496 KB |
Output is correct |
51 |
Correct |
7 ms |
17500 KB |
Output is correct |
52 |
Correct |
7 ms |
17500 KB |
Output is correct |
53 |
Correct |
8 ms |
17500 KB |
Output is correct |
54 |
Correct |
7 ms |
17500 KB |
Output is correct |
55 |
Correct |
8 ms |
17568 KB |
Output is correct |
56 |
Correct |
7 ms |
17500 KB |
Output is correct |
57 |
Correct |
9 ms |
17240 KB |
Output is correct |
58 |
Correct |
11 ms |
17316 KB |
Output is correct |
59 |
Correct |
11 ms |
17244 KB |
Output is correct |
60 |
Correct |
10 ms |
17244 KB |
Output is correct |
61 |
Correct |
7 ms |
17500 KB |
Output is correct |
62 |
Correct |
7 ms |
17500 KB |
Output is correct |
63 |
Correct |
6 ms |
17508 KB |
Output is correct |
64 |
Correct |
10 ms |
17360 KB |
Output is correct |
65 |
Correct |
8 ms |
17244 KB |
Output is correct |
66 |
Correct |
11 ms |
17244 KB |
Output is correct |
67 |
Correct |
8 ms |
17244 KB |
Output is correct |
68 |
Correct |
6 ms |
17500 KB |
Output is correct |
69 |
Correct |
6 ms |
17500 KB |
Output is correct |
70 |
Correct |
7 ms |
17500 KB |
Output is correct |
71 |
Correct |
6 ms |
17496 KB |
Output is correct |
72 |
Correct |
9 ms |
17240 KB |
Output is correct |
73 |
Correct |
11 ms |
17244 KB |
Output is correct |
74 |
Correct |
6 ms |
17500 KB |
Output is correct |
75 |
Correct |
7 ms |
17308 KB |
Output is correct |
76 |
Correct |
7 ms |
17240 KB |
Output is correct |
77 |
Correct |
7 ms |
17244 KB |
Output is correct |
78 |
Correct |
6 ms |
17244 KB |
Output is correct |
79 |
Correct |
8 ms |
17244 KB |
Output is correct |
80 |
Correct |
7 ms |
17244 KB |
Output is correct |
81 |
Correct |
8 ms |
17240 KB |
Output is correct |
82 |
Correct |
8 ms |
17500 KB |
Output is correct |
83 |
Correct |
7 ms |
17240 KB |
Output is correct |
84 |
Correct |
6 ms |
17240 KB |
Output is correct |
85 |
Correct |
7 ms |
17240 KB |
Output is correct |
86 |
Correct |
7 ms |
17244 KB |
Output is correct |
87 |
Correct |
15 ms |
17756 KB |
Output is correct |
88 |
Correct |
42 ms |
19408 KB |
Output is correct |
89 |
Correct |
148 ms |
25552 KB |
Output is correct |
90 |
Correct |
167 ms |
25548 KB |
Output is correct |
91 |
Correct |
144 ms |
25540 KB |
Output is correct |
92 |
Correct |
84 ms |
31440 KB |
Output is correct |
93 |
Correct |
79 ms |
31244 KB |
Output is correct |
94 |
Correct |
94 ms |
31292 KB |
Output is correct |
95 |
Correct |
93 ms |
31944 KB |
Output is correct |
96 |
Correct |
107 ms |
32136 KB |
Output is correct |
97 |
Correct |
88 ms |
32108 KB |
Output is correct |
98 |
Correct |
90 ms |
32148 KB |
Output is correct |
99 |
Correct |
85 ms |
29292 KB |
Output is correct |
100 |
Correct |
187 ms |
25148 KB |
Output is correct |
101 |
Correct |
236 ms |
25536 KB |
Output is correct |
102 |
Correct |
233 ms |
25388 KB |
Output is correct |
103 |
Correct |
213 ms |
25360 KB |
Output is correct |
104 |
Correct |
103 ms |
29380 KB |
Output is correct |
105 |
Correct |
93 ms |
29212 KB |
Output is correct |
106 |
Correct |
90 ms |
29072 KB |
Output is correct |
107 |
Correct |
141 ms |
24868 KB |
Output is correct |
108 |
Correct |
132 ms |
25036 KB |
Output is correct |
109 |
Correct |
140 ms |
25236 KB |
Output is correct |
110 |
Correct |
68 ms |
30988 KB |
Output is correct |
111 |
Correct |
93 ms |
31984 KB |
Output is correct |
112 |
Correct |
76 ms |
31492 KB |
Output is correct |
113 |
Correct |
75 ms |
28500 KB |
Output is correct |
114 |
Correct |
185 ms |
25036 KB |
Output is correct |
115 |
Correct |
208 ms |
24732 KB |
Output is correct |
116 |
Correct |
85 ms |
28452 KB |
Output is correct |
117 |
Correct |
96 ms |
26832 KB |
Output is correct |
118 |
Correct |
105 ms |
26324 KB |
Output is correct |
119 |
Correct |
108 ms |
25884 KB |
Output is correct |
120 |
Correct |
74 ms |
26572 KB |
Output is correct |
121 |
Correct |
78 ms |
25968 KB |
Output is correct |
122 |
Correct |
76 ms |
25548 KB |
Output is correct |
123 |
Correct |
100 ms |
26908 KB |
Output is correct |
124 |
Correct |
95 ms |
26196 KB |
Output is correct |
125 |
Correct |
96 ms |
25876 KB |
Output is correct |
126 |
Correct |
85 ms |
26576 KB |
Output is correct |
127 |
Correct |
84 ms |
25916 KB |
Output is correct |
128 |
Correct |
85 ms |
25548 KB |
Output is correct |