#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
const int maxn=1e5+9;
int c[maxn];
vector<int>a[maxn];
int bit[maxn];
int n;
void add(int pos,int val){
for(;pos<=n;pos+=(pos-(pos&(pos-1))))bit[pos]+=val;
}
int get(int pos){
int sum=0;
for(;pos>=1;pos-=(pos-(pos&(pos-1))))sum+=bit[pos];
return sum;
}
pii b[maxn];
int head[maxn];
int sub[maxn];
int gr[maxn];
int p[maxn];
void dfs(int u,int par){
sub[u]=1;
p[u]=par;
for (auto &v:a[u]){
if (v==par)continue;
dfs(v,u);
sub[u]+=sub[v];
if (a[u][0]==par||sub[a[u][0]]<sub[v]){
swap(v,a[u][0]);
}
}
}
int tme=0,cnt=1;
int pos[maxn];
void hld(int u,int par,int h){
head[u]=h;
gr[u]=cnt;
pos[u]=++tme;
if (a[u][0]==par)return;
hld(a[u][0],u,h);
for (auto v:a[u]){
if (v==a[u][0]||v==par)continue;
cnt++;
hld(v,u,v);
}
}
struct line{
int l,r,val;
bool operator < (const line &p1)const {
return l<p1.l;
}
};
multiset<line>nw[maxn];
multiset<line>::iterator it;
long long addpath(int u,int val){
vector<pii>needtoerase;
int began=u;
long long ans=0;
while (u!=0){
line tmp;
tmp.l=pos[head[u]],tmp.r=pos[u],tmp.val=val;
if (!nw[gr[u]].empty()){
vector<pii>xoa;
while (!nw[gr[u]].empty()){
it=nw[gr[u]].begin();
auto t=(*it);
if (t.l>tmp.r)break;
if (t.r<=tmp.r){
xoa.pb({t.r-t.l+1,t.val});
nw[gr[u]].erase(it);
continue;
}
//tmp.l<=t.l<=tmp.r<=t.r
auto copyt=t;
copyt.l=tmp.r+1;
nw[gr[u]].erase(it);
nw[gr[u]].insert(copyt);
t.r=tmp.r;
xoa.pb({t.r-t.l+1,t.val});
}
reverse(all(xoa));
for (auto v:xoa){
needtoerase.pb(v);
long long bonus=1ll*v.fi*get(v.se-1);
add(v.se,v.fi);
ans+=bonus;
}
}
nw[gr[u]].insert(tmp);
u=p[head[u]];
}
for (auto v:needtoerase){
add(v.se,-v.fi);
//cout<<began<<" "<<v.fi<<" "<<v.se<<'\n';
}
return ans;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen(".OUT","w",stdout);
cin>>n;
vector<int>nen;
nen.pb(0);
map<int,int>nenso;
for1(i,1,n){
cin>>c[i];
nen.pb(c[i]);
}
sort(all(nen));
for1(i,1,sz(nen)-1){
if (nen[i]==nen[i-1])continue;
nenso[nen[i]]=nenso[nen[i-1]]+1;
}
for1(i,1,n){
c[i]=nenso[c[i]];
}
for1(i,1,n-1){
cin>>b[i].fi>>b[i].se;
a[b[i].fi].pb(b[i].se);
a[b[i].se].pb(b[i].fi);
}
dfs(1,0);
hld(1,0,1);
addpath(1,c[1]);
for1(i,1,n-1){
cout<<addpath(b[i].se,c[b[i].se])<<'\n';
}
}
Compilation message
construction.cpp: In function 'long long int addpath(int, int)':
construction.cpp:71:5: warning: unused variable 'began' [-Wunused-variable]
71 | int began=u;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7356 KB |
Output is correct |
3 |
Correct |
3 ms |
7384 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
5 ms |
7396 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7508 KB |
Output is correct |
9 |
Correct |
3 ms |
7508 KB |
Output is correct |
10 |
Correct |
4 ms |
7388 KB |
Output is correct |
11 |
Correct |
5 ms |
7508 KB |
Output is correct |
12 |
Correct |
3 ms |
7524 KB |
Output is correct |
13 |
Correct |
4 ms |
7508 KB |
Output is correct |
14 |
Correct |
4 ms |
7508 KB |
Output is correct |
15 |
Correct |
6 ms |
7380 KB |
Output is correct |
16 |
Correct |
5 ms |
7380 KB |
Output is correct |
17 |
Correct |
6 ms |
7384 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7508 KB |
Output is correct |
21 |
Correct |
4 ms |
7392 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
4 ms |
7388 KB |
Output is correct |
24 |
Correct |
3 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
4 ms |
7380 KB |
Output is correct |
27 |
Correct |
4 ms |
7508 KB |
Output is correct |
28 |
Correct |
6 ms |
7384 KB |
Output is correct |
29 |
Correct |
5 ms |
7388 KB |
Output is correct |
30 |
Correct |
4 ms |
7468 KB |
Output is correct |
31 |
Correct |
4 ms |
7388 KB |
Output is correct |
32 |
Correct |
3 ms |
7392 KB |
Output is correct |
33 |
Correct |
4 ms |
7496 KB |
Output is correct |
34 |
Correct |
3 ms |
7388 KB |
Output is correct |
35 |
Correct |
4 ms |
7384 KB |
Output is correct |
36 |
Correct |
3 ms |
7380 KB |
Output is correct |
37 |
Correct |
4 ms |
7380 KB |
Output is correct |
38 |
Correct |
4 ms |
7380 KB |
Output is correct |
39 |
Correct |
3 ms |
7380 KB |
Output is correct |
40 |
Correct |
4 ms |
7416 KB |
Output is correct |
41 |
Correct |
4 ms |
7388 KB |
Output is correct |
42 |
Correct |
6 ms |
7388 KB |
Output is correct |
43 |
Correct |
4 ms |
7384 KB |
Output is correct |
44 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7356 KB |
Output is correct |
3 |
Correct |
3 ms |
7384 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
5 ms |
7396 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7508 KB |
Output is correct |
9 |
Correct |
3 ms |
7508 KB |
Output is correct |
10 |
Correct |
4 ms |
7388 KB |
Output is correct |
11 |
Correct |
5 ms |
7508 KB |
Output is correct |
12 |
Correct |
3 ms |
7524 KB |
Output is correct |
13 |
Correct |
4 ms |
7508 KB |
Output is correct |
14 |
Correct |
4 ms |
7508 KB |
Output is correct |
15 |
Correct |
6 ms |
7380 KB |
Output is correct |
16 |
Correct |
5 ms |
7380 KB |
Output is correct |
17 |
Correct |
6 ms |
7384 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7508 KB |
Output is correct |
21 |
Correct |
4 ms |
7392 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
4 ms |
7388 KB |
Output is correct |
24 |
Correct |
3 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
4 ms |
7380 KB |
Output is correct |
27 |
Correct |
4 ms |
7508 KB |
Output is correct |
28 |
Correct |
6 ms |
7384 KB |
Output is correct |
29 |
Correct |
5 ms |
7388 KB |
Output is correct |
30 |
Correct |
4 ms |
7468 KB |
Output is correct |
31 |
Correct |
4 ms |
7388 KB |
Output is correct |
32 |
Correct |
3 ms |
7392 KB |
Output is correct |
33 |
Correct |
4 ms |
7496 KB |
Output is correct |
34 |
Correct |
3 ms |
7388 KB |
Output is correct |
35 |
Correct |
4 ms |
7384 KB |
Output is correct |
36 |
Correct |
3 ms |
7380 KB |
Output is correct |
37 |
Correct |
4 ms |
7380 KB |
Output is correct |
38 |
Correct |
4 ms |
7380 KB |
Output is correct |
39 |
Correct |
3 ms |
7380 KB |
Output is correct |
40 |
Correct |
4 ms |
7416 KB |
Output is correct |
41 |
Correct |
4 ms |
7388 KB |
Output is correct |
42 |
Correct |
6 ms |
7388 KB |
Output is correct |
43 |
Correct |
4 ms |
7384 KB |
Output is correct |
44 |
Correct |
4 ms |
7380 KB |
Output is correct |
45 |
Correct |
6 ms |
7504 KB |
Output is correct |
46 |
Correct |
9 ms |
8020 KB |
Output is correct |
47 |
Correct |
13 ms |
8020 KB |
Output is correct |
48 |
Correct |
8 ms |
8020 KB |
Output is correct |
49 |
Correct |
6 ms |
8148 KB |
Output is correct |
50 |
Correct |
6 ms |
8200 KB |
Output is correct |
51 |
Correct |
6 ms |
8148 KB |
Output is correct |
52 |
Correct |
6 ms |
8276 KB |
Output is correct |
53 |
Correct |
7 ms |
8372 KB |
Output is correct |
54 |
Correct |
6 ms |
8404 KB |
Output is correct |
55 |
Correct |
6 ms |
8404 KB |
Output is correct |
56 |
Correct |
6 ms |
8148 KB |
Output is correct |
57 |
Correct |
10 ms |
8148 KB |
Output is correct |
58 |
Correct |
13 ms |
8116 KB |
Output is correct |
59 |
Correct |
15 ms |
8140 KB |
Output is correct |
60 |
Correct |
12 ms |
8164 KB |
Output is correct |
61 |
Correct |
7 ms |
8148 KB |
Output is correct |
62 |
Correct |
7 ms |
8148 KB |
Output is correct |
63 |
Correct |
7 ms |
8208 KB |
Output is correct |
64 |
Correct |
7 ms |
7892 KB |
Output is correct |
65 |
Correct |
7 ms |
7892 KB |
Output is correct |
66 |
Correct |
8 ms |
7892 KB |
Output is correct |
67 |
Correct |
10 ms |
7916 KB |
Output is correct |
68 |
Correct |
5 ms |
8020 KB |
Output is correct |
69 |
Correct |
7 ms |
8276 KB |
Output is correct |
70 |
Correct |
6 ms |
8160 KB |
Output is correct |
71 |
Correct |
7 ms |
8020 KB |
Output is correct |
72 |
Correct |
11 ms |
8064 KB |
Output is correct |
73 |
Correct |
11 ms |
7912 KB |
Output is correct |
74 |
Correct |
6 ms |
8020 KB |
Output is correct |
75 |
Correct |
8 ms |
8148 KB |
Output is correct |
76 |
Correct |
6 ms |
8020 KB |
Output is correct |
77 |
Correct |
7 ms |
8108 KB |
Output is correct |
78 |
Correct |
6 ms |
7892 KB |
Output is correct |
79 |
Correct |
6 ms |
7912 KB |
Output is correct |
80 |
Correct |
6 ms |
7904 KB |
Output is correct |
81 |
Correct |
7 ms |
8148 KB |
Output is correct |
82 |
Correct |
7 ms |
8108 KB |
Output is correct |
83 |
Correct |
7 ms |
8020 KB |
Output is correct |
84 |
Correct |
6 ms |
7912 KB |
Output is correct |
85 |
Correct |
6 ms |
7904 KB |
Output is correct |
86 |
Correct |
6 ms |
7892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7356 KB |
Output is correct |
3 |
Correct |
3 ms |
7384 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
5 ms |
7396 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7508 KB |
Output is correct |
9 |
Correct |
3 ms |
7508 KB |
Output is correct |
10 |
Correct |
4 ms |
7388 KB |
Output is correct |
11 |
Correct |
5 ms |
7508 KB |
Output is correct |
12 |
Correct |
3 ms |
7524 KB |
Output is correct |
13 |
Correct |
4 ms |
7508 KB |
Output is correct |
14 |
Correct |
4 ms |
7508 KB |
Output is correct |
15 |
Correct |
6 ms |
7380 KB |
Output is correct |
16 |
Correct |
5 ms |
7380 KB |
Output is correct |
17 |
Correct |
6 ms |
7384 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7508 KB |
Output is correct |
21 |
Correct |
4 ms |
7392 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
4 ms |
7388 KB |
Output is correct |
24 |
Correct |
3 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
4 ms |
7380 KB |
Output is correct |
27 |
Correct |
4 ms |
7508 KB |
Output is correct |
28 |
Correct |
6 ms |
7384 KB |
Output is correct |
29 |
Correct |
5 ms |
7388 KB |
Output is correct |
30 |
Correct |
4 ms |
7468 KB |
Output is correct |
31 |
Correct |
4 ms |
7388 KB |
Output is correct |
32 |
Correct |
3 ms |
7392 KB |
Output is correct |
33 |
Correct |
4 ms |
7496 KB |
Output is correct |
34 |
Correct |
3 ms |
7388 KB |
Output is correct |
35 |
Correct |
4 ms |
7384 KB |
Output is correct |
36 |
Correct |
3 ms |
7380 KB |
Output is correct |
37 |
Correct |
4 ms |
7380 KB |
Output is correct |
38 |
Correct |
4 ms |
7380 KB |
Output is correct |
39 |
Correct |
3 ms |
7380 KB |
Output is correct |
40 |
Correct |
4 ms |
7416 KB |
Output is correct |
41 |
Correct |
4 ms |
7388 KB |
Output is correct |
42 |
Correct |
6 ms |
7388 KB |
Output is correct |
43 |
Correct |
4 ms |
7384 KB |
Output is correct |
44 |
Correct |
4 ms |
7380 KB |
Output is correct |
45 |
Correct |
6 ms |
7504 KB |
Output is correct |
46 |
Correct |
9 ms |
8020 KB |
Output is correct |
47 |
Correct |
13 ms |
8020 KB |
Output is correct |
48 |
Correct |
8 ms |
8020 KB |
Output is correct |
49 |
Correct |
6 ms |
8148 KB |
Output is correct |
50 |
Correct |
6 ms |
8200 KB |
Output is correct |
51 |
Correct |
6 ms |
8148 KB |
Output is correct |
52 |
Correct |
6 ms |
8276 KB |
Output is correct |
53 |
Correct |
7 ms |
8372 KB |
Output is correct |
54 |
Correct |
6 ms |
8404 KB |
Output is correct |
55 |
Correct |
6 ms |
8404 KB |
Output is correct |
56 |
Correct |
6 ms |
8148 KB |
Output is correct |
57 |
Correct |
10 ms |
8148 KB |
Output is correct |
58 |
Correct |
13 ms |
8116 KB |
Output is correct |
59 |
Correct |
15 ms |
8140 KB |
Output is correct |
60 |
Correct |
12 ms |
8164 KB |
Output is correct |
61 |
Correct |
7 ms |
8148 KB |
Output is correct |
62 |
Correct |
7 ms |
8148 KB |
Output is correct |
63 |
Correct |
7 ms |
8208 KB |
Output is correct |
64 |
Correct |
7 ms |
7892 KB |
Output is correct |
65 |
Correct |
7 ms |
7892 KB |
Output is correct |
66 |
Correct |
8 ms |
7892 KB |
Output is correct |
67 |
Correct |
10 ms |
7916 KB |
Output is correct |
68 |
Correct |
5 ms |
8020 KB |
Output is correct |
69 |
Correct |
7 ms |
8276 KB |
Output is correct |
70 |
Correct |
6 ms |
8160 KB |
Output is correct |
71 |
Correct |
7 ms |
8020 KB |
Output is correct |
72 |
Correct |
11 ms |
8064 KB |
Output is correct |
73 |
Correct |
11 ms |
7912 KB |
Output is correct |
74 |
Correct |
6 ms |
8020 KB |
Output is correct |
75 |
Correct |
8 ms |
8148 KB |
Output is correct |
76 |
Correct |
6 ms |
8020 KB |
Output is correct |
77 |
Correct |
7 ms |
8108 KB |
Output is correct |
78 |
Correct |
6 ms |
7892 KB |
Output is correct |
79 |
Correct |
6 ms |
7912 KB |
Output is correct |
80 |
Correct |
6 ms |
7904 KB |
Output is correct |
81 |
Correct |
7 ms |
8148 KB |
Output is correct |
82 |
Correct |
7 ms |
8108 KB |
Output is correct |
83 |
Correct |
7 ms |
8020 KB |
Output is correct |
84 |
Correct |
6 ms |
7912 KB |
Output is correct |
85 |
Correct |
6 ms |
7904 KB |
Output is correct |
86 |
Correct |
6 ms |
7892 KB |
Output is correct |
87 |
Correct |
19 ms |
9188 KB |
Output is correct |
88 |
Correct |
54 ms |
12712 KB |
Output is correct |
89 |
Correct |
222 ms |
25448 KB |
Output is correct |
90 |
Correct |
223 ms |
25480 KB |
Output is correct |
91 |
Correct |
215 ms |
25556 KB |
Output is correct |
92 |
Correct |
107 ms |
29152 KB |
Output is correct |
93 |
Correct |
112 ms |
29164 KB |
Output is correct |
94 |
Correct |
106 ms |
29184 KB |
Output is correct |
95 |
Correct |
125 ms |
32360 KB |
Output is correct |
96 |
Correct |
131 ms |
32708 KB |
Output is correct |
97 |
Correct |
124 ms |
32728 KB |
Output is correct |
98 |
Correct |
128 ms |
32684 KB |
Output is correct |
99 |
Correct |
120 ms |
28596 KB |
Output is correct |
100 |
Correct |
281 ms |
25296 KB |
Output is correct |
101 |
Correct |
302 ms |
25516 KB |
Output is correct |
102 |
Correct |
329 ms |
25632 KB |
Output is correct |
103 |
Correct |
310 ms |
25580 KB |
Output is correct |
104 |
Correct |
126 ms |
28572 KB |
Output is correct |
105 |
Correct |
132 ms |
28620 KB |
Output is correct |
106 |
Correct |
121 ms |
28492 KB |
Output is correct |
107 |
Correct |
160 ms |
19692 KB |
Output is correct |
108 |
Correct |
167 ms |
19780 KB |
Output is correct |
109 |
Correct |
189 ms |
21468 KB |
Output is correct |
110 |
Correct |
63 ms |
23528 KB |
Output is correct |
111 |
Correct |
133 ms |
32276 KB |
Output is correct |
112 |
Correct |
84 ms |
27056 KB |
Output is correct |
113 |
Correct |
86 ms |
23016 KB |
Output is correct |
114 |
Correct |
292 ms |
25216 KB |
Output is correct |
115 |
Correct |
254 ms |
19964 KB |
Output is correct |
116 |
Correct |
101 ms |
22900 KB |
Output is correct |
117 |
Correct |
114 ms |
26060 KB |
Output is correct |
118 |
Correct |
120 ms |
25332 KB |
Output is correct |
119 |
Correct |
126 ms |
25036 KB |
Output is correct |
120 |
Correct |
77 ms |
20824 KB |
Output is correct |
121 |
Correct |
76 ms |
20240 KB |
Output is correct |
122 |
Correct |
76 ms |
19936 KB |
Output is correct |
123 |
Correct |
133 ms |
26060 KB |
Output is correct |
124 |
Correct |
128 ms |
25416 KB |
Output is correct |
125 |
Correct |
135 ms |
25132 KB |
Output is correct |
126 |
Correct |
87 ms |
20940 KB |
Output is correct |
127 |
Correct |
92 ms |
20352 KB |
Output is correct |
128 |
Correct |
91 ms |
19920 KB |
Output is correct |