#include<bits/stdc++.h>
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
int readint(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
ll ans;
int n;
int ch[100005][2],fa[100005],siz[100005],val[100005],rev[100005],p[100005];
ll c[100005];
vector<pii> v;
int lowbit(int x){return x&(-x);}
void add(int x,int k){for(;x<=n;x+=lowbit(x))c[x]+=k;}
ll ask(int x){ll ret=0;for(;x;x-=lowbit(x))ret+=c[x];return ret;}
bool nroot(int x){return ch[fa[x]][0]==x||ch[fa[x]][1]==x;}
bool son(int x){return ch[fa[x]][1]==x;}
void reverse(int x){rev[x]^=1,swap(ch[x][0],ch[x][1]);}
void pushdown(int x){if(rev[x])reverse(ch[x][0]),reverse(ch[x][1]),rev[x]=0;}
void update(int x){siz[x]=1+siz[ch[x][0]]+siz[ch[x][1]];}
void rotate(int x){
int y=fa[x],z=fa[y],k=son(x),w=ch[x][!k];
if(nroot(y)) ch[z][son(y)]=x;
ch[x][!k]=y,ch[y][k]=w;
if(w) fa[w]=y;
fa[y]=x,fa[x]=z;
if(!nroot(x)) val[x]=val[y];
update(y),update(x);
}
void pushall(int x){
if(nroot(x)) pushall(fa[x]);
pushdown(x);
}
void splay(int x){
pushall(x);
while(nroot(x)){
int y=fa[x];
if(nroot(y)) rotate(son(x)==son(y)?y:x);
rotate(x);
}
}
void access(int x,int k=0){
for(int y=0;x;x=fa[y=x]){
splay(x);
val[ch[x][1]]=val[x],ch[x][1]=y;
update(x);
if(k) ans+=1ll*(siz[x]-siz[ch[x][1]])*ask(val[x]-1);
if(k) add(val[x],siz[x]-siz[ch[x][1]]),v.push_back(mp(val[x],siz[x]-siz[ch[x][1]]));
if(k) val[x]=k;
}
}
void makeroot(int x){access(x); splay(x); reverse(x);}
int main(){
n=readint();
for(int i=1;i<=n;i++) p[i]=val[i]=readint();
sort(p+1,p+n+1);
for(int i=1;i<=n;i++) val[i]=lower_bound(p+1,p+n+1,val[i])-p;
for(int i=1;i<=n;i++) siz[i]=1;
int x,y;
for(int i=1;i<=n-1;i++){
x=readint(); y=readint();
ans=0;
// cout<<"###########################"<<endl;
// for(int j=1;j<=n;j++) cout<<j<<' '<<fa[j]<<' '<<ch[j][0]<<' '<<ch[j][1]<<' '<<val[j]<<' '<<siz[j]<<endl;
// cout<<"###########################"<<endl;
access(x,val[y]);
printf("%lld\n",ans);
fa[y]=x;
for(int j=0;j<v.size();j++) add(v[j].fi,-v[j].se);
v.clear();
}
return 0;
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:87:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<v.size();j++) add(v[j].fi,-v[j].se);
~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
384 KB |
Output is correct |
33 |
Correct |
3 ms |
384 KB |
Output is correct |
34 |
Correct |
3 ms |
512 KB |
Output is correct |
35 |
Correct |
2 ms |
384 KB |
Output is correct |
36 |
Correct |
3 ms |
384 KB |
Output is correct |
37 |
Correct |
3 ms |
512 KB |
Output is correct |
38 |
Correct |
3 ms |
384 KB |
Output is correct |
39 |
Correct |
3 ms |
384 KB |
Output is correct |
40 |
Correct |
3 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
2 ms |
384 KB |
Output is correct |
44 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
384 KB |
Output is correct |
33 |
Correct |
3 ms |
384 KB |
Output is correct |
34 |
Correct |
3 ms |
512 KB |
Output is correct |
35 |
Correct |
2 ms |
384 KB |
Output is correct |
36 |
Correct |
3 ms |
384 KB |
Output is correct |
37 |
Correct |
3 ms |
512 KB |
Output is correct |
38 |
Correct |
3 ms |
384 KB |
Output is correct |
39 |
Correct |
3 ms |
384 KB |
Output is correct |
40 |
Correct |
3 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
2 ms |
384 KB |
Output is correct |
44 |
Correct |
3 ms |
384 KB |
Output is correct |
45 |
Correct |
3 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
512 KB |
Output is correct |
47 |
Correct |
6 ms |
640 KB |
Output is correct |
48 |
Correct |
6 ms |
640 KB |
Output is correct |
49 |
Correct |
6 ms |
640 KB |
Output is correct |
50 |
Correct |
15 ms |
512 KB |
Output is correct |
51 |
Correct |
5 ms |
560 KB |
Output is correct |
52 |
Correct |
4 ms |
512 KB |
Output is correct |
53 |
Correct |
6 ms |
612 KB |
Output is correct |
54 |
Correct |
5 ms |
640 KB |
Output is correct |
55 |
Correct |
6 ms |
640 KB |
Output is correct |
56 |
Correct |
5 ms |
640 KB |
Output is correct |
57 |
Correct |
6 ms |
512 KB |
Output is correct |
58 |
Correct |
8 ms |
640 KB |
Output is correct |
59 |
Correct |
6 ms |
512 KB |
Output is correct |
60 |
Correct |
6 ms |
512 KB |
Output is correct |
61 |
Correct |
6 ms |
640 KB |
Output is correct |
62 |
Correct |
6 ms |
640 KB |
Output is correct |
63 |
Correct |
6 ms |
640 KB |
Output is correct |
64 |
Correct |
5 ms |
512 KB |
Output is correct |
65 |
Correct |
5 ms |
512 KB |
Output is correct |
66 |
Correct |
5 ms |
512 KB |
Output is correct |
67 |
Correct |
6 ms |
512 KB |
Output is correct |
68 |
Correct |
4 ms |
512 KB |
Output is correct |
69 |
Correct |
5 ms |
640 KB |
Output is correct |
70 |
Correct |
4 ms |
640 KB |
Output is correct |
71 |
Correct |
6 ms |
640 KB |
Output is correct |
72 |
Correct |
6 ms |
512 KB |
Output is correct |
73 |
Correct |
6 ms |
512 KB |
Output is correct |
74 |
Correct |
5 ms |
512 KB |
Output is correct |
75 |
Correct |
5 ms |
640 KB |
Output is correct |
76 |
Correct |
5 ms |
512 KB |
Output is correct |
77 |
Correct |
5 ms |
512 KB |
Output is correct |
78 |
Correct |
5 ms |
640 KB |
Output is correct |
79 |
Correct |
5 ms |
512 KB |
Output is correct |
80 |
Correct |
4 ms |
588 KB |
Output is correct |
81 |
Correct |
6 ms |
640 KB |
Output is correct |
82 |
Correct |
6 ms |
640 KB |
Output is correct |
83 |
Correct |
6 ms |
640 KB |
Output is correct |
84 |
Correct |
7 ms |
640 KB |
Output is correct |
85 |
Correct |
6 ms |
512 KB |
Output is correct |
86 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
384 KB |
Output is correct |
33 |
Correct |
3 ms |
384 KB |
Output is correct |
34 |
Correct |
3 ms |
512 KB |
Output is correct |
35 |
Correct |
2 ms |
384 KB |
Output is correct |
36 |
Correct |
3 ms |
384 KB |
Output is correct |
37 |
Correct |
3 ms |
512 KB |
Output is correct |
38 |
Correct |
3 ms |
384 KB |
Output is correct |
39 |
Correct |
3 ms |
384 KB |
Output is correct |
40 |
Correct |
3 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
2 ms |
384 KB |
Output is correct |
44 |
Correct |
3 ms |
384 KB |
Output is correct |
45 |
Correct |
3 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
512 KB |
Output is correct |
47 |
Correct |
6 ms |
640 KB |
Output is correct |
48 |
Correct |
6 ms |
640 KB |
Output is correct |
49 |
Correct |
6 ms |
640 KB |
Output is correct |
50 |
Correct |
15 ms |
512 KB |
Output is correct |
51 |
Correct |
5 ms |
560 KB |
Output is correct |
52 |
Correct |
4 ms |
512 KB |
Output is correct |
53 |
Correct |
6 ms |
612 KB |
Output is correct |
54 |
Correct |
5 ms |
640 KB |
Output is correct |
55 |
Correct |
6 ms |
640 KB |
Output is correct |
56 |
Correct |
5 ms |
640 KB |
Output is correct |
57 |
Correct |
6 ms |
512 KB |
Output is correct |
58 |
Correct |
8 ms |
640 KB |
Output is correct |
59 |
Correct |
6 ms |
512 KB |
Output is correct |
60 |
Correct |
6 ms |
512 KB |
Output is correct |
61 |
Correct |
6 ms |
640 KB |
Output is correct |
62 |
Correct |
6 ms |
640 KB |
Output is correct |
63 |
Correct |
6 ms |
640 KB |
Output is correct |
64 |
Correct |
5 ms |
512 KB |
Output is correct |
65 |
Correct |
5 ms |
512 KB |
Output is correct |
66 |
Correct |
5 ms |
512 KB |
Output is correct |
67 |
Correct |
6 ms |
512 KB |
Output is correct |
68 |
Correct |
4 ms |
512 KB |
Output is correct |
69 |
Correct |
5 ms |
640 KB |
Output is correct |
70 |
Correct |
4 ms |
640 KB |
Output is correct |
71 |
Correct |
6 ms |
640 KB |
Output is correct |
72 |
Correct |
6 ms |
512 KB |
Output is correct |
73 |
Correct |
6 ms |
512 KB |
Output is correct |
74 |
Correct |
5 ms |
512 KB |
Output is correct |
75 |
Correct |
5 ms |
640 KB |
Output is correct |
76 |
Correct |
5 ms |
512 KB |
Output is correct |
77 |
Correct |
5 ms |
512 KB |
Output is correct |
78 |
Correct |
5 ms |
640 KB |
Output is correct |
79 |
Correct |
5 ms |
512 KB |
Output is correct |
80 |
Correct |
4 ms |
588 KB |
Output is correct |
81 |
Correct |
6 ms |
640 KB |
Output is correct |
82 |
Correct |
6 ms |
640 KB |
Output is correct |
83 |
Correct |
6 ms |
640 KB |
Output is correct |
84 |
Correct |
7 ms |
640 KB |
Output is correct |
85 |
Correct |
6 ms |
512 KB |
Output is correct |
86 |
Correct |
6 ms |
512 KB |
Output is correct |
87 |
Correct |
11 ms |
896 KB |
Output is correct |
88 |
Correct |
35 ms |
2040 KB |
Output is correct |
89 |
Correct |
165 ms |
5900 KB |
Output is correct |
90 |
Correct |
167 ms |
5904 KB |
Output is correct |
91 |
Correct |
151 ms |
5880 KB |
Output is correct |
92 |
Correct |
97 ms |
5752 KB |
Output is correct |
93 |
Correct |
95 ms |
5880 KB |
Output is correct |
94 |
Correct |
82 ms |
5752 KB |
Output is correct |
95 |
Correct |
74 ms |
6016 KB |
Output is correct |
96 |
Correct |
94 ms |
6480 KB |
Output is correct |
97 |
Correct |
83 ms |
6492 KB |
Output is correct |
98 |
Correct |
83 ms |
6400 KB |
Output is correct |
99 |
Correct |
90 ms |
7544 KB |
Output is correct |
100 |
Correct |
185 ms |
5624 KB |
Output is correct |
101 |
Correct |
260 ms |
6008 KB |
Output is correct |
102 |
Correct |
213 ms |
6008 KB |
Output is correct |
103 |
Correct |
213 ms |
5880 KB |
Output is correct |
104 |
Correct |
136 ms |
6904 KB |
Output is correct |
105 |
Correct |
147 ms |
7160 KB |
Output is correct |
106 |
Correct |
177 ms |
6572 KB |
Output is correct |
107 |
Correct |
142 ms |
4344 KB |
Output is correct |
108 |
Correct |
156 ms |
4984 KB |
Output is correct |
109 |
Correct |
171 ms |
5496 KB |
Output is correct |
110 |
Correct |
82 ms |
5240 KB |
Output is correct |
111 |
Correct |
92 ms |
6112 KB |
Output is correct |
112 |
Correct |
116 ms |
5848 KB |
Output is correct |
113 |
Correct |
88 ms |
6904 KB |
Output is correct |
114 |
Correct |
157 ms |
5580 KB |
Output is correct |
115 |
Correct |
209 ms |
5412 KB |
Output is correct |
116 |
Correct |
160 ms |
6272 KB |
Output is correct |
117 |
Correct |
125 ms |
6520 KB |
Output is correct |
118 |
Correct |
97 ms |
6296 KB |
Output is correct |
119 |
Correct |
121 ms |
5880 KB |
Output is correct |
120 |
Correct |
90 ms |
6008 KB |
Output is correct |
121 |
Correct |
107 ms |
5752 KB |
Output is correct |
122 |
Correct |
94 ms |
5672 KB |
Output is correct |
123 |
Correct |
157 ms |
6424 KB |
Output is correct |
124 |
Correct |
167 ms |
6136 KB |
Output is correct |
125 |
Correct |
157 ms |
6148 KB |
Output is correct |
126 |
Correct |
152 ms |
6012 KB |
Output is correct |
127 |
Correct |
147 ms |
5996 KB |
Output is correct |
128 |
Correct |
151 ms |
5624 KB |
Output is correct |