#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+10;
vector<int>adj[maxn];
int all[maxn],part[maxn],parh[maxn],high[maxn],sz[maxn],n,timea=0;
pair<int,int>stf[maxn];
vector<pair<int,int>>val[maxn];
pair<int,int>alle[maxn];
bool cmp(int a,int b){
return sz[a]>sz[b];
}
struct fen{
int fn[maxn];
vector<int>allv;
void clear(){
for(auto x:allv){
fn[x]=0;
}
allv.clear();
}
void upd(int i,int w){
//cout<<"wtf inf: "<<i<<" "<<w<<endl;
i++;
while(i<maxn){
allv.push_back(i);
fn[i]+=w;
i+=((-i)&i);
}
}
int pors(int i){
//cout<<"tof pi: "<<i<<" ";
i++;
int ret=0;
while(i>0){
ret+=fn[i];
i-=((-i)&i);
}
//cout<<ret<<endl;
return ret;
}
}fn;
void pre(int u=1,int par=1){
if(u!=1){
sort(adj[u].begin(),adj[u].end());
adj[u].erase(lower_bound(adj[u].begin(),adj[u].end(),par));
}
part[u]=par;
sz[u]=1;
for(auto x:adj[u]){
high[x]=high[u]+1;
pre(x,u);
sz[u]+=sz[x];
}
sort(adj[u].begin(),adj[u].end(),cmp);
}
void dfs(int u=1,int par=1){
timea++;
stf[u].first=timea;
parh[u]=par;
if(adj[u].size()>0){
dfs(adj[u][0],par);
}
for(int i=1;i<(int)adj[u].size();i++){
dfs(adj[u][i],adj[u][i]);
}
stf[u].second=timea;
}
void upd(int u,int w){
//cout<<u<<" "<<w<<" "<<parh[u]<<endl;
int ted=high[u]-high[parh[u]]+1;
u=parh[u];
int now=0;
while((int)val[u].size()>0){
if(val[u].back().first+now<=ted){
now+=val[u].back().first;
val[u].pop_back();
continue;
}
val[u].back().first-=ted-now;
break;
}
val[u].push_back(make_pair(ted,w));
if(u==1){
return ;
}
return upd(part[u],w);
}
int get(int u){
// //cout<<"inget: "<<u<<endl;
int ted=high[u]-high[parh[u]]+1;
int now=0;
int ph=parh[u];
if((int)val[ph].size()==0){
if(ph==1){
return 0;
}
return get(part[parh[u]]);
}
u=parh[u];
vector<pair<int,int>>tof;
for(int i=(int)val[u].size()-1;i>=0;i--){
if(val[u][i].first+now<=ted){
tof.push_back(val[u][i]);
now+=val[u][i].first;
}
else{
tof.push_back(make_pair(ted-now,val[u][i].second));
break;
}
}
// //cout<<"injy: "<<u<<endl;
reverse(tof.begin(),tof.end());
long long ret=0;
for(auto x:tof){
ret+=x.first*fn.pors(x.second-1);
fn.upd(x.second,x.first);
}
// //cout<<"oonjy: "<<u<<endl;
if(u==1){
return ret;
}
ret+=get(part[parh[u]]);
return ret;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
//cout.tie(0);
cin>>n;
vector<int>allind;
for(int i=1;i<=n;i++){
cin>>all[i];
allind.push_back(all[i]);
}
sort(allind.begin(),allind.end());
allind.resize(unique(allind.begin(),allind.end())-allind.begin());
for(int i=1;i<=n;i++){
all[i]=lower_bound(allind.begin(),allind.end(),all[i])-allind.begin();
}
for(int i=0;i<n-1;i++){
cin>>alle[i].first>>alle[i].second;
adj[alle[i].first].push_back(alle[i].second);
adj[alle[i].second].push_back(alle[i].first);
}
pre();
dfs();
upd(1,all[1]);
////cout<<"sa;am"<<endl;
for(int i=0;i<n-1;i++){
if(high[alle[i].first]>high[alle[i].second]){
swap(alle[i].first,alle[i].second);
}
cout<<get(alle[i].first)<<endl;
// //cout<<"wtf"<<endl;
upd(alle[i].second,all[alle[i].second]);
// //cout<<"radshod"<<endl;
fn.clear();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8804 KB |
Output is correct |
2 |
Correct |
3 ms |
8792 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
4 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
4 ms |
8796 KB |
Output is correct |
8 |
Correct |
3 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8908 KB |
Output is correct |
10 |
Correct |
3 ms |
8908 KB |
Output is correct |
11 |
Correct |
3 ms |
8792 KB |
Output is correct |
12 |
Correct |
4 ms |
8796 KB |
Output is correct |
13 |
Correct |
3 ms |
8796 KB |
Output is correct |
14 |
Correct |
3 ms |
8796 KB |
Output is correct |
15 |
Correct |
3 ms |
8796 KB |
Output is correct |
16 |
Correct |
3 ms |
8796 KB |
Output is correct |
17 |
Correct |
4 ms |
8796 KB |
Output is correct |
18 |
Correct |
4 ms |
8796 KB |
Output is correct |
19 |
Correct |
3 ms |
8796 KB |
Output is correct |
20 |
Correct |
3 ms |
8796 KB |
Output is correct |
21 |
Correct |
3 ms |
8912 KB |
Output is correct |
22 |
Correct |
3 ms |
8796 KB |
Output is correct |
23 |
Correct |
3 ms |
8804 KB |
Output is correct |
24 |
Correct |
4 ms |
8796 KB |
Output is correct |
25 |
Correct |
4 ms |
8796 KB |
Output is correct |
26 |
Correct |
3 ms |
8796 KB |
Output is correct |
27 |
Correct |
3 ms |
8796 KB |
Output is correct |
28 |
Correct |
4 ms |
8796 KB |
Output is correct |
29 |
Correct |
4 ms |
8796 KB |
Output is correct |
30 |
Correct |
4 ms |
8796 KB |
Output is correct |
31 |
Correct |
4 ms |
8908 KB |
Output is correct |
32 |
Correct |
4 ms |
8792 KB |
Output is correct |
33 |
Correct |
4 ms |
8792 KB |
Output is correct |
34 |
Correct |
3 ms |
8796 KB |
Output is correct |
35 |
Correct |
3 ms |
8908 KB |
Output is correct |
36 |
Correct |
3 ms |
8796 KB |
Output is correct |
37 |
Correct |
5 ms |
9052 KB |
Output is correct |
38 |
Correct |
3 ms |
8796 KB |
Output is correct |
39 |
Correct |
4 ms |
8796 KB |
Output is correct |
40 |
Correct |
3 ms |
8904 KB |
Output is correct |
41 |
Correct |
4 ms |
8796 KB |
Output is correct |
42 |
Correct |
3 ms |
8796 KB |
Output is correct |
43 |
Correct |
3 ms |
8796 KB |
Output is correct |
44 |
Correct |
4 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8804 KB |
Output is correct |
2 |
Correct |
3 ms |
8792 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
4 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
4 ms |
8796 KB |
Output is correct |
8 |
Correct |
3 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8908 KB |
Output is correct |
10 |
Correct |
3 ms |
8908 KB |
Output is correct |
11 |
Correct |
3 ms |
8792 KB |
Output is correct |
12 |
Correct |
4 ms |
8796 KB |
Output is correct |
13 |
Correct |
3 ms |
8796 KB |
Output is correct |
14 |
Correct |
3 ms |
8796 KB |
Output is correct |
15 |
Correct |
3 ms |
8796 KB |
Output is correct |
16 |
Correct |
3 ms |
8796 KB |
Output is correct |
17 |
Correct |
4 ms |
8796 KB |
Output is correct |
18 |
Correct |
4 ms |
8796 KB |
Output is correct |
19 |
Correct |
3 ms |
8796 KB |
Output is correct |
20 |
Correct |
3 ms |
8796 KB |
Output is correct |
21 |
Correct |
3 ms |
8912 KB |
Output is correct |
22 |
Correct |
3 ms |
8796 KB |
Output is correct |
23 |
Correct |
3 ms |
8804 KB |
Output is correct |
24 |
Correct |
4 ms |
8796 KB |
Output is correct |
25 |
Correct |
4 ms |
8796 KB |
Output is correct |
26 |
Correct |
3 ms |
8796 KB |
Output is correct |
27 |
Correct |
3 ms |
8796 KB |
Output is correct |
28 |
Correct |
4 ms |
8796 KB |
Output is correct |
29 |
Correct |
4 ms |
8796 KB |
Output is correct |
30 |
Correct |
4 ms |
8796 KB |
Output is correct |
31 |
Correct |
4 ms |
8908 KB |
Output is correct |
32 |
Correct |
4 ms |
8792 KB |
Output is correct |
33 |
Correct |
4 ms |
8792 KB |
Output is correct |
34 |
Correct |
3 ms |
8796 KB |
Output is correct |
35 |
Correct |
3 ms |
8908 KB |
Output is correct |
36 |
Correct |
3 ms |
8796 KB |
Output is correct |
37 |
Correct |
5 ms |
9052 KB |
Output is correct |
38 |
Correct |
3 ms |
8796 KB |
Output is correct |
39 |
Correct |
4 ms |
8796 KB |
Output is correct |
40 |
Correct |
3 ms |
8904 KB |
Output is correct |
41 |
Correct |
4 ms |
8796 KB |
Output is correct |
42 |
Correct |
3 ms |
8796 KB |
Output is correct |
43 |
Correct |
3 ms |
8796 KB |
Output is correct |
44 |
Correct |
4 ms |
8796 KB |
Output is correct |
45 |
Correct |
5 ms |
8796 KB |
Output is correct |
46 |
Correct |
11 ms |
9184 KB |
Output is correct |
47 |
Correct |
11 ms |
9052 KB |
Output is correct |
48 |
Correct |
13 ms |
9052 KB |
Output is correct |
49 |
Correct |
9 ms |
9308 KB |
Output is correct |
50 |
Correct |
9 ms |
9308 KB |
Output is correct |
51 |
Correct |
11 ms |
9392 KB |
Output is correct |
52 |
Correct |
9 ms |
9564 KB |
Output is correct |
53 |
Correct |
9 ms |
9564 KB |
Output is correct |
54 |
Correct |
9 ms |
9616 KB |
Output is correct |
55 |
Correct |
11 ms |
9560 KB |
Output is correct |
56 |
Correct |
9 ms |
9308 KB |
Output is correct |
57 |
Correct |
12 ms |
9052 KB |
Output is correct |
58 |
Correct |
13 ms |
9052 KB |
Output is correct |
59 |
Correct |
13 ms |
9052 KB |
Output is correct |
60 |
Correct |
13 ms |
9228 KB |
Output is correct |
61 |
Correct |
13 ms |
9308 KB |
Output is correct |
62 |
Correct |
10 ms |
9308 KB |
Output is correct |
63 |
Correct |
10 ms |
9308 KB |
Output is correct |
64 |
Correct |
11 ms |
9128 KB |
Output is correct |
65 |
Correct |
11 ms |
9052 KB |
Output is correct |
66 |
Correct |
11 ms |
9048 KB |
Output is correct |
67 |
Correct |
11 ms |
9052 KB |
Output is correct |
68 |
Correct |
9 ms |
9304 KB |
Output is correct |
69 |
Correct |
10 ms |
9564 KB |
Output is correct |
70 |
Correct |
9 ms |
9580 KB |
Output is correct |
71 |
Correct |
10 ms |
9308 KB |
Output is correct |
72 |
Correct |
13 ms |
9052 KB |
Output is correct |
73 |
Correct |
13 ms |
9052 KB |
Output is correct |
74 |
Correct |
10 ms |
9308 KB |
Output is correct |
75 |
Correct |
10 ms |
9052 KB |
Output is correct |
76 |
Correct |
9 ms |
9052 KB |
Output is correct |
77 |
Correct |
9 ms |
9048 KB |
Output is correct |
78 |
Correct |
9 ms |
9052 KB |
Output is correct |
79 |
Correct |
10 ms |
9240 KB |
Output is correct |
80 |
Correct |
9 ms |
9184 KB |
Output is correct |
81 |
Correct |
12 ms |
9252 KB |
Output is correct |
82 |
Correct |
10 ms |
9052 KB |
Output is correct |
83 |
Correct |
10 ms |
9048 KB |
Output is correct |
84 |
Correct |
9 ms |
9052 KB |
Output is correct |
85 |
Correct |
10 ms |
9052 KB |
Output is correct |
86 |
Correct |
10 ms |
9172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8804 KB |
Output is correct |
2 |
Correct |
3 ms |
8792 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
4 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
4 ms |
8796 KB |
Output is correct |
8 |
Correct |
3 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8908 KB |
Output is correct |
10 |
Correct |
3 ms |
8908 KB |
Output is correct |
11 |
Correct |
3 ms |
8792 KB |
Output is correct |
12 |
Correct |
4 ms |
8796 KB |
Output is correct |
13 |
Correct |
3 ms |
8796 KB |
Output is correct |
14 |
Correct |
3 ms |
8796 KB |
Output is correct |
15 |
Correct |
3 ms |
8796 KB |
Output is correct |
16 |
Correct |
3 ms |
8796 KB |
Output is correct |
17 |
Correct |
4 ms |
8796 KB |
Output is correct |
18 |
Correct |
4 ms |
8796 KB |
Output is correct |
19 |
Correct |
3 ms |
8796 KB |
Output is correct |
20 |
Correct |
3 ms |
8796 KB |
Output is correct |
21 |
Correct |
3 ms |
8912 KB |
Output is correct |
22 |
Correct |
3 ms |
8796 KB |
Output is correct |
23 |
Correct |
3 ms |
8804 KB |
Output is correct |
24 |
Correct |
4 ms |
8796 KB |
Output is correct |
25 |
Correct |
4 ms |
8796 KB |
Output is correct |
26 |
Correct |
3 ms |
8796 KB |
Output is correct |
27 |
Correct |
3 ms |
8796 KB |
Output is correct |
28 |
Correct |
4 ms |
8796 KB |
Output is correct |
29 |
Correct |
4 ms |
8796 KB |
Output is correct |
30 |
Correct |
4 ms |
8796 KB |
Output is correct |
31 |
Correct |
4 ms |
8908 KB |
Output is correct |
32 |
Correct |
4 ms |
8792 KB |
Output is correct |
33 |
Correct |
4 ms |
8792 KB |
Output is correct |
34 |
Correct |
3 ms |
8796 KB |
Output is correct |
35 |
Correct |
3 ms |
8908 KB |
Output is correct |
36 |
Correct |
3 ms |
8796 KB |
Output is correct |
37 |
Correct |
5 ms |
9052 KB |
Output is correct |
38 |
Correct |
3 ms |
8796 KB |
Output is correct |
39 |
Correct |
4 ms |
8796 KB |
Output is correct |
40 |
Correct |
3 ms |
8904 KB |
Output is correct |
41 |
Correct |
4 ms |
8796 KB |
Output is correct |
42 |
Correct |
3 ms |
8796 KB |
Output is correct |
43 |
Correct |
3 ms |
8796 KB |
Output is correct |
44 |
Correct |
4 ms |
8796 KB |
Output is correct |
45 |
Correct |
5 ms |
8796 KB |
Output is correct |
46 |
Correct |
11 ms |
9184 KB |
Output is correct |
47 |
Correct |
11 ms |
9052 KB |
Output is correct |
48 |
Correct |
13 ms |
9052 KB |
Output is correct |
49 |
Correct |
9 ms |
9308 KB |
Output is correct |
50 |
Correct |
9 ms |
9308 KB |
Output is correct |
51 |
Correct |
11 ms |
9392 KB |
Output is correct |
52 |
Correct |
9 ms |
9564 KB |
Output is correct |
53 |
Correct |
9 ms |
9564 KB |
Output is correct |
54 |
Correct |
9 ms |
9616 KB |
Output is correct |
55 |
Correct |
11 ms |
9560 KB |
Output is correct |
56 |
Correct |
9 ms |
9308 KB |
Output is correct |
57 |
Correct |
12 ms |
9052 KB |
Output is correct |
58 |
Correct |
13 ms |
9052 KB |
Output is correct |
59 |
Correct |
13 ms |
9052 KB |
Output is correct |
60 |
Correct |
13 ms |
9228 KB |
Output is correct |
61 |
Correct |
13 ms |
9308 KB |
Output is correct |
62 |
Correct |
10 ms |
9308 KB |
Output is correct |
63 |
Correct |
10 ms |
9308 KB |
Output is correct |
64 |
Correct |
11 ms |
9128 KB |
Output is correct |
65 |
Correct |
11 ms |
9052 KB |
Output is correct |
66 |
Correct |
11 ms |
9048 KB |
Output is correct |
67 |
Correct |
11 ms |
9052 KB |
Output is correct |
68 |
Correct |
9 ms |
9304 KB |
Output is correct |
69 |
Correct |
10 ms |
9564 KB |
Output is correct |
70 |
Correct |
9 ms |
9580 KB |
Output is correct |
71 |
Correct |
10 ms |
9308 KB |
Output is correct |
72 |
Correct |
13 ms |
9052 KB |
Output is correct |
73 |
Correct |
13 ms |
9052 KB |
Output is correct |
74 |
Correct |
10 ms |
9308 KB |
Output is correct |
75 |
Correct |
10 ms |
9052 KB |
Output is correct |
76 |
Correct |
9 ms |
9052 KB |
Output is correct |
77 |
Correct |
9 ms |
9048 KB |
Output is correct |
78 |
Correct |
9 ms |
9052 KB |
Output is correct |
79 |
Correct |
10 ms |
9240 KB |
Output is correct |
80 |
Correct |
9 ms |
9184 KB |
Output is correct |
81 |
Correct |
12 ms |
9252 KB |
Output is correct |
82 |
Correct |
10 ms |
9052 KB |
Output is correct |
83 |
Correct |
10 ms |
9048 KB |
Output is correct |
84 |
Correct |
9 ms |
9052 KB |
Output is correct |
85 |
Correct |
10 ms |
9052 KB |
Output is correct |
86 |
Correct |
10 ms |
9172 KB |
Output is correct |
87 |
Correct |
25 ms |
9564 KB |
Output is correct |
88 |
Correct |
77 ms |
11020 KB |
Output is correct |
89 |
Correct |
278 ms |
16084 KB |
Output is correct |
90 |
Correct |
277 ms |
15824 KB |
Output is correct |
91 |
Correct |
284 ms |
16232 KB |
Output is correct |
92 |
Correct |
180 ms |
23252 KB |
Output is correct |
93 |
Correct |
186 ms |
23368 KB |
Output is correct |
94 |
Correct |
180 ms |
23252 KB |
Output is correct |
95 |
Correct |
208 ms |
22912 KB |
Output is correct |
96 |
Correct |
194 ms |
23192 KB |
Output is correct |
97 |
Correct |
197 ms |
23432 KB |
Output is correct |
98 |
Correct |
213 ms |
23252 KB |
Output is correct |
99 |
Correct |
192 ms |
20336 KB |
Output is correct |
100 |
Correct |
328 ms |
15312 KB |
Output is correct |
101 |
Correct |
335 ms |
15308 KB |
Output is correct |
102 |
Correct |
346 ms |
15452 KB |
Output is correct |
103 |
Correct |
360 ms |
15492 KB |
Output is correct |
104 |
Correct |
204 ms |
20440 KB |
Output is correct |
105 |
Correct |
195 ms |
20300 KB |
Output is correct |
106 |
Correct |
196 ms |
20356 KB |
Output is correct |
107 |
Correct |
264 ms |
15324 KB |
Output is correct |
108 |
Correct |
260 ms |
15316 KB |
Output is correct |
109 |
Correct |
276 ms |
16068 KB |
Output is correct |
110 |
Correct |
169 ms |
22996 KB |
Output is correct |
111 |
Correct |
201 ms |
23036 KB |
Output is correct |
112 |
Correct |
196 ms |
25040 KB |
Output is correct |
113 |
Correct |
181 ms |
20060 KB |
Output is correct |
114 |
Correct |
315 ms |
15312 KB |
Output is correct |
115 |
Correct |
335 ms |
15060 KB |
Output is correct |
116 |
Correct |
188 ms |
19924 KB |
Output is correct |
117 |
Correct |
194 ms |
18008 KB |
Output is correct |
118 |
Correct |
197 ms |
17256 KB |
Output is correct |
119 |
Correct |
191 ms |
16844 KB |
Output is correct |
120 |
Correct |
191 ms |
17576 KB |
Output is correct |
121 |
Correct |
184 ms |
16848 KB |
Output is correct |
122 |
Correct |
191 ms |
16456 KB |
Output is correct |
123 |
Correct |
207 ms |
18052 KB |
Output is correct |
124 |
Correct |
206 ms |
17176 KB |
Output is correct |
125 |
Correct |
211 ms |
16732 KB |
Output is correct |
126 |
Correct |
210 ms |
17708 KB |
Output is correct |
127 |
Correct |
195 ms |
16852 KB |
Output is correct |
128 |
Correct |
206 ms |
16308 KB |
Output is correct |