//1L1YA
#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3,unrool-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
#define Pb push_back
#define F first
#define S second
#define endl '\n'
#define sep ' '
#define all(x) x.begin(),x.end()
#define al(x,n) x+1,x+n+1
#define SZ(x) ((int)x.size())
#define lc (id<<1)
#define rc (lc|1)
#define mid (l+r>>1)
#define dokme(x) cout << x << endl, exit(0)
#define sik(x) cout << x << endl;continue;
#define FastIO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define FileIO freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll oo=1e18;
const int mod=1e9+7;
const int inf=1e9+111;
const int N=1e5+11;
const int lg=19;
int n,t=1,a[N],h[N],sz[N],st[N],up[N],fen[N],head[N],seg[N<<2],par[lg][N];
vector<int> G[N];
vector<pii> E(N);
void shift(int id){
if(!seg[id]) return;
seg[lc]=seg[id];seg[rc]=seg[id];
seg[id]=0;
}
void update(int ql,int qr,int val,int id=1,int l=1,int r=n+1){
if(qr<=l || ql>=r) return;
if(ql<=l && r<=qr){
seg[id]=val;
return;
}
shift(id);
update(ql,qr,val,lc,l,mid);
update(ql,qr,val,rc,mid,r);
}
int get(int pos,int id=1,int l=1,int r=n+1){
if(r-l==1) return seg[id];
shift(id);
if(pos<mid) return get(pos,lc,l,mid);
return get(pos,rc,mid,r);
}
bool cmp(int x,int y){
return sz[x]>sz[y];
}
void dfsset(int v){
sz[v]=1;
for(int u: G[v]){
h[u]=h[v]+1;
dfsset(u);
sz[v]+=sz[u];
}
sort(all(G[v]),cmp);
}
void dfs_hld(int v){
st[v]=t++;
if(SZ(G[v])) head[G[v][0]]=head[v];
for(int u: G[v]) dfs_hld(u);
}
void update_path(int v,int x){
while(head[v]!=1){
update(st[head[v]],st[v]+1,x);
v=par[0][head[v]];
}
update(1,st[v]+1,x);
}
int jump(int v,int k){
if(k<0) return v;
for(int i=0;i<lg;i++)
if(k>>i & 1)
v=par[i][v];
return v;
}
void upd(int x,int y){
for(x=n-x;x<=n;x+=x&-x) fen[x]+=y;
}
int gett(int x){
int ans=0;
for(x=n-x;x;x-=x&-x) ans+=fen[x];
return ans;
}
int main(){
FastIO
cin >> n;
vector<int> comp;
for(int i=1;i<=n;i++) cin >> a[i],comp.Pb(a[i]);
sort(all(comp));
comp.resize(unique(all(comp))-comp.begin());
for(int i=1;i<=n;i++) a[i]=lower_bound(all(comp),a[i])-comp.begin();
for(int i=1;i<n;i++){
int u,v;
cin >> u >> v;
par[0][v]=u;
G[u].Pb(v);
E[i]={u,v};
}
for(int i=1;i<lg;i++)
for(int v=1;v<=n;v++)
par[i][v]=par[i-1][par[i-1][v]];
dfsset(1);
for(int i=1;i<=n;i++) head[i]=i;
dfs_hld(1);
update_path(1,1);
up[1]=1;
for(int i=1;i<n;i++){
int u=E[i].F,v=E[i].S;
vector<pii> vc;
while(u){
int w=get(st[u]);
vc.Pb({w,h[u]-h[up[w]]+1});
int p=jump(w,h[w]-h[u]-1);
u=par[0][up[w]];
up[w]=p;
}
reverse(all(vc));
ll ans=0;
for(auto [x,y]: vc) ans+=(ll)y*gett(a[x]+1),upd(a[x],y);
cout << ans << endl;
for(auto [x,y]: vc) upd(a[x],-y);
up[v]=1;
update_path(v,v);
}
return 0;
}
Compilation message
construction.cpp: In function 'void update(int, int, int, int, int, int)':
construction.cpp:23:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | #define mid (l+r>>1)
| ~^~
construction.cpp:54:24: note: in expansion of macro 'mid'
54 | update(ql,qr,val,lc,l,mid);
| ^~~
construction.cpp:23:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | #define mid (l+r>>1)
| ~^~
construction.cpp:55:22: note: in expansion of macro 'mid'
55 | update(ql,qr,val,rc,mid,r);
| ^~~
construction.cpp: In function 'int get(int, int, int, int)':
construction.cpp:23:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | #define mid (l+r>>1)
| ~^~
construction.cpp:61:9: note: in expansion of macro 'mid'
61 | if(pos<mid) return get(pos,lc,l,mid);
| ^~~
construction.cpp:23:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | #define mid (l+r>>1)
| ~^~
construction.cpp:61:34: note: in expansion of macro 'mid'
61 | if(pos<mid) return get(pos,lc,l,mid);
| ^~~
construction.cpp:23:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | #define mid (l+r>>1)
| ~^~
construction.cpp:62:20: note: in expansion of macro 'mid'
62 | return get(pos,rc,mid,r);
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
13656 KB |
Output is correct |
2 |
Correct |
2 ms |
13660 KB |
Output is correct |
3 |
Correct |
2 ms |
13660 KB |
Output is correct |
4 |
Correct |
2 ms |
13660 KB |
Output is correct |
5 |
Correct |
3 ms |
13660 KB |
Output is correct |
6 |
Correct |
2 ms |
13660 KB |
Output is correct |
7 |
Correct |
2 ms |
13664 KB |
Output is correct |
8 |
Correct |
2 ms |
13660 KB |
Output is correct |
9 |
Correct |
2 ms |
13660 KB |
Output is correct |
10 |
Correct |
2 ms |
13660 KB |
Output is correct |
11 |
Correct |
2 ms |
13916 KB |
Output is correct |
12 |
Correct |
2 ms |
13660 KB |
Output is correct |
13 |
Correct |
2 ms |
13800 KB |
Output is correct |
14 |
Correct |
2 ms |
13660 KB |
Output is correct |
15 |
Correct |
2 ms |
13660 KB |
Output is correct |
16 |
Correct |
2 ms |
13660 KB |
Output is correct |
17 |
Correct |
2 ms |
13656 KB |
Output is correct |
18 |
Correct |
2 ms |
13660 KB |
Output is correct |
19 |
Correct |
2 ms |
13660 KB |
Output is correct |
20 |
Correct |
2 ms |
13660 KB |
Output is correct |
21 |
Correct |
2 ms |
13660 KB |
Output is correct |
22 |
Correct |
2 ms |
13724 KB |
Output is correct |
23 |
Correct |
2 ms |
13660 KB |
Output is correct |
24 |
Correct |
2 ms |
13768 KB |
Output is correct |
25 |
Correct |
2 ms |
13656 KB |
Output is correct |
26 |
Correct |
2 ms |
13660 KB |
Output is correct |
27 |
Correct |
2 ms |
13660 KB |
Output is correct |
28 |
Correct |
2 ms |
13740 KB |
Output is correct |
29 |
Correct |
2 ms |
13660 KB |
Output is correct |
30 |
Correct |
2 ms |
13656 KB |
Output is correct |
31 |
Correct |
2 ms |
13660 KB |
Output is correct |
32 |
Correct |
2 ms |
13660 KB |
Output is correct |
33 |
Correct |
2 ms |
13660 KB |
Output is correct |
34 |
Correct |
2 ms |
13656 KB |
Output is correct |
35 |
Correct |
2 ms |
13660 KB |
Output is correct |
36 |
Correct |
2 ms |
13656 KB |
Output is correct |
37 |
Correct |
2 ms |
13660 KB |
Output is correct |
38 |
Correct |
2 ms |
13656 KB |
Output is correct |
39 |
Correct |
2 ms |
13660 KB |
Output is correct |
40 |
Correct |
2 ms |
13660 KB |
Output is correct |
41 |
Correct |
2 ms |
13732 KB |
Output is correct |
42 |
Correct |
2 ms |
13656 KB |
Output is correct |
43 |
Correct |
2 ms |
13660 KB |
Output is correct |
44 |
Correct |
2 ms |
13660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
13656 KB |
Output is correct |
2 |
Correct |
2 ms |
13660 KB |
Output is correct |
3 |
Correct |
2 ms |
13660 KB |
Output is correct |
4 |
Correct |
2 ms |
13660 KB |
Output is correct |
5 |
Correct |
3 ms |
13660 KB |
Output is correct |
6 |
Correct |
2 ms |
13660 KB |
Output is correct |
7 |
Correct |
2 ms |
13664 KB |
Output is correct |
8 |
Correct |
2 ms |
13660 KB |
Output is correct |
9 |
Correct |
2 ms |
13660 KB |
Output is correct |
10 |
Correct |
2 ms |
13660 KB |
Output is correct |
11 |
Correct |
2 ms |
13916 KB |
Output is correct |
12 |
Correct |
2 ms |
13660 KB |
Output is correct |
13 |
Correct |
2 ms |
13800 KB |
Output is correct |
14 |
Correct |
2 ms |
13660 KB |
Output is correct |
15 |
Correct |
2 ms |
13660 KB |
Output is correct |
16 |
Correct |
2 ms |
13660 KB |
Output is correct |
17 |
Correct |
2 ms |
13656 KB |
Output is correct |
18 |
Correct |
2 ms |
13660 KB |
Output is correct |
19 |
Correct |
2 ms |
13660 KB |
Output is correct |
20 |
Correct |
2 ms |
13660 KB |
Output is correct |
21 |
Correct |
2 ms |
13660 KB |
Output is correct |
22 |
Correct |
2 ms |
13724 KB |
Output is correct |
23 |
Correct |
2 ms |
13660 KB |
Output is correct |
24 |
Correct |
2 ms |
13768 KB |
Output is correct |
25 |
Correct |
2 ms |
13656 KB |
Output is correct |
26 |
Correct |
2 ms |
13660 KB |
Output is correct |
27 |
Correct |
2 ms |
13660 KB |
Output is correct |
28 |
Correct |
2 ms |
13740 KB |
Output is correct |
29 |
Correct |
2 ms |
13660 KB |
Output is correct |
30 |
Correct |
2 ms |
13656 KB |
Output is correct |
31 |
Correct |
2 ms |
13660 KB |
Output is correct |
32 |
Correct |
2 ms |
13660 KB |
Output is correct |
33 |
Correct |
2 ms |
13660 KB |
Output is correct |
34 |
Correct |
2 ms |
13656 KB |
Output is correct |
35 |
Correct |
2 ms |
13660 KB |
Output is correct |
36 |
Correct |
2 ms |
13656 KB |
Output is correct |
37 |
Correct |
2 ms |
13660 KB |
Output is correct |
38 |
Correct |
2 ms |
13656 KB |
Output is correct |
39 |
Correct |
2 ms |
13660 KB |
Output is correct |
40 |
Correct |
2 ms |
13660 KB |
Output is correct |
41 |
Correct |
2 ms |
13732 KB |
Output is correct |
42 |
Correct |
2 ms |
13656 KB |
Output is correct |
43 |
Correct |
2 ms |
13660 KB |
Output is correct |
44 |
Correct |
2 ms |
13660 KB |
Output is correct |
45 |
Correct |
4 ms |
13660 KB |
Output is correct |
46 |
Correct |
7 ms |
13916 KB |
Output is correct |
47 |
Correct |
7 ms |
13916 KB |
Output is correct |
48 |
Correct |
7 ms |
13916 KB |
Output is correct |
49 |
Correct |
4 ms |
14172 KB |
Output is correct |
50 |
Correct |
4 ms |
14344 KB |
Output is correct |
51 |
Correct |
4 ms |
14308 KB |
Output is correct |
52 |
Correct |
4 ms |
13916 KB |
Output is correct |
53 |
Correct |
4 ms |
13916 KB |
Output is correct |
54 |
Correct |
5 ms |
13916 KB |
Output is correct |
55 |
Correct |
5 ms |
13916 KB |
Output is correct |
56 |
Correct |
4 ms |
13916 KB |
Output is correct |
57 |
Correct |
9 ms |
13916 KB |
Output is correct |
58 |
Correct |
9 ms |
13916 KB |
Output is correct |
59 |
Correct |
9 ms |
13916 KB |
Output is correct |
60 |
Correct |
10 ms |
13916 KB |
Output is correct |
61 |
Correct |
5 ms |
13916 KB |
Output is correct |
62 |
Correct |
5 ms |
13916 KB |
Output is correct |
63 |
Correct |
6 ms |
13912 KB |
Output is correct |
64 |
Correct |
6 ms |
13916 KB |
Output is correct |
65 |
Correct |
6 ms |
13916 KB |
Output is correct |
66 |
Correct |
7 ms |
13916 KB |
Output is correct |
67 |
Correct |
7 ms |
13916 KB |
Output is correct |
68 |
Correct |
4 ms |
14172 KB |
Output is correct |
69 |
Correct |
4 ms |
14100 KB |
Output is correct |
70 |
Correct |
4 ms |
13916 KB |
Output is correct |
71 |
Correct |
4 ms |
13916 KB |
Output is correct |
72 |
Correct |
8 ms |
13916 KB |
Output is correct |
73 |
Correct |
8 ms |
13832 KB |
Output is correct |
74 |
Correct |
5 ms |
14168 KB |
Output is correct |
75 |
Correct |
5 ms |
13916 KB |
Output is correct |
76 |
Correct |
5 ms |
13916 KB |
Output is correct |
77 |
Correct |
4 ms |
13916 KB |
Output is correct |
78 |
Correct |
4 ms |
13832 KB |
Output is correct |
79 |
Correct |
4 ms |
13912 KB |
Output is correct |
80 |
Correct |
6 ms |
13912 KB |
Output is correct |
81 |
Correct |
5 ms |
13912 KB |
Output is correct |
82 |
Correct |
6 ms |
13916 KB |
Output is correct |
83 |
Correct |
6 ms |
14116 KB |
Output is correct |
84 |
Correct |
5 ms |
13916 KB |
Output is correct |
85 |
Correct |
5 ms |
13916 KB |
Output is correct |
86 |
Correct |
5 ms |
13916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
13656 KB |
Output is correct |
2 |
Correct |
2 ms |
13660 KB |
Output is correct |
3 |
Correct |
2 ms |
13660 KB |
Output is correct |
4 |
Correct |
2 ms |
13660 KB |
Output is correct |
5 |
Correct |
3 ms |
13660 KB |
Output is correct |
6 |
Correct |
2 ms |
13660 KB |
Output is correct |
7 |
Correct |
2 ms |
13664 KB |
Output is correct |
8 |
Correct |
2 ms |
13660 KB |
Output is correct |
9 |
Correct |
2 ms |
13660 KB |
Output is correct |
10 |
Correct |
2 ms |
13660 KB |
Output is correct |
11 |
Correct |
2 ms |
13916 KB |
Output is correct |
12 |
Correct |
2 ms |
13660 KB |
Output is correct |
13 |
Correct |
2 ms |
13800 KB |
Output is correct |
14 |
Correct |
2 ms |
13660 KB |
Output is correct |
15 |
Correct |
2 ms |
13660 KB |
Output is correct |
16 |
Correct |
2 ms |
13660 KB |
Output is correct |
17 |
Correct |
2 ms |
13656 KB |
Output is correct |
18 |
Correct |
2 ms |
13660 KB |
Output is correct |
19 |
Correct |
2 ms |
13660 KB |
Output is correct |
20 |
Correct |
2 ms |
13660 KB |
Output is correct |
21 |
Correct |
2 ms |
13660 KB |
Output is correct |
22 |
Correct |
2 ms |
13724 KB |
Output is correct |
23 |
Correct |
2 ms |
13660 KB |
Output is correct |
24 |
Correct |
2 ms |
13768 KB |
Output is correct |
25 |
Correct |
2 ms |
13656 KB |
Output is correct |
26 |
Correct |
2 ms |
13660 KB |
Output is correct |
27 |
Correct |
2 ms |
13660 KB |
Output is correct |
28 |
Correct |
2 ms |
13740 KB |
Output is correct |
29 |
Correct |
2 ms |
13660 KB |
Output is correct |
30 |
Correct |
2 ms |
13656 KB |
Output is correct |
31 |
Correct |
2 ms |
13660 KB |
Output is correct |
32 |
Correct |
2 ms |
13660 KB |
Output is correct |
33 |
Correct |
2 ms |
13660 KB |
Output is correct |
34 |
Correct |
2 ms |
13656 KB |
Output is correct |
35 |
Correct |
2 ms |
13660 KB |
Output is correct |
36 |
Correct |
2 ms |
13656 KB |
Output is correct |
37 |
Correct |
2 ms |
13660 KB |
Output is correct |
38 |
Correct |
2 ms |
13656 KB |
Output is correct |
39 |
Correct |
2 ms |
13660 KB |
Output is correct |
40 |
Correct |
2 ms |
13660 KB |
Output is correct |
41 |
Correct |
2 ms |
13732 KB |
Output is correct |
42 |
Correct |
2 ms |
13656 KB |
Output is correct |
43 |
Correct |
2 ms |
13660 KB |
Output is correct |
44 |
Correct |
2 ms |
13660 KB |
Output is correct |
45 |
Correct |
4 ms |
13660 KB |
Output is correct |
46 |
Correct |
7 ms |
13916 KB |
Output is correct |
47 |
Correct |
7 ms |
13916 KB |
Output is correct |
48 |
Correct |
7 ms |
13916 KB |
Output is correct |
49 |
Correct |
4 ms |
14172 KB |
Output is correct |
50 |
Correct |
4 ms |
14344 KB |
Output is correct |
51 |
Correct |
4 ms |
14308 KB |
Output is correct |
52 |
Correct |
4 ms |
13916 KB |
Output is correct |
53 |
Correct |
4 ms |
13916 KB |
Output is correct |
54 |
Correct |
5 ms |
13916 KB |
Output is correct |
55 |
Correct |
5 ms |
13916 KB |
Output is correct |
56 |
Correct |
4 ms |
13916 KB |
Output is correct |
57 |
Correct |
9 ms |
13916 KB |
Output is correct |
58 |
Correct |
9 ms |
13916 KB |
Output is correct |
59 |
Correct |
9 ms |
13916 KB |
Output is correct |
60 |
Correct |
10 ms |
13916 KB |
Output is correct |
61 |
Correct |
5 ms |
13916 KB |
Output is correct |
62 |
Correct |
5 ms |
13916 KB |
Output is correct |
63 |
Correct |
6 ms |
13912 KB |
Output is correct |
64 |
Correct |
6 ms |
13916 KB |
Output is correct |
65 |
Correct |
6 ms |
13916 KB |
Output is correct |
66 |
Correct |
7 ms |
13916 KB |
Output is correct |
67 |
Correct |
7 ms |
13916 KB |
Output is correct |
68 |
Correct |
4 ms |
14172 KB |
Output is correct |
69 |
Correct |
4 ms |
14100 KB |
Output is correct |
70 |
Correct |
4 ms |
13916 KB |
Output is correct |
71 |
Correct |
4 ms |
13916 KB |
Output is correct |
72 |
Correct |
8 ms |
13916 KB |
Output is correct |
73 |
Correct |
8 ms |
13832 KB |
Output is correct |
74 |
Correct |
5 ms |
14168 KB |
Output is correct |
75 |
Correct |
5 ms |
13916 KB |
Output is correct |
76 |
Correct |
5 ms |
13916 KB |
Output is correct |
77 |
Correct |
4 ms |
13916 KB |
Output is correct |
78 |
Correct |
4 ms |
13832 KB |
Output is correct |
79 |
Correct |
4 ms |
13912 KB |
Output is correct |
80 |
Correct |
6 ms |
13912 KB |
Output is correct |
81 |
Correct |
5 ms |
13912 KB |
Output is correct |
82 |
Correct |
6 ms |
13916 KB |
Output is correct |
83 |
Correct |
6 ms |
14116 KB |
Output is correct |
84 |
Correct |
5 ms |
13916 KB |
Output is correct |
85 |
Correct |
5 ms |
13916 KB |
Output is correct |
86 |
Correct |
5 ms |
13916 KB |
Output is correct |
87 |
Correct |
17 ms |
14168 KB |
Output is correct |
88 |
Correct |
60 ms |
15484 KB |
Output is correct |
89 |
Correct |
225 ms |
19620 KB |
Output is correct |
90 |
Correct |
233 ms |
19668 KB |
Output is correct |
91 |
Correct |
227 ms |
19668 KB |
Output is correct |
92 |
Correct |
75 ms |
28844 KB |
Output is correct |
93 |
Correct |
104 ms |
28952 KB |
Output is correct |
94 |
Correct |
76 ms |
28880 KB |
Output is correct |
95 |
Correct |
82 ms |
23736 KB |
Output is correct |
96 |
Correct |
87 ms |
24276 KB |
Output is correct |
97 |
Correct |
84 ms |
24224 KB |
Output is correct |
98 |
Correct |
86 ms |
24276 KB |
Output is correct |
99 |
Correct |
84 ms |
23504 KB |
Output is correct |
100 |
Correct |
340 ms |
19792 KB |
Output is correct |
101 |
Correct |
371 ms |
20176 KB |
Output is correct |
102 |
Correct |
363 ms |
20176 KB |
Output is correct |
103 |
Correct |
355 ms |
20180 KB |
Output is correct |
104 |
Correct |
102 ms |
23508 KB |
Output is correct |
105 |
Correct |
118 ms |
23728 KB |
Output is correct |
106 |
Correct |
99 ms |
23508 KB |
Output is correct |
107 |
Correct |
182 ms |
18900 KB |
Output is correct |
108 |
Correct |
194 ms |
18896 KB |
Output is correct |
109 |
Correct |
220 ms |
19152 KB |
Output is correct |
110 |
Correct |
65 ms |
28368 KB |
Output is correct |
111 |
Correct |
79 ms |
23792 KB |
Output is correct |
112 |
Correct |
75 ms |
23504 KB |
Output is correct |
113 |
Correct |
72 ms |
22996 KB |
Output is correct |
114 |
Correct |
347 ms |
19836 KB |
Output is correct |
115 |
Correct |
343 ms |
19412 KB |
Output is correct |
116 |
Correct |
88 ms |
22992 KB |
Output is correct |
117 |
Correct |
92 ms |
21208 KB |
Output is correct |
118 |
Correct |
98 ms |
20640 KB |
Output is correct |
119 |
Correct |
91 ms |
20180 KB |
Output is correct |
120 |
Correct |
79 ms |
20944 KB |
Output is correct |
121 |
Correct |
98 ms |
20180 KB |
Output is correct |
122 |
Correct |
83 ms |
19924 KB |
Output is correct |
123 |
Correct |
116 ms |
21244 KB |
Output is correct |
124 |
Correct |
111 ms |
20744 KB |
Output is correct |
125 |
Correct |
115 ms |
20388 KB |
Output is correct |
126 |
Correct |
99 ms |
20948 KB |
Output is correct |
127 |
Correct |
109 ms |
20432 KB |
Output is correct |
128 |
Correct |
99 ms |
19964 KB |
Output is correct |