#include <bits/stdc++.h>
using namespace std;
const int NMAX=100000;
const int BS=17;
int Par[NMAX],Dep[NMAX],X[NMAX],dep[NMAX],par[NMAX],S[NMAX];
int n;
vector<vector<int> > e;
void dfs(int i)
{
S[i]=0;
for(int x=0;x<e[i].size();x++){
int j=e[i][x];
dep[j]=dep[i]+1;
par[j]=i;
dfs(j);
S[i]+=S[j];
}
}
void HLD()
{
dep[0]=0;
par[0]=-1;
dfs(0);
int NX=0;
Par[0]=0;
Dep[0]=0;
stack<int> ST;
ST.push(0);
while(!ST.empty()){
int i=ST.top();
ST.pop();
X[i]=NX;
NX++;
int K=-1;
for(int j=0;j<e[i].size();j++){
int k=e[i][j];
if(K==-1||S[K]<S[k]){
K=k;
}
}
if(K==-1)continue;
Par[K]=Par[i];
Dep[K]=Dep[i]+1;
for(int j=0;j<e[i].size();j++){
int k=e[i][j];
if(k==K)continue;
Par[k]=k;
Dep[k]=0;
ST.push(k);
}
ST.push(K);
}
}
stack<pair<int,int> > St[NMAX];
long long bit[1<<BS];
void bitadd(int N,int i,long long v)
{
int p=i+1;
while(p<1<<N){
bit[p]+=v;
p+=p&-p;
}
}
long long bitsum(int N,int i)
{
int p=i+1;
long long ret=0;
while(p>0){
ret+=bit[p];
p-=p&-p;
}
return ret;
}
long long cal_inv(vector<pair<int,int> > &V)
{
int n=V.size(),N=0;
while((1<<N)<=n)N++;
vector<int> X(n);
for(int i=0;i<n;i++)X[i]=V[i].first;
sort(X.begin(),X.end());
for(int i=0;i<n;i++)V[i].first=lower_bound(X.begin(),X.end(),V[i].first)-X.begin();
for(int i=0;i<(1<<N);i++)bit[i]=0;
long long ret=0ll;
for(int t=0;t<n;t++){
ret+=bitsum(N,V[t].first-1)*V[t].second;
bitadd(N,V[t].first,V[t].second);
}
return ret;
}
int main()
{
scanf("%d",&n);
vector<int> C(n);
for(int i=0;i<n;i++){
scanf("%d",&(C[i]));
}
vector<int> A(n-1),B(n-1);
for(int i=0;i<n-1;i++){
scanf("%d%d",&(A[i]),&(B[i]));
A[i]--,B[i]--;
}
e=vector<vector<int> >(n);
for(int i=0;i<n-1;i++){
e[A[i]].push_back(B[i]);
}
HLD();
for(int T=0;T<n-1;T++){
int p=B[T],c=C[p];
vector<pair<int,int> > V;
while(p!=-1){
int d=Dep[p]+1,t=0;
p=Par[p];
stack<pair<int,int> > &S=St[p];
vector<pair<int,int> > V2;
while(!S.empty()&&S.top().second<=d){
pair<int,int> pr=S.top();
S.pop();
V2.push_back(make_pair(pr.first,pr.second-t));
t=pr.second;
}
if(!S.empty()){
V2.push_back(make_pair(S.top().first,d-t));
}
S.push(make_pair(c,d));
for(int x=V2.size()-1;x>=0;x--)V.push_back(V2[x]);
p=par[p];
}
printf("%lld\n",cal_inv(V));
}
return 0;
}
Compilation message
construction.cpp: In function 'void dfs(int)':
construction.cpp:15:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int x=0;x<e[i].size();x++){
~^~~~~~~~~~~~
construction.cpp: In function 'void HLD()':
construction.cpp:40:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<e[i].size();j++){
~^~~~~~~~~~~~
construction.cpp:49:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<e[i].size();j++){
~^~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:101:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
construction.cpp:104:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&(C[i]));
~~~~~^~~~~~~~~~~~~~
construction.cpp:108:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&(A[i]),&(B[i]));
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
67704 KB |
Output is correct |
2 |
Correct |
56 ms |
67816 KB |
Output is correct |
3 |
Correct |
56 ms |
67816 KB |
Output is correct |
4 |
Correct |
57 ms |
67816 KB |
Output is correct |
5 |
Correct |
56 ms |
67944 KB |
Output is correct |
6 |
Correct |
58 ms |
67948 KB |
Output is correct |
7 |
Correct |
56 ms |
67964 KB |
Output is correct |
8 |
Correct |
59 ms |
67964 KB |
Output is correct |
9 |
Correct |
57 ms |
67964 KB |
Output is correct |
10 |
Correct |
56 ms |
68056 KB |
Output is correct |
11 |
Correct |
58 ms |
68116 KB |
Output is correct |
12 |
Correct |
57 ms |
68116 KB |
Output is correct |
13 |
Correct |
59 ms |
68116 KB |
Output is correct |
14 |
Correct |
59 ms |
68116 KB |
Output is correct |
15 |
Correct |
59 ms |
68116 KB |
Output is correct |
16 |
Correct |
58 ms |
68116 KB |
Output is correct |
17 |
Correct |
63 ms |
68116 KB |
Output is correct |
18 |
Correct |
59 ms |
68116 KB |
Output is correct |
19 |
Correct |
58 ms |
68116 KB |
Output is correct |
20 |
Correct |
59 ms |
68116 KB |
Output is correct |
21 |
Correct |
58 ms |
68116 KB |
Output is correct |
22 |
Correct |
59 ms |
68116 KB |
Output is correct |
23 |
Correct |
60 ms |
68116 KB |
Output is correct |
24 |
Correct |
59 ms |
68116 KB |
Output is correct |
25 |
Correct |
59 ms |
68116 KB |
Output is correct |
26 |
Correct |
56 ms |
68116 KB |
Output is correct |
27 |
Correct |
56 ms |
68116 KB |
Output is correct |
28 |
Correct |
57 ms |
68116 KB |
Output is correct |
29 |
Correct |
57 ms |
68116 KB |
Output is correct |
30 |
Correct |
67 ms |
68116 KB |
Output is correct |
31 |
Correct |
57 ms |
68116 KB |
Output is correct |
32 |
Correct |
56 ms |
68116 KB |
Output is correct |
33 |
Correct |
57 ms |
68116 KB |
Output is correct |
34 |
Correct |
59 ms |
68116 KB |
Output is correct |
35 |
Correct |
58 ms |
68116 KB |
Output is correct |
36 |
Correct |
57 ms |
68116 KB |
Output is correct |
37 |
Correct |
58 ms |
68116 KB |
Output is correct |
38 |
Correct |
56 ms |
68116 KB |
Output is correct |
39 |
Correct |
107 ms |
68116 KB |
Output is correct |
40 |
Correct |
59 ms |
68116 KB |
Output is correct |
41 |
Correct |
57 ms |
68116 KB |
Output is correct |
42 |
Correct |
59 ms |
68116 KB |
Output is correct |
43 |
Correct |
59 ms |
68116 KB |
Output is correct |
44 |
Correct |
58 ms |
68116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
67704 KB |
Output is correct |
2 |
Correct |
56 ms |
67816 KB |
Output is correct |
3 |
Correct |
56 ms |
67816 KB |
Output is correct |
4 |
Correct |
57 ms |
67816 KB |
Output is correct |
5 |
Correct |
56 ms |
67944 KB |
Output is correct |
6 |
Correct |
58 ms |
67948 KB |
Output is correct |
7 |
Correct |
56 ms |
67964 KB |
Output is correct |
8 |
Correct |
59 ms |
67964 KB |
Output is correct |
9 |
Correct |
57 ms |
67964 KB |
Output is correct |
10 |
Correct |
56 ms |
68056 KB |
Output is correct |
11 |
Correct |
58 ms |
68116 KB |
Output is correct |
12 |
Correct |
57 ms |
68116 KB |
Output is correct |
13 |
Correct |
59 ms |
68116 KB |
Output is correct |
14 |
Correct |
59 ms |
68116 KB |
Output is correct |
15 |
Correct |
59 ms |
68116 KB |
Output is correct |
16 |
Correct |
58 ms |
68116 KB |
Output is correct |
17 |
Correct |
63 ms |
68116 KB |
Output is correct |
18 |
Correct |
59 ms |
68116 KB |
Output is correct |
19 |
Correct |
58 ms |
68116 KB |
Output is correct |
20 |
Correct |
59 ms |
68116 KB |
Output is correct |
21 |
Correct |
58 ms |
68116 KB |
Output is correct |
22 |
Correct |
59 ms |
68116 KB |
Output is correct |
23 |
Correct |
60 ms |
68116 KB |
Output is correct |
24 |
Correct |
59 ms |
68116 KB |
Output is correct |
25 |
Correct |
59 ms |
68116 KB |
Output is correct |
26 |
Correct |
56 ms |
68116 KB |
Output is correct |
27 |
Correct |
56 ms |
68116 KB |
Output is correct |
28 |
Correct |
57 ms |
68116 KB |
Output is correct |
29 |
Correct |
57 ms |
68116 KB |
Output is correct |
30 |
Correct |
67 ms |
68116 KB |
Output is correct |
31 |
Correct |
57 ms |
68116 KB |
Output is correct |
32 |
Correct |
56 ms |
68116 KB |
Output is correct |
33 |
Correct |
57 ms |
68116 KB |
Output is correct |
34 |
Correct |
59 ms |
68116 KB |
Output is correct |
35 |
Correct |
58 ms |
68116 KB |
Output is correct |
36 |
Correct |
57 ms |
68116 KB |
Output is correct |
37 |
Correct |
58 ms |
68116 KB |
Output is correct |
38 |
Correct |
56 ms |
68116 KB |
Output is correct |
39 |
Correct |
107 ms |
68116 KB |
Output is correct |
40 |
Correct |
59 ms |
68116 KB |
Output is correct |
41 |
Correct |
57 ms |
68116 KB |
Output is correct |
42 |
Correct |
59 ms |
68116 KB |
Output is correct |
43 |
Correct |
59 ms |
68116 KB |
Output is correct |
44 |
Correct |
58 ms |
68116 KB |
Output is correct |
45 |
Correct |
61 ms |
68116 KB |
Output is correct |
46 |
Correct |
89 ms |
68252 KB |
Output is correct |
47 |
Correct |
73 ms |
68412 KB |
Output is correct |
48 |
Correct |
71 ms |
68488 KB |
Output is correct |
49 |
Correct |
72 ms |
68804 KB |
Output is correct |
50 |
Correct |
66 ms |
68876 KB |
Output is correct |
51 |
Correct |
65 ms |
68876 KB |
Output is correct |
52 |
Correct |
66 ms |
68892 KB |
Output is correct |
53 |
Correct |
74 ms |
69036 KB |
Output is correct |
54 |
Correct |
68 ms |
69076 KB |
Output is correct |
55 |
Correct |
68 ms |
69076 KB |
Output is correct |
56 |
Correct |
70 ms |
69076 KB |
Output is correct |
57 |
Correct |
70 ms |
69076 KB |
Output is correct |
58 |
Correct |
74 ms |
69100 KB |
Output is correct |
59 |
Correct |
74 ms |
69112 KB |
Output is correct |
60 |
Correct |
75 ms |
69216 KB |
Output is correct |
61 |
Correct |
69 ms |
69272 KB |
Output is correct |
62 |
Correct |
142 ms |
69336 KB |
Output is correct |
63 |
Correct |
68 ms |
69396 KB |
Output is correct |
64 |
Correct |
71 ms |
69396 KB |
Output is correct |
65 |
Correct |
71 ms |
69528 KB |
Output is correct |
66 |
Correct |
71 ms |
69528 KB |
Output is correct |
67 |
Correct |
71 ms |
69528 KB |
Output is correct |
68 |
Correct |
67 ms |
69724 KB |
Output is correct |
69 |
Correct |
66 ms |
69756 KB |
Output is correct |
70 |
Correct |
66 ms |
69776 KB |
Output is correct |
71 |
Correct |
67 ms |
69796 KB |
Output is correct |
72 |
Correct |
69 ms |
69796 KB |
Output is correct |
73 |
Correct |
71 ms |
69796 KB |
Output is correct |
74 |
Correct |
67 ms |
69804 KB |
Output is correct |
75 |
Correct |
68 ms |
69836 KB |
Output is correct |
76 |
Correct |
67 ms |
69888 KB |
Output is correct |
77 |
Correct |
71 ms |
69932 KB |
Output is correct |
78 |
Correct |
72 ms |
69980 KB |
Output is correct |
79 |
Correct |
68 ms |
70012 KB |
Output is correct |
80 |
Correct |
67 ms |
70012 KB |
Output is correct |
81 |
Correct |
89 ms |
70076 KB |
Output is correct |
82 |
Correct |
69 ms |
70168 KB |
Output is correct |
83 |
Correct |
74 ms |
70168 KB |
Output is correct |
84 |
Correct |
67 ms |
70220 KB |
Output is correct |
85 |
Correct |
68 ms |
70220 KB |
Output is correct |
86 |
Correct |
71 ms |
70220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
67704 KB |
Output is correct |
2 |
Correct |
56 ms |
67816 KB |
Output is correct |
3 |
Correct |
56 ms |
67816 KB |
Output is correct |
4 |
Correct |
57 ms |
67816 KB |
Output is correct |
5 |
Correct |
56 ms |
67944 KB |
Output is correct |
6 |
Correct |
58 ms |
67948 KB |
Output is correct |
7 |
Correct |
56 ms |
67964 KB |
Output is correct |
8 |
Correct |
59 ms |
67964 KB |
Output is correct |
9 |
Correct |
57 ms |
67964 KB |
Output is correct |
10 |
Correct |
56 ms |
68056 KB |
Output is correct |
11 |
Correct |
58 ms |
68116 KB |
Output is correct |
12 |
Correct |
57 ms |
68116 KB |
Output is correct |
13 |
Correct |
59 ms |
68116 KB |
Output is correct |
14 |
Correct |
59 ms |
68116 KB |
Output is correct |
15 |
Correct |
59 ms |
68116 KB |
Output is correct |
16 |
Correct |
58 ms |
68116 KB |
Output is correct |
17 |
Correct |
63 ms |
68116 KB |
Output is correct |
18 |
Correct |
59 ms |
68116 KB |
Output is correct |
19 |
Correct |
58 ms |
68116 KB |
Output is correct |
20 |
Correct |
59 ms |
68116 KB |
Output is correct |
21 |
Correct |
58 ms |
68116 KB |
Output is correct |
22 |
Correct |
59 ms |
68116 KB |
Output is correct |
23 |
Correct |
60 ms |
68116 KB |
Output is correct |
24 |
Correct |
59 ms |
68116 KB |
Output is correct |
25 |
Correct |
59 ms |
68116 KB |
Output is correct |
26 |
Correct |
56 ms |
68116 KB |
Output is correct |
27 |
Correct |
56 ms |
68116 KB |
Output is correct |
28 |
Correct |
57 ms |
68116 KB |
Output is correct |
29 |
Correct |
57 ms |
68116 KB |
Output is correct |
30 |
Correct |
67 ms |
68116 KB |
Output is correct |
31 |
Correct |
57 ms |
68116 KB |
Output is correct |
32 |
Correct |
56 ms |
68116 KB |
Output is correct |
33 |
Correct |
57 ms |
68116 KB |
Output is correct |
34 |
Correct |
59 ms |
68116 KB |
Output is correct |
35 |
Correct |
58 ms |
68116 KB |
Output is correct |
36 |
Correct |
57 ms |
68116 KB |
Output is correct |
37 |
Correct |
58 ms |
68116 KB |
Output is correct |
38 |
Correct |
56 ms |
68116 KB |
Output is correct |
39 |
Correct |
107 ms |
68116 KB |
Output is correct |
40 |
Correct |
59 ms |
68116 KB |
Output is correct |
41 |
Correct |
57 ms |
68116 KB |
Output is correct |
42 |
Correct |
59 ms |
68116 KB |
Output is correct |
43 |
Correct |
59 ms |
68116 KB |
Output is correct |
44 |
Correct |
58 ms |
68116 KB |
Output is correct |
45 |
Correct |
61 ms |
68116 KB |
Output is correct |
46 |
Correct |
89 ms |
68252 KB |
Output is correct |
47 |
Correct |
73 ms |
68412 KB |
Output is correct |
48 |
Correct |
71 ms |
68488 KB |
Output is correct |
49 |
Correct |
72 ms |
68804 KB |
Output is correct |
50 |
Correct |
66 ms |
68876 KB |
Output is correct |
51 |
Correct |
65 ms |
68876 KB |
Output is correct |
52 |
Correct |
66 ms |
68892 KB |
Output is correct |
53 |
Correct |
74 ms |
69036 KB |
Output is correct |
54 |
Correct |
68 ms |
69076 KB |
Output is correct |
55 |
Correct |
68 ms |
69076 KB |
Output is correct |
56 |
Correct |
70 ms |
69076 KB |
Output is correct |
57 |
Correct |
70 ms |
69076 KB |
Output is correct |
58 |
Correct |
74 ms |
69100 KB |
Output is correct |
59 |
Correct |
74 ms |
69112 KB |
Output is correct |
60 |
Correct |
75 ms |
69216 KB |
Output is correct |
61 |
Correct |
69 ms |
69272 KB |
Output is correct |
62 |
Correct |
142 ms |
69336 KB |
Output is correct |
63 |
Correct |
68 ms |
69396 KB |
Output is correct |
64 |
Correct |
71 ms |
69396 KB |
Output is correct |
65 |
Correct |
71 ms |
69528 KB |
Output is correct |
66 |
Correct |
71 ms |
69528 KB |
Output is correct |
67 |
Correct |
71 ms |
69528 KB |
Output is correct |
68 |
Correct |
67 ms |
69724 KB |
Output is correct |
69 |
Correct |
66 ms |
69756 KB |
Output is correct |
70 |
Correct |
66 ms |
69776 KB |
Output is correct |
71 |
Correct |
67 ms |
69796 KB |
Output is correct |
72 |
Correct |
69 ms |
69796 KB |
Output is correct |
73 |
Correct |
71 ms |
69796 KB |
Output is correct |
74 |
Correct |
67 ms |
69804 KB |
Output is correct |
75 |
Correct |
68 ms |
69836 KB |
Output is correct |
76 |
Correct |
67 ms |
69888 KB |
Output is correct |
77 |
Correct |
71 ms |
69932 KB |
Output is correct |
78 |
Correct |
72 ms |
69980 KB |
Output is correct |
79 |
Correct |
68 ms |
70012 KB |
Output is correct |
80 |
Correct |
67 ms |
70012 KB |
Output is correct |
81 |
Correct |
89 ms |
70076 KB |
Output is correct |
82 |
Correct |
69 ms |
70168 KB |
Output is correct |
83 |
Correct |
74 ms |
70168 KB |
Output is correct |
84 |
Correct |
67 ms |
70220 KB |
Output is correct |
85 |
Correct |
68 ms |
70220 KB |
Output is correct |
86 |
Correct |
71 ms |
70220 KB |
Output is correct |
87 |
Correct |
82 ms |
70828 KB |
Output is correct |
88 |
Correct |
132 ms |
73024 KB |
Output is correct |
89 |
Correct |
384 ms |
80568 KB |
Output is correct |
90 |
Correct |
373 ms |
82940 KB |
Output is correct |
91 |
Correct |
360 ms |
84760 KB |
Output is correct |
92 |
Correct |
166 ms |
92872 KB |
Output is correct |
93 |
Correct |
162 ms |
94968 KB |
Output is correct |
94 |
Correct |
184 ms |
97064 KB |
Output is correct |
95 |
Correct |
189 ms |
97064 KB |
Output is correct |
96 |
Correct |
197 ms |
98512 KB |
Output is correct |
97 |
Correct |
192 ms |
100740 KB |
Output is correct |
98 |
Correct |
200 ms |
102716 KB |
Output is correct |
99 |
Correct |
285 ms |
103624 KB |
Output is correct |
100 |
Correct |
365 ms |
103624 KB |
Output is correct |
101 |
Correct |
424 ms |
105336 KB |
Output is correct |
102 |
Correct |
445 ms |
107432 KB |
Output is correct |
103 |
Correct |
435 ms |
109548 KB |
Output is correct |
104 |
Correct |
182 ms |
113592 KB |
Output is correct |
105 |
Correct |
184 ms |
115696 KB |
Output is correct |
106 |
Correct |
187 ms |
117900 KB |
Output is correct |
107 |
Correct |
324 ms |
117900 KB |
Output is correct |
108 |
Correct |
382 ms |
118064 KB |
Output is correct |
109 |
Correct |
374 ms |
119768 KB |
Output is correct |
110 |
Correct |
154 ms |
127472 KB |
Output is correct |
111 |
Correct |
177 ms |
127472 KB |
Output is correct |
112 |
Correct |
181 ms |
128204 KB |
Output is correct |
113 |
Correct |
167 ms |
128464 KB |
Output is correct |
114 |
Correct |
336 ms |
128464 KB |
Output is correct |
115 |
Correct |
436 ms |
129884 KB |
Output is correct |
116 |
Correct |
171 ms |
133200 KB |
Output is correct |
117 |
Correct |
180 ms |
133748 KB |
Output is correct |
118 |
Correct |
187 ms |
135172 KB |
Output is correct |
119 |
Correct |
190 ms |
136724 KB |
Output is correct |
120 |
Correct |
172 ms |
138788 KB |
Output is correct |
121 |
Correct |
183 ms |
139896 KB |
Output is correct |
122 |
Correct |
186 ms |
141356 KB |
Output is correct |
123 |
Correct |
192 ms |
143648 KB |
Output is correct |
124 |
Correct |
201 ms |
145200 KB |
Output is correct |
125 |
Correct |
193 ms |
146624 KB |
Output is correct |
126 |
Correct |
179 ms |
148820 KB |
Output is correct |
127 |
Correct |
186 ms |
149948 KB |
Output is correct |
128 |
Correct |
210 ms |
151112 KB |
Output is correct |