#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]));
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
67704 KB |
Output is correct |
2 |
Correct |
65 ms |
67704 KB |
Output is correct |
3 |
Correct |
69 ms |
67676 KB |
Output is correct |
4 |
Correct |
67 ms |
67832 KB |
Output is correct |
5 |
Correct |
72 ms |
67704 KB |
Output is correct |
6 |
Correct |
67 ms |
67704 KB |
Output is correct |
7 |
Correct |
66 ms |
67744 KB |
Output is correct |
8 |
Correct |
69 ms |
67704 KB |
Output is correct |
9 |
Correct |
76 ms |
67836 KB |
Output is correct |
10 |
Correct |
64 ms |
67704 KB |
Output is correct |
11 |
Correct |
74 ms |
67704 KB |
Output is correct |
12 |
Correct |
67 ms |
67704 KB |
Output is correct |
13 |
Correct |
74 ms |
67704 KB |
Output is correct |
14 |
Correct |
73 ms |
67704 KB |
Output is correct |
15 |
Correct |
65 ms |
67576 KB |
Output is correct |
16 |
Correct |
75 ms |
67704 KB |
Output is correct |
17 |
Correct |
65 ms |
67704 KB |
Output is correct |
18 |
Correct |
73 ms |
67688 KB |
Output is correct |
19 |
Correct |
65 ms |
67704 KB |
Output is correct |
20 |
Correct |
66 ms |
67704 KB |
Output is correct |
21 |
Correct |
66 ms |
67704 KB |
Output is correct |
22 |
Correct |
65 ms |
67704 KB |
Output is correct |
23 |
Correct |
69 ms |
67704 KB |
Output is correct |
24 |
Correct |
75 ms |
67704 KB |
Output is correct |
25 |
Correct |
66 ms |
67832 KB |
Output is correct |
26 |
Correct |
66 ms |
67704 KB |
Output is correct |
27 |
Correct |
66 ms |
67704 KB |
Output is correct |
28 |
Correct |
67 ms |
67732 KB |
Output is correct |
29 |
Correct |
65 ms |
67708 KB |
Output is correct |
30 |
Correct |
66 ms |
67704 KB |
Output is correct |
31 |
Correct |
68 ms |
67704 KB |
Output is correct |
32 |
Correct |
78 ms |
67676 KB |
Output is correct |
33 |
Correct |
65 ms |
67704 KB |
Output is correct |
34 |
Correct |
66 ms |
67760 KB |
Output is correct |
35 |
Correct |
67 ms |
67876 KB |
Output is correct |
36 |
Correct |
67 ms |
67704 KB |
Output is correct |
37 |
Correct |
67 ms |
67704 KB |
Output is correct |
38 |
Correct |
65 ms |
67704 KB |
Output is correct |
39 |
Correct |
66 ms |
67708 KB |
Output is correct |
40 |
Correct |
65 ms |
67704 KB |
Output is correct |
41 |
Correct |
64 ms |
67704 KB |
Output is correct |
42 |
Correct |
64 ms |
67704 KB |
Output is correct |
43 |
Correct |
67 ms |
67704 KB |
Output is correct |
44 |
Correct |
73 ms |
67748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
67704 KB |
Output is correct |
2 |
Correct |
65 ms |
67704 KB |
Output is correct |
3 |
Correct |
69 ms |
67676 KB |
Output is correct |
4 |
Correct |
67 ms |
67832 KB |
Output is correct |
5 |
Correct |
72 ms |
67704 KB |
Output is correct |
6 |
Correct |
67 ms |
67704 KB |
Output is correct |
7 |
Correct |
66 ms |
67744 KB |
Output is correct |
8 |
Correct |
69 ms |
67704 KB |
Output is correct |
9 |
Correct |
76 ms |
67836 KB |
Output is correct |
10 |
Correct |
64 ms |
67704 KB |
Output is correct |
11 |
Correct |
74 ms |
67704 KB |
Output is correct |
12 |
Correct |
67 ms |
67704 KB |
Output is correct |
13 |
Correct |
74 ms |
67704 KB |
Output is correct |
14 |
Correct |
73 ms |
67704 KB |
Output is correct |
15 |
Correct |
65 ms |
67576 KB |
Output is correct |
16 |
Correct |
75 ms |
67704 KB |
Output is correct |
17 |
Correct |
65 ms |
67704 KB |
Output is correct |
18 |
Correct |
73 ms |
67688 KB |
Output is correct |
19 |
Correct |
65 ms |
67704 KB |
Output is correct |
20 |
Correct |
66 ms |
67704 KB |
Output is correct |
21 |
Correct |
66 ms |
67704 KB |
Output is correct |
22 |
Correct |
65 ms |
67704 KB |
Output is correct |
23 |
Correct |
69 ms |
67704 KB |
Output is correct |
24 |
Correct |
75 ms |
67704 KB |
Output is correct |
25 |
Correct |
66 ms |
67832 KB |
Output is correct |
26 |
Correct |
66 ms |
67704 KB |
Output is correct |
27 |
Correct |
66 ms |
67704 KB |
Output is correct |
28 |
Correct |
67 ms |
67732 KB |
Output is correct |
29 |
Correct |
65 ms |
67708 KB |
Output is correct |
30 |
Correct |
66 ms |
67704 KB |
Output is correct |
31 |
Correct |
68 ms |
67704 KB |
Output is correct |
32 |
Correct |
78 ms |
67676 KB |
Output is correct |
33 |
Correct |
65 ms |
67704 KB |
Output is correct |
34 |
Correct |
66 ms |
67760 KB |
Output is correct |
35 |
Correct |
67 ms |
67876 KB |
Output is correct |
36 |
Correct |
67 ms |
67704 KB |
Output is correct |
37 |
Correct |
67 ms |
67704 KB |
Output is correct |
38 |
Correct |
65 ms |
67704 KB |
Output is correct |
39 |
Correct |
66 ms |
67708 KB |
Output is correct |
40 |
Correct |
65 ms |
67704 KB |
Output is correct |
41 |
Correct |
64 ms |
67704 KB |
Output is correct |
42 |
Correct |
64 ms |
67704 KB |
Output is correct |
43 |
Correct |
67 ms |
67704 KB |
Output is correct |
44 |
Correct |
73 ms |
67748 KB |
Output is correct |
45 |
Correct |
66 ms |
67704 KB |
Output is correct |
46 |
Correct |
78 ms |
67960 KB |
Output is correct |
47 |
Correct |
79 ms |
67960 KB |
Output is correct |
48 |
Correct |
89 ms |
68088 KB |
Output is correct |
49 |
Correct |
80 ms |
68276 KB |
Output is correct |
50 |
Correct |
83 ms |
68344 KB |
Output is correct |
51 |
Correct |
76 ms |
68216 KB |
Output is correct |
52 |
Correct |
80 ms |
68300 KB |
Output is correct |
53 |
Correct |
78 ms |
68088 KB |
Output is correct |
54 |
Correct |
76 ms |
68088 KB |
Output is correct |
55 |
Correct |
76 ms |
68088 KB |
Output is correct |
56 |
Correct |
76 ms |
68088 KB |
Output is correct |
57 |
Correct |
79 ms |
67960 KB |
Output is correct |
58 |
Correct |
83 ms |
67960 KB |
Output is correct |
59 |
Correct |
80 ms |
68088 KB |
Output is correct |
60 |
Correct |
84 ms |
67964 KB |
Output is correct |
61 |
Correct |
82 ms |
68220 KB |
Output is correct |
62 |
Correct |
81 ms |
68068 KB |
Output is correct |
63 |
Correct |
78 ms |
68216 KB |
Output is correct |
64 |
Correct |
79 ms |
67960 KB |
Output is correct |
65 |
Correct |
86 ms |
67968 KB |
Output is correct |
66 |
Correct |
88 ms |
68088 KB |
Output is correct |
67 |
Correct |
79 ms |
68016 KB |
Output is correct |
68 |
Correct |
77 ms |
68216 KB |
Output is correct |
69 |
Correct |
79 ms |
68332 KB |
Output is correct |
70 |
Correct |
80 ms |
68344 KB |
Output is correct |
71 |
Correct |
79 ms |
68088 KB |
Output is correct |
72 |
Correct |
90 ms |
67964 KB |
Output is correct |
73 |
Correct |
83 ms |
67960 KB |
Output is correct |
74 |
Correct |
80 ms |
68088 KB |
Output is correct |
75 |
Correct |
82 ms |
68088 KB |
Output is correct |
76 |
Correct |
81 ms |
68092 KB |
Output is correct |
77 |
Correct |
82 ms |
67960 KB |
Output is correct |
78 |
Correct |
80 ms |
68020 KB |
Output is correct |
79 |
Correct |
80 ms |
68088 KB |
Output is correct |
80 |
Correct |
95 ms |
67960 KB |
Output is correct |
81 |
Correct |
82 ms |
68088 KB |
Output is correct |
82 |
Correct |
87 ms |
67936 KB |
Output is correct |
83 |
Correct |
82 ms |
67960 KB |
Output is correct |
84 |
Correct |
79 ms |
67960 KB |
Output is correct |
85 |
Correct |
82 ms |
68088 KB |
Output is correct |
86 |
Correct |
83 ms |
68216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
67704 KB |
Output is correct |
2 |
Correct |
65 ms |
67704 KB |
Output is correct |
3 |
Correct |
69 ms |
67676 KB |
Output is correct |
4 |
Correct |
67 ms |
67832 KB |
Output is correct |
5 |
Correct |
72 ms |
67704 KB |
Output is correct |
6 |
Correct |
67 ms |
67704 KB |
Output is correct |
7 |
Correct |
66 ms |
67744 KB |
Output is correct |
8 |
Correct |
69 ms |
67704 KB |
Output is correct |
9 |
Correct |
76 ms |
67836 KB |
Output is correct |
10 |
Correct |
64 ms |
67704 KB |
Output is correct |
11 |
Correct |
74 ms |
67704 KB |
Output is correct |
12 |
Correct |
67 ms |
67704 KB |
Output is correct |
13 |
Correct |
74 ms |
67704 KB |
Output is correct |
14 |
Correct |
73 ms |
67704 KB |
Output is correct |
15 |
Correct |
65 ms |
67576 KB |
Output is correct |
16 |
Correct |
75 ms |
67704 KB |
Output is correct |
17 |
Correct |
65 ms |
67704 KB |
Output is correct |
18 |
Correct |
73 ms |
67688 KB |
Output is correct |
19 |
Correct |
65 ms |
67704 KB |
Output is correct |
20 |
Correct |
66 ms |
67704 KB |
Output is correct |
21 |
Correct |
66 ms |
67704 KB |
Output is correct |
22 |
Correct |
65 ms |
67704 KB |
Output is correct |
23 |
Correct |
69 ms |
67704 KB |
Output is correct |
24 |
Correct |
75 ms |
67704 KB |
Output is correct |
25 |
Correct |
66 ms |
67832 KB |
Output is correct |
26 |
Correct |
66 ms |
67704 KB |
Output is correct |
27 |
Correct |
66 ms |
67704 KB |
Output is correct |
28 |
Correct |
67 ms |
67732 KB |
Output is correct |
29 |
Correct |
65 ms |
67708 KB |
Output is correct |
30 |
Correct |
66 ms |
67704 KB |
Output is correct |
31 |
Correct |
68 ms |
67704 KB |
Output is correct |
32 |
Correct |
78 ms |
67676 KB |
Output is correct |
33 |
Correct |
65 ms |
67704 KB |
Output is correct |
34 |
Correct |
66 ms |
67760 KB |
Output is correct |
35 |
Correct |
67 ms |
67876 KB |
Output is correct |
36 |
Correct |
67 ms |
67704 KB |
Output is correct |
37 |
Correct |
67 ms |
67704 KB |
Output is correct |
38 |
Correct |
65 ms |
67704 KB |
Output is correct |
39 |
Correct |
66 ms |
67708 KB |
Output is correct |
40 |
Correct |
65 ms |
67704 KB |
Output is correct |
41 |
Correct |
64 ms |
67704 KB |
Output is correct |
42 |
Correct |
64 ms |
67704 KB |
Output is correct |
43 |
Correct |
67 ms |
67704 KB |
Output is correct |
44 |
Correct |
73 ms |
67748 KB |
Output is correct |
45 |
Correct |
66 ms |
67704 KB |
Output is correct |
46 |
Correct |
78 ms |
67960 KB |
Output is correct |
47 |
Correct |
79 ms |
67960 KB |
Output is correct |
48 |
Correct |
89 ms |
68088 KB |
Output is correct |
49 |
Correct |
80 ms |
68276 KB |
Output is correct |
50 |
Correct |
83 ms |
68344 KB |
Output is correct |
51 |
Correct |
76 ms |
68216 KB |
Output is correct |
52 |
Correct |
80 ms |
68300 KB |
Output is correct |
53 |
Correct |
78 ms |
68088 KB |
Output is correct |
54 |
Correct |
76 ms |
68088 KB |
Output is correct |
55 |
Correct |
76 ms |
68088 KB |
Output is correct |
56 |
Correct |
76 ms |
68088 KB |
Output is correct |
57 |
Correct |
79 ms |
67960 KB |
Output is correct |
58 |
Correct |
83 ms |
67960 KB |
Output is correct |
59 |
Correct |
80 ms |
68088 KB |
Output is correct |
60 |
Correct |
84 ms |
67964 KB |
Output is correct |
61 |
Correct |
82 ms |
68220 KB |
Output is correct |
62 |
Correct |
81 ms |
68068 KB |
Output is correct |
63 |
Correct |
78 ms |
68216 KB |
Output is correct |
64 |
Correct |
79 ms |
67960 KB |
Output is correct |
65 |
Correct |
86 ms |
67968 KB |
Output is correct |
66 |
Correct |
88 ms |
68088 KB |
Output is correct |
67 |
Correct |
79 ms |
68016 KB |
Output is correct |
68 |
Correct |
77 ms |
68216 KB |
Output is correct |
69 |
Correct |
79 ms |
68332 KB |
Output is correct |
70 |
Correct |
80 ms |
68344 KB |
Output is correct |
71 |
Correct |
79 ms |
68088 KB |
Output is correct |
72 |
Correct |
90 ms |
67964 KB |
Output is correct |
73 |
Correct |
83 ms |
67960 KB |
Output is correct |
74 |
Correct |
80 ms |
68088 KB |
Output is correct |
75 |
Correct |
82 ms |
68088 KB |
Output is correct |
76 |
Correct |
81 ms |
68092 KB |
Output is correct |
77 |
Correct |
82 ms |
67960 KB |
Output is correct |
78 |
Correct |
80 ms |
68020 KB |
Output is correct |
79 |
Correct |
80 ms |
68088 KB |
Output is correct |
80 |
Correct |
95 ms |
67960 KB |
Output is correct |
81 |
Correct |
82 ms |
68088 KB |
Output is correct |
82 |
Correct |
87 ms |
67936 KB |
Output is correct |
83 |
Correct |
82 ms |
67960 KB |
Output is correct |
84 |
Correct |
79 ms |
67960 KB |
Output is correct |
85 |
Correct |
82 ms |
68088 KB |
Output is correct |
86 |
Correct |
83 ms |
68216 KB |
Output is correct |
87 |
Correct |
88 ms |
68472 KB |
Output is correct |
88 |
Correct |
132 ms |
70020 KB |
Output is correct |
89 |
Correct |
361 ms |
75560 KB |
Output is correct |
90 |
Correct |
376 ms |
75384 KB |
Output is correct |
91 |
Correct |
338 ms |
75512 KB |
Output is correct |
92 |
Correct |
167 ms |
81524 KB |
Output is correct |
93 |
Correct |
158 ms |
81552 KB |
Output is correct |
94 |
Correct |
178 ms |
81528 KB |
Output is correct |
95 |
Correct |
193 ms |
79188 KB |
Output is correct |
96 |
Correct |
184 ms |
81236 KB |
Output is correct |
97 |
Correct |
187 ms |
81304 KB |
Output is correct |
98 |
Correct |
188 ms |
81280 KB |
Output is correct |
99 |
Correct |
171 ms |
80044 KB |
Output is correct |
100 |
Correct |
357 ms |
77720 KB |
Output is correct |
101 |
Correct |
435 ms |
78200 KB |
Output is correct |
102 |
Correct |
416 ms |
78072 KB |
Output is correct |
103 |
Correct |
393 ms |
78072 KB |
Output is correct |
104 |
Correct |
177 ms |
80040 KB |
Output is correct |
105 |
Correct |
176 ms |
80040 KB |
Output is correct |
106 |
Correct |
173 ms |
79972 KB |
Output is correct |
107 |
Correct |
303 ms |
76724 KB |
Output is correct |
108 |
Correct |
338 ms |
76892 KB |
Output is correct |
109 |
Correct |
401 ms |
77148 KB |
Output is correct |
110 |
Correct |
153 ms |
83064 KB |
Output is correct |
111 |
Correct |
193 ms |
80984 KB |
Output is correct |
112 |
Correct |
192 ms |
80724 KB |
Output is correct |
113 |
Correct |
181 ms |
79656 KB |
Output is correct |
114 |
Correct |
332 ms |
77652 KB |
Output is correct |
115 |
Correct |
420 ms |
77560 KB |
Output is correct |
116 |
Correct |
182 ms |
79464 KB |
Output is correct |
117 |
Correct |
181 ms |
78432 KB |
Output is correct |
118 |
Correct |
181 ms |
78172 KB |
Output is correct |
119 |
Correct |
193 ms |
77824 KB |
Output is correct |
120 |
Correct |
170 ms |
78072 KB |
Output is correct |
121 |
Correct |
178 ms |
77688 KB |
Output is correct |
122 |
Correct |
184 ms |
77596 KB |
Output is correct |
123 |
Correct |
183 ms |
78584 KB |
Output is correct |
124 |
Correct |
200 ms |
78176 KB |
Output is correct |
125 |
Correct |
196 ms |
78020 KB |
Output is correct |
126 |
Correct |
202 ms |
78284 KB |
Output is correct |
127 |
Correct |
207 ms |
77956 KB |
Output is correct |
128 |
Correct |
186 ms |
77560 KB |
Output is correct |