#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 |
71 ms |
67576 KB |
Output is correct |
2 |
Correct |
72 ms |
67684 KB |
Output is correct |
3 |
Correct |
74 ms |
67736 KB |
Output is correct |
4 |
Correct |
69 ms |
67808 KB |
Output is correct |
5 |
Correct |
76 ms |
68100 KB |
Output is correct |
6 |
Correct |
71 ms |
68100 KB |
Output is correct |
7 |
Correct |
75 ms |
68100 KB |
Output is correct |
8 |
Correct |
70 ms |
68100 KB |
Output is correct |
9 |
Correct |
69 ms |
68100 KB |
Output is correct |
10 |
Correct |
69 ms |
68100 KB |
Output is correct |
11 |
Correct |
71 ms |
68100 KB |
Output is correct |
12 |
Correct |
67 ms |
68100 KB |
Output is correct |
13 |
Correct |
73 ms |
68100 KB |
Output is correct |
14 |
Correct |
134 ms |
68100 KB |
Output is correct |
15 |
Correct |
71 ms |
68100 KB |
Output is correct |
16 |
Correct |
72 ms |
68100 KB |
Output is correct |
17 |
Correct |
74 ms |
68100 KB |
Output is correct |
18 |
Correct |
70 ms |
68100 KB |
Output is correct |
19 |
Correct |
60 ms |
68100 KB |
Output is correct |
20 |
Correct |
62 ms |
68100 KB |
Output is correct |
21 |
Correct |
70 ms |
68100 KB |
Output is correct |
22 |
Correct |
70 ms |
68100 KB |
Output is correct |
23 |
Correct |
72 ms |
68100 KB |
Output is correct |
24 |
Correct |
69 ms |
68100 KB |
Output is correct |
25 |
Correct |
60 ms |
68100 KB |
Output is correct |
26 |
Correct |
64 ms |
68100 KB |
Output is correct |
27 |
Correct |
64 ms |
68100 KB |
Output is correct |
28 |
Correct |
63 ms |
68100 KB |
Output is correct |
29 |
Correct |
63 ms |
68100 KB |
Output is correct |
30 |
Correct |
77 ms |
68100 KB |
Output is correct |
31 |
Correct |
71 ms |
68100 KB |
Output is correct |
32 |
Correct |
69 ms |
68100 KB |
Output is correct |
33 |
Correct |
61 ms |
68100 KB |
Output is correct |
34 |
Correct |
73 ms |
68100 KB |
Output is correct |
35 |
Correct |
61 ms |
68120 KB |
Output is correct |
36 |
Correct |
61 ms |
68120 KB |
Output is correct |
37 |
Correct |
62 ms |
68120 KB |
Output is correct |
38 |
Correct |
73 ms |
68120 KB |
Output is correct |
39 |
Correct |
74 ms |
68120 KB |
Output is correct |
40 |
Correct |
69 ms |
68120 KB |
Output is correct |
41 |
Correct |
70 ms |
68120 KB |
Output is correct |
42 |
Correct |
67 ms |
68120 KB |
Output is correct |
43 |
Correct |
82 ms |
68120 KB |
Output is correct |
44 |
Correct |
70 ms |
68120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
67576 KB |
Output is correct |
2 |
Correct |
72 ms |
67684 KB |
Output is correct |
3 |
Correct |
74 ms |
67736 KB |
Output is correct |
4 |
Correct |
69 ms |
67808 KB |
Output is correct |
5 |
Correct |
76 ms |
68100 KB |
Output is correct |
6 |
Correct |
71 ms |
68100 KB |
Output is correct |
7 |
Correct |
75 ms |
68100 KB |
Output is correct |
8 |
Correct |
70 ms |
68100 KB |
Output is correct |
9 |
Correct |
69 ms |
68100 KB |
Output is correct |
10 |
Correct |
69 ms |
68100 KB |
Output is correct |
11 |
Correct |
71 ms |
68100 KB |
Output is correct |
12 |
Correct |
67 ms |
68100 KB |
Output is correct |
13 |
Correct |
73 ms |
68100 KB |
Output is correct |
14 |
Correct |
134 ms |
68100 KB |
Output is correct |
15 |
Correct |
71 ms |
68100 KB |
Output is correct |
16 |
Correct |
72 ms |
68100 KB |
Output is correct |
17 |
Correct |
74 ms |
68100 KB |
Output is correct |
18 |
Correct |
70 ms |
68100 KB |
Output is correct |
19 |
Correct |
60 ms |
68100 KB |
Output is correct |
20 |
Correct |
62 ms |
68100 KB |
Output is correct |
21 |
Correct |
70 ms |
68100 KB |
Output is correct |
22 |
Correct |
70 ms |
68100 KB |
Output is correct |
23 |
Correct |
72 ms |
68100 KB |
Output is correct |
24 |
Correct |
69 ms |
68100 KB |
Output is correct |
25 |
Correct |
60 ms |
68100 KB |
Output is correct |
26 |
Correct |
64 ms |
68100 KB |
Output is correct |
27 |
Correct |
64 ms |
68100 KB |
Output is correct |
28 |
Correct |
63 ms |
68100 KB |
Output is correct |
29 |
Correct |
63 ms |
68100 KB |
Output is correct |
30 |
Correct |
77 ms |
68100 KB |
Output is correct |
31 |
Correct |
71 ms |
68100 KB |
Output is correct |
32 |
Correct |
69 ms |
68100 KB |
Output is correct |
33 |
Correct |
61 ms |
68100 KB |
Output is correct |
34 |
Correct |
73 ms |
68100 KB |
Output is correct |
35 |
Correct |
61 ms |
68120 KB |
Output is correct |
36 |
Correct |
61 ms |
68120 KB |
Output is correct |
37 |
Correct |
62 ms |
68120 KB |
Output is correct |
38 |
Correct |
73 ms |
68120 KB |
Output is correct |
39 |
Correct |
74 ms |
68120 KB |
Output is correct |
40 |
Correct |
69 ms |
68120 KB |
Output is correct |
41 |
Correct |
70 ms |
68120 KB |
Output is correct |
42 |
Correct |
67 ms |
68120 KB |
Output is correct |
43 |
Correct |
82 ms |
68120 KB |
Output is correct |
44 |
Correct |
70 ms |
68120 KB |
Output is correct |
45 |
Correct |
73 ms |
68120 KB |
Output is correct |
46 |
Correct |
86 ms |
68288 KB |
Output is correct |
47 |
Correct |
104 ms |
68288 KB |
Output is correct |
48 |
Correct |
82 ms |
68288 KB |
Output is correct |
49 |
Correct |
84 ms |
68440 KB |
Output is correct |
50 |
Correct |
102 ms |
68508 KB |
Output is correct |
51 |
Correct |
71 ms |
68508 KB |
Output is correct |
52 |
Correct |
76 ms |
68508 KB |
Output is correct |
53 |
Correct |
79 ms |
68508 KB |
Output is correct |
54 |
Correct |
89 ms |
68508 KB |
Output is correct |
55 |
Correct |
91 ms |
68508 KB |
Output is correct |
56 |
Correct |
117 ms |
68508 KB |
Output is correct |
57 |
Correct |
101 ms |
68508 KB |
Output is correct |
58 |
Correct |
87 ms |
68508 KB |
Output is correct |
59 |
Correct |
76 ms |
68508 KB |
Output is correct |
60 |
Correct |
76 ms |
68508 KB |
Output is correct |
61 |
Correct |
71 ms |
68508 KB |
Output is correct |
62 |
Correct |
82 ms |
68540 KB |
Output is correct |
63 |
Correct |
74 ms |
68540 KB |
Output is correct |
64 |
Correct |
109 ms |
68540 KB |
Output is correct |
65 |
Correct |
85 ms |
68540 KB |
Output is correct |
66 |
Correct |
86 ms |
68540 KB |
Output is correct |
67 |
Correct |
80 ms |
68540 KB |
Output is correct |
68 |
Correct |
73 ms |
68592 KB |
Output is correct |
69 |
Correct |
78 ms |
68592 KB |
Output is correct |
70 |
Correct |
79 ms |
68592 KB |
Output is correct |
71 |
Correct |
76 ms |
68592 KB |
Output is correct |
72 |
Correct |
92 ms |
68592 KB |
Output is correct |
73 |
Correct |
96 ms |
68592 KB |
Output is correct |
74 |
Correct |
84 ms |
68592 KB |
Output is correct |
75 |
Correct |
74 ms |
68592 KB |
Output is correct |
76 |
Correct |
79 ms |
68592 KB |
Output is correct |
77 |
Correct |
81 ms |
68592 KB |
Output is correct |
78 |
Correct |
88 ms |
68592 KB |
Output is correct |
79 |
Correct |
70 ms |
68592 KB |
Output is correct |
80 |
Correct |
71 ms |
68592 KB |
Output is correct |
81 |
Correct |
72 ms |
68592 KB |
Output is correct |
82 |
Correct |
85 ms |
68592 KB |
Output is correct |
83 |
Correct |
108 ms |
68592 KB |
Output is correct |
84 |
Correct |
77 ms |
68592 KB |
Output is correct |
85 |
Correct |
89 ms |
68592 KB |
Output is correct |
86 |
Correct |
89 ms |
68592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
67576 KB |
Output is correct |
2 |
Correct |
72 ms |
67684 KB |
Output is correct |
3 |
Correct |
74 ms |
67736 KB |
Output is correct |
4 |
Correct |
69 ms |
67808 KB |
Output is correct |
5 |
Correct |
76 ms |
68100 KB |
Output is correct |
6 |
Correct |
71 ms |
68100 KB |
Output is correct |
7 |
Correct |
75 ms |
68100 KB |
Output is correct |
8 |
Correct |
70 ms |
68100 KB |
Output is correct |
9 |
Correct |
69 ms |
68100 KB |
Output is correct |
10 |
Correct |
69 ms |
68100 KB |
Output is correct |
11 |
Correct |
71 ms |
68100 KB |
Output is correct |
12 |
Correct |
67 ms |
68100 KB |
Output is correct |
13 |
Correct |
73 ms |
68100 KB |
Output is correct |
14 |
Correct |
134 ms |
68100 KB |
Output is correct |
15 |
Correct |
71 ms |
68100 KB |
Output is correct |
16 |
Correct |
72 ms |
68100 KB |
Output is correct |
17 |
Correct |
74 ms |
68100 KB |
Output is correct |
18 |
Correct |
70 ms |
68100 KB |
Output is correct |
19 |
Correct |
60 ms |
68100 KB |
Output is correct |
20 |
Correct |
62 ms |
68100 KB |
Output is correct |
21 |
Correct |
70 ms |
68100 KB |
Output is correct |
22 |
Correct |
70 ms |
68100 KB |
Output is correct |
23 |
Correct |
72 ms |
68100 KB |
Output is correct |
24 |
Correct |
69 ms |
68100 KB |
Output is correct |
25 |
Correct |
60 ms |
68100 KB |
Output is correct |
26 |
Correct |
64 ms |
68100 KB |
Output is correct |
27 |
Correct |
64 ms |
68100 KB |
Output is correct |
28 |
Correct |
63 ms |
68100 KB |
Output is correct |
29 |
Correct |
63 ms |
68100 KB |
Output is correct |
30 |
Correct |
77 ms |
68100 KB |
Output is correct |
31 |
Correct |
71 ms |
68100 KB |
Output is correct |
32 |
Correct |
69 ms |
68100 KB |
Output is correct |
33 |
Correct |
61 ms |
68100 KB |
Output is correct |
34 |
Correct |
73 ms |
68100 KB |
Output is correct |
35 |
Correct |
61 ms |
68120 KB |
Output is correct |
36 |
Correct |
61 ms |
68120 KB |
Output is correct |
37 |
Correct |
62 ms |
68120 KB |
Output is correct |
38 |
Correct |
73 ms |
68120 KB |
Output is correct |
39 |
Correct |
74 ms |
68120 KB |
Output is correct |
40 |
Correct |
69 ms |
68120 KB |
Output is correct |
41 |
Correct |
70 ms |
68120 KB |
Output is correct |
42 |
Correct |
67 ms |
68120 KB |
Output is correct |
43 |
Correct |
82 ms |
68120 KB |
Output is correct |
44 |
Correct |
70 ms |
68120 KB |
Output is correct |
45 |
Correct |
73 ms |
68120 KB |
Output is correct |
46 |
Correct |
86 ms |
68288 KB |
Output is correct |
47 |
Correct |
104 ms |
68288 KB |
Output is correct |
48 |
Correct |
82 ms |
68288 KB |
Output is correct |
49 |
Correct |
84 ms |
68440 KB |
Output is correct |
50 |
Correct |
102 ms |
68508 KB |
Output is correct |
51 |
Correct |
71 ms |
68508 KB |
Output is correct |
52 |
Correct |
76 ms |
68508 KB |
Output is correct |
53 |
Correct |
79 ms |
68508 KB |
Output is correct |
54 |
Correct |
89 ms |
68508 KB |
Output is correct |
55 |
Correct |
91 ms |
68508 KB |
Output is correct |
56 |
Correct |
117 ms |
68508 KB |
Output is correct |
57 |
Correct |
101 ms |
68508 KB |
Output is correct |
58 |
Correct |
87 ms |
68508 KB |
Output is correct |
59 |
Correct |
76 ms |
68508 KB |
Output is correct |
60 |
Correct |
76 ms |
68508 KB |
Output is correct |
61 |
Correct |
71 ms |
68508 KB |
Output is correct |
62 |
Correct |
82 ms |
68540 KB |
Output is correct |
63 |
Correct |
74 ms |
68540 KB |
Output is correct |
64 |
Correct |
109 ms |
68540 KB |
Output is correct |
65 |
Correct |
85 ms |
68540 KB |
Output is correct |
66 |
Correct |
86 ms |
68540 KB |
Output is correct |
67 |
Correct |
80 ms |
68540 KB |
Output is correct |
68 |
Correct |
73 ms |
68592 KB |
Output is correct |
69 |
Correct |
78 ms |
68592 KB |
Output is correct |
70 |
Correct |
79 ms |
68592 KB |
Output is correct |
71 |
Correct |
76 ms |
68592 KB |
Output is correct |
72 |
Correct |
92 ms |
68592 KB |
Output is correct |
73 |
Correct |
96 ms |
68592 KB |
Output is correct |
74 |
Correct |
84 ms |
68592 KB |
Output is correct |
75 |
Correct |
74 ms |
68592 KB |
Output is correct |
76 |
Correct |
79 ms |
68592 KB |
Output is correct |
77 |
Correct |
81 ms |
68592 KB |
Output is correct |
78 |
Correct |
88 ms |
68592 KB |
Output is correct |
79 |
Correct |
70 ms |
68592 KB |
Output is correct |
80 |
Correct |
71 ms |
68592 KB |
Output is correct |
81 |
Correct |
72 ms |
68592 KB |
Output is correct |
82 |
Correct |
85 ms |
68592 KB |
Output is correct |
83 |
Correct |
108 ms |
68592 KB |
Output is correct |
84 |
Correct |
77 ms |
68592 KB |
Output is correct |
85 |
Correct |
89 ms |
68592 KB |
Output is correct |
86 |
Correct |
89 ms |
68592 KB |
Output is correct |
87 |
Correct |
88 ms |
68796 KB |
Output is correct |
88 |
Correct |
159 ms |
70480 KB |
Output is correct |
89 |
Correct |
467 ms |
76092 KB |
Output is correct |
90 |
Correct |
566 ms |
76092 KB |
Output is correct |
91 |
Correct |
407 ms |
76092 KB |
Output is correct |
92 |
Correct |
231 ms |
81956 KB |
Output is correct |
93 |
Correct |
233 ms |
81956 KB |
Output is correct |
94 |
Correct |
230 ms |
81956 KB |
Output is correct |
95 |
Correct |
245 ms |
81956 KB |
Output is correct |
96 |
Correct |
244 ms |
81956 KB |
Output is correct |
97 |
Correct |
295 ms |
81956 KB |
Output is correct |
98 |
Correct |
259 ms |
81956 KB |
Output is correct |
99 |
Correct |
235 ms |
81956 KB |
Output is correct |
100 |
Correct |
642 ms |
81956 KB |
Output is correct |
101 |
Correct |
750 ms |
81956 KB |
Output is correct |
102 |
Correct |
705 ms |
81956 KB |
Output is correct |
103 |
Correct |
591 ms |
81956 KB |
Output is correct |
104 |
Correct |
240 ms |
81956 KB |
Output is correct |
105 |
Correct |
196 ms |
81956 KB |
Output is correct |
106 |
Correct |
244 ms |
81956 KB |
Output is correct |
107 |
Correct |
459 ms |
81956 KB |
Output is correct |
108 |
Correct |
421 ms |
81956 KB |
Output is correct |
109 |
Correct |
436 ms |
81956 KB |
Output is correct |
110 |
Correct |
224 ms |
81956 KB |
Output is correct |
111 |
Correct |
241 ms |
81956 KB |
Output is correct |
112 |
Correct |
211 ms |
81956 KB |
Output is correct |
113 |
Correct |
225 ms |
81956 KB |
Output is correct |
114 |
Correct |
546 ms |
81956 KB |
Output is correct |
115 |
Correct |
574 ms |
81956 KB |
Output is correct |
116 |
Correct |
226 ms |
81956 KB |
Output is correct |
117 |
Correct |
263 ms |
81956 KB |
Output is correct |
118 |
Correct |
296 ms |
81956 KB |
Output is correct |
119 |
Correct |
237 ms |
81956 KB |
Output is correct |
120 |
Correct |
270 ms |
81956 KB |
Output is correct |
121 |
Correct |
231 ms |
81956 KB |
Output is correct |
122 |
Correct |
304 ms |
81956 KB |
Output is correct |
123 |
Correct |
221 ms |
81956 KB |
Output is correct |
124 |
Correct |
281 ms |
81956 KB |
Output is correct |
125 |
Correct |
236 ms |
81956 KB |
Output is correct |
126 |
Correct |
240 ms |
81956 KB |
Output is correct |
127 |
Correct |
217 ms |
81956 KB |
Output is correct |
128 |
Correct |
245 ms |
81956 KB |
Output is correct |