/* in the name of allah */
#include<bits/stdc++.h>
using namespace std;
//#define endl '\n'
#define pb push_back
#define F first
#define S second
#define mk make_pair
#define lc (2*id)
#define rc (2*id+1)
#define md ((s+e)/2)
#define ln (e-s+1)
typedef long long ll;
const int N=1e5+7,lg=20;
int n,c[N],a[N],b[N],h[N],p[N],fen[N],sp[N][lg],x;
int seg[4*N],st[N],fn[N],T;
pair<int,int>com[N];
ll ans[N];
vector<int>vec[N];
vector<pair<int,int>>vc;
void upd(int i,int x){
for(;i<N;i+=i&(-i)){
fen[i]+=x;
}
}
int get(int i){
int ans=0;
for(;i;i-=i&(-i)){
ans+=fen[i];
}
return ans;
}
void upds(int k,int x,int id=1,int s=1,int e=n){
if(ln==1){
seg[id]=x;
return;
}
if(k<=md) upds(k,x,lc,s,md);
else upds(k,x,rc,md+1,e);
seg[id]=max(seg[lc],seg[rc]);
}
int gets(int l,int r,int id=1,int s=1,int e=n){
if(l>e || s>r){
return 0;
}
if(s>=l && e<=r){
return seg[id];
}
return max(gets(l,r,lc,s,md),gets(l,r,rc,md+1,e));
}
int lca(int u,int v){
for(int i=lg-1;i>=0;i--){
if(h[sp[u][i]]>=h[v]){
u=sp[u][i];
}
if(h[sp[v][i]]>=h[u]){
v=sp[v][i];
}
}
for(int i=lg-1;i>=0;i--){
if(sp[u][i]!=sp[v][i]){
u=sp[u][i];
v=sp[v][i];
}
}
return u;
}
void dfs(int v=1){
st[v]=++T;
for(auto u:vec[v]){
dfs(u);
}
fn[v]=T;
}
int main(){
ios:: sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>c[i];
com[i]=mk(c[i],i);
}
int k=1;
sort(com,com+n+1);
for(int i=1;i<=n;i++){
c[com[i].S]=k;
if(com[i].F!=com[i+1].F)k++;
}
for(int i=0;i<n-1;i++){
cin>>a[i]>>b[i];
p[b[i]]=a[i];
h[b[i]]=h[a[i]]+1;
sp[b[i]][0]=a[i];
vec[a[i]].pb(b[i]);
}
dfs();
for(int j=1;j<lg;j++){
for(int i=1;i<=n;i++){
sp[i][j]=sp[sp[i][j-1]][j-1];
}
}
upds(1,1);
b[-1]=1;
for(int i=0;i<n-1;i++){
int u=1;
vc.clear();
while(u!=b[i]){
int v;
int x=b[gets(st[u],fn[u])-2];
//cout<<u<<endl;
if(u==a[i])v=b[i];
else v=lca(a[i],x);
ll t=h[v]-h[u];
ans[i]+=t*(get(N-1)-get(c[x]));
upd(c[x],t);
vc.pb(mk(c[x],t));
u=v;
}
for(auto [p,q]:vc){
upd(p,-q);
}
upds(st[b[i]],i+2);
cout<<ans[i]<<endl;
}
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:112:6: warning: array subscript -1 is below array bounds of 'int [100007]' [-Warray-bounds]
112 | b[-1]=1;
| ~~~~^
construction.cpp:18:17: note: while referencing 'b'
18 | int n,c[N],a[N],b[N],h[N],p[N],fen[N],sp[N][lg],x;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
3 ms |
2772 KB |
Output is correct |
6 |
Correct |
3 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2728 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2732 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
3 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2772 KB |
Output is correct |
32 |
Correct |
3 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
2 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2772 KB |
Output is correct |
38 |
Correct |
2 ms |
2772 KB |
Output is correct |
39 |
Correct |
2 ms |
2772 KB |
Output is correct |
40 |
Correct |
3 ms |
2772 KB |
Output is correct |
41 |
Correct |
3 ms |
2772 KB |
Output is correct |
42 |
Correct |
2 ms |
2772 KB |
Output is correct |
43 |
Correct |
2 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
3 ms |
2772 KB |
Output is correct |
6 |
Correct |
3 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2728 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2732 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
3 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2772 KB |
Output is correct |
32 |
Correct |
3 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
2 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2772 KB |
Output is correct |
38 |
Correct |
2 ms |
2772 KB |
Output is correct |
39 |
Correct |
2 ms |
2772 KB |
Output is correct |
40 |
Correct |
3 ms |
2772 KB |
Output is correct |
41 |
Correct |
3 ms |
2772 KB |
Output is correct |
42 |
Correct |
2 ms |
2772 KB |
Output is correct |
43 |
Correct |
2 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2772 KB |
Output is correct |
45 |
Correct |
4 ms |
2900 KB |
Output is correct |
46 |
Correct |
13 ms |
3352 KB |
Output is correct |
47 |
Correct |
13 ms |
3356 KB |
Output is correct |
48 |
Correct |
13 ms |
3356 KB |
Output is correct |
49 |
Correct |
9 ms |
3472 KB |
Output is correct |
50 |
Correct |
9 ms |
3412 KB |
Output is correct |
51 |
Correct |
9 ms |
3500 KB |
Output is correct |
52 |
Correct |
11 ms |
3416 KB |
Output is correct |
53 |
Correct |
10 ms |
3368 KB |
Output is correct |
54 |
Correct |
10 ms |
3412 KB |
Output is correct |
55 |
Correct |
10 ms |
3348 KB |
Output is correct |
56 |
Correct |
9 ms |
3284 KB |
Output is correct |
57 |
Correct |
21 ms |
3344 KB |
Output is correct |
58 |
Correct |
25 ms |
3360 KB |
Output is correct |
59 |
Correct |
24 ms |
3356 KB |
Output is correct |
60 |
Correct |
26 ms |
3344 KB |
Output is correct |
61 |
Correct |
10 ms |
3352 KB |
Output is correct |
62 |
Correct |
10 ms |
3388 KB |
Output is correct |
63 |
Correct |
10 ms |
3396 KB |
Output is correct |
64 |
Correct |
13 ms |
3336 KB |
Output is correct |
65 |
Correct |
13 ms |
3284 KB |
Output is correct |
66 |
Correct |
13 ms |
3336 KB |
Output is correct |
67 |
Correct |
15 ms |
3280 KB |
Output is correct |
68 |
Correct |
8 ms |
3412 KB |
Output is correct |
69 |
Correct |
12 ms |
3360 KB |
Output is correct |
70 |
Correct |
10 ms |
3376 KB |
Output is correct |
71 |
Correct |
9 ms |
3360 KB |
Output is correct |
72 |
Correct |
21 ms |
3336 KB |
Output is correct |
73 |
Correct |
25 ms |
3328 KB |
Output is correct |
74 |
Correct |
13 ms |
3388 KB |
Output is correct |
75 |
Correct |
9 ms |
3340 KB |
Output is correct |
76 |
Correct |
9 ms |
3284 KB |
Output is correct |
77 |
Correct |
9 ms |
3284 KB |
Output is correct |
78 |
Correct |
9 ms |
3344 KB |
Output is correct |
79 |
Correct |
9 ms |
3368 KB |
Output is correct |
80 |
Correct |
9 ms |
3284 KB |
Output is correct |
81 |
Correct |
10 ms |
3336 KB |
Output is correct |
82 |
Correct |
11 ms |
3344 KB |
Output is correct |
83 |
Correct |
10 ms |
3284 KB |
Output is correct |
84 |
Correct |
10 ms |
3284 KB |
Output is correct |
85 |
Correct |
10 ms |
3344 KB |
Output is correct |
86 |
Correct |
11 ms |
3308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
3 ms |
2772 KB |
Output is correct |
6 |
Correct |
3 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2728 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2732 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
3 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2772 KB |
Output is correct |
32 |
Correct |
3 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
2 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2772 KB |
Output is correct |
38 |
Correct |
2 ms |
2772 KB |
Output is correct |
39 |
Correct |
2 ms |
2772 KB |
Output is correct |
40 |
Correct |
3 ms |
2772 KB |
Output is correct |
41 |
Correct |
3 ms |
2772 KB |
Output is correct |
42 |
Correct |
2 ms |
2772 KB |
Output is correct |
43 |
Correct |
2 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2772 KB |
Output is correct |
45 |
Correct |
4 ms |
2900 KB |
Output is correct |
46 |
Correct |
13 ms |
3352 KB |
Output is correct |
47 |
Correct |
13 ms |
3356 KB |
Output is correct |
48 |
Correct |
13 ms |
3356 KB |
Output is correct |
49 |
Correct |
9 ms |
3472 KB |
Output is correct |
50 |
Correct |
9 ms |
3412 KB |
Output is correct |
51 |
Correct |
9 ms |
3500 KB |
Output is correct |
52 |
Correct |
11 ms |
3416 KB |
Output is correct |
53 |
Correct |
10 ms |
3368 KB |
Output is correct |
54 |
Correct |
10 ms |
3412 KB |
Output is correct |
55 |
Correct |
10 ms |
3348 KB |
Output is correct |
56 |
Correct |
9 ms |
3284 KB |
Output is correct |
57 |
Correct |
21 ms |
3344 KB |
Output is correct |
58 |
Correct |
25 ms |
3360 KB |
Output is correct |
59 |
Correct |
24 ms |
3356 KB |
Output is correct |
60 |
Correct |
26 ms |
3344 KB |
Output is correct |
61 |
Correct |
10 ms |
3352 KB |
Output is correct |
62 |
Correct |
10 ms |
3388 KB |
Output is correct |
63 |
Correct |
10 ms |
3396 KB |
Output is correct |
64 |
Correct |
13 ms |
3336 KB |
Output is correct |
65 |
Correct |
13 ms |
3284 KB |
Output is correct |
66 |
Correct |
13 ms |
3336 KB |
Output is correct |
67 |
Correct |
15 ms |
3280 KB |
Output is correct |
68 |
Correct |
8 ms |
3412 KB |
Output is correct |
69 |
Correct |
12 ms |
3360 KB |
Output is correct |
70 |
Correct |
10 ms |
3376 KB |
Output is correct |
71 |
Correct |
9 ms |
3360 KB |
Output is correct |
72 |
Correct |
21 ms |
3336 KB |
Output is correct |
73 |
Correct |
25 ms |
3328 KB |
Output is correct |
74 |
Correct |
13 ms |
3388 KB |
Output is correct |
75 |
Correct |
9 ms |
3340 KB |
Output is correct |
76 |
Correct |
9 ms |
3284 KB |
Output is correct |
77 |
Correct |
9 ms |
3284 KB |
Output is correct |
78 |
Correct |
9 ms |
3344 KB |
Output is correct |
79 |
Correct |
9 ms |
3368 KB |
Output is correct |
80 |
Correct |
9 ms |
3284 KB |
Output is correct |
81 |
Correct |
10 ms |
3336 KB |
Output is correct |
82 |
Correct |
11 ms |
3344 KB |
Output is correct |
83 |
Correct |
10 ms |
3284 KB |
Output is correct |
84 |
Correct |
10 ms |
3284 KB |
Output is correct |
85 |
Correct |
10 ms |
3344 KB |
Output is correct |
86 |
Correct |
11 ms |
3308 KB |
Output is correct |
87 |
Correct |
36 ms |
4296 KB |
Output is correct |
88 |
Correct |
118 ms |
7812 KB |
Output is correct |
89 |
Correct |
482 ms |
20200 KB |
Output is correct |
90 |
Correct |
491 ms |
20224 KB |
Output is correct |
91 |
Correct |
505 ms |
20152 KB |
Output is correct |
92 |
Correct |
210 ms |
24144 KB |
Output is correct |
93 |
Correct |
211 ms |
24148 KB |
Output is correct |
94 |
Correct |
220 ms |
24140 KB |
Output is correct |
95 |
Correct |
266 ms |
21648 KB |
Output is correct |
96 |
Correct |
285 ms |
21992 KB |
Output is correct |
97 |
Correct |
252 ms |
22024 KB |
Output is correct |
98 |
Correct |
261 ms |
22000 KB |
Output is correct |
99 |
Correct |
241 ms |
21508 KB |
Output is correct |
100 |
Correct |
868 ms |
20240 KB |
Output is correct |
101 |
Correct |
918 ms |
20676 KB |
Output is correct |
102 |
Correct |
928 ms |
20604 KB |
Output is correct |
103 |
Correct |
928 ms |
20664 KB |
Output is correct |
104 |
Correct |
299 ms |
21464 KB |
Output is correct |
105 |
Correct |
290 ms |
21456 KB |
Output is correct |
106 |
Correct |
296 ms |
21492 KB |
Output is correct |
107 |
Correct |
442 ms |
18948 KB |
Output is correct |
108 |
Correct |
473 ms |
19200 KB |
Output is correct |
109 |
Correct |
487 ms |
19520 KB |
Output is correct |
110 |
Correct |
211 ms |
23268 KB |
Output is correct |
111 |
Correct |
246 ms |
21644 KB |
Output is correct |
112 |
Correct |
250 ms |
21084 KB |
Output is correct |
113 |
Correct |
224 ms |
20532 KB |
Output is correct |
114 |
Correct |
871 ms |
20284 KB |
Output is correct |
115 |
Correct |
901 ms |
19820 KB |
Output is correct |
116 |
Correct |
326 ms |
20560 KB |
Output is correct |
117 |
Correct |
214 ms |
20532 KB |
Output is correct |
118 |
Correct |
214 ms |
20304 KB |
Output is correct |
119 |
Correct |
238 ms |
20236 KB |
Output is correct |
120 |
Correct |
214 ms |
19764 KB |
Output is correct |
121 |
Correct |
214 ms |
19648 KB |
Output is correct |
122 |
Correct |
226 ms |
19660 KB |
Output is correct |
123 |
Correct |
296 ms |
20464 KB |
Output is correct |
124 |
Correct |
307 ms |
20348 KB |
Output is correct |
125 |
Correct |
319 ms |
20216 KB |
Output is correct |
126 |
Correct |
321 ms |
19832 KB |
Output is correct |
127 |
Correct |
308 ms |
19696 KB |
Output is correct |
128 |
Correct |
310 ms |
19608 KB |
Output is correct |