#include<bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
using namespace std;
using ll=long long;
template<typename T> void chmax(T &a,T b){a=max(a,b);}
template<typename T> void chmin(T &a,T b){a=min(a,b);}
vector<vector<int>> G;
struct BIT{
vector<int> bit;
BIT(int n){
bit.resize(n+1,0);
}
void add(int k,int x){
for(++k;k<bit.size();k+=k&-k){
bit[k]+=x;
}
}
int sum(int k){
int res=0;
for(++k;k>0;k-=k&-k){
res+=bit[k];
}
return res;
}
};
int main(){
int n;cin>>n;
vector<ll> c(n);
G.resize(n);
vector<int> par(n,-1);
for(int i=0;i<n;i++)cin>>c[i];
for(int i=0;i<n-1;i++){
int a,b;cin>>a>>b;--a;--b;
G[a].push_back(b);
par[b]=a;
vector<int> v,vv;
while(a!=-1){
v.push_back(c[a]);
vv.push_back(c[a]);
c[a]=c[b];
a=par[a];
}
sort(all(vv));
map<int,int> mp;
for(int j=0;j<vv.size();j++){
mp[vv[j]]=j;
}
reverse(all(v));
int ans=0;
BIT bit(vv.size());
for(int j=0;j<v.size();j++){
v[j]=mp[v[j]];
ans+=bit.sum(vv.size()-1)-bit.sum(v[j]);
bit.add(v[j],1);
}
cout<<ans<<endl;
}
}
Compilation message
construction.cpp: In member function 'void BIT::add(int, int)':
construction.cpp:14:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(++k;k<bit.size();k+=k&-k){
~^~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:45:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<vv.size();j++){
~^~~~~~~~~~
construction.cpp:51:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<v.size();j++){
~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
9 ms |
384 KB |
Output is correct |
9 |
Correct |
7 ms |
384 KB |
Output is correct |
10 |
Correct |
8 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
6 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
6 ms |
384 KB |
Output is correct |
21 |
Correct |
6 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
8 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
29 |
Correct |
6 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
256 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
4 ms |
384 KB |
Output is correct |
36 |
Correct |
4 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
5 ms |
384 KB |
Output is correct |
40 |
Correct |
6 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
42 |
Correct |
6 ms |
384 KB |
Output is correct |
43 |
Correct |
5 ms |
384 KB |
Output is correct |
44 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
9 ms |
384 KB |
Output is correct |
9 |
Correct |
7 ms |
384 KB |
Output is correct |
10 |
Correct |
8 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
6 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
6 ms |
384 KB |
Output is correct |
21 |
Correct |
6 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
8 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
29 |
Correct |
6 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
256 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
4 ms |
384 KB |
Output is correct |
36 |
Correct |
4 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
5 ms |
384 KB |
Output is correct |
40 |
Correct |
6 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
42 |
Correct |
6 ms |
384 KB |
Output is correct |
43 |
Correct |
5 ms |
384 KB |
Output is correct |
44 |
Correct |
5 ms |
384 KB |
Output is correct |
45 |
Correct |
7 ms |
384 KB |
Output is correct |
46 |
Correct |
20 ms |
640 KB |
Output is correct |
47 |
Correct |
23 ms |
612 KB |
Output is correct |
48 |
Correct |
18 ms |
676 KB |
Output is correct |
49 |
Correct |
357 ms |
952 KB |
Output is correct |
50 |
Correct |
396 ms |
888 KB |
Output is correct |
51 |
Correct |
348 ms |
776 KB |
Output is correct |
52 |
Correct |
182 ms |
760 KB |
Output is correct |
53 |
Correct |
152 ms |
632 KB |
Output is correct |
54 |
Correct |
214 ms |
780 KB |
Output is correct |
55 |
Correct |
188 ms |
632 KB |
Output is correct |
56 |
Correct |
180 ms |
724 KB |
Output is correct |
57 |
Correct |
17 ms |
576 KB |
Output is correct |
58 |
Correct |
22 ms |
640 KB |
Output is correct |
59 |
Correct |
20 ms |
640 KB |
Output is correct |
60 |
Correct |
21 ms |
640 KB |
Output is correct |
61 |
Correct |
183 ms |
756 KB |
Output is correct |
62 |
Correct |
176 ms |
632 KB |
Output is correct |
63 |
Correct |
213 ms |
740 KB |
Output is correct |
64 |
Correct |
22 ms |
512 KB |
Output is correct |
65 |
Correct |
15 ms |
512 KB |
Output is correct |
66 |
Correct |
24 ms |
640 KB |
Output is correct |
67 |
Correct |
16 ms |
640 KB |
Output is correct |
68 |
Correct |
310 ms |
632 KB |
Output is correct |
69 |
Correct |
164 ms |
680 KB |
Output is correct |
70 |
Correct |
170 ms |
664 KB |
Output is correct |
71 |
Correct |
186 ms |
632 KB |
Output is correct |
72 |
Correct |
18 ms |
640 KB |
Output is correct |
73 |
Correct |
21 ms |
640 KB |
Output is correct |
74 |
Correct |
179 ms |
632 KB |
Output is correct |
75 |
Correct |
101 ms |
632 KB |
Output is correct |
76 |
Correct |
71 ms |
632 KB |
Output is correct |
77 |
Correct |
53 ms |
632 KB |
Output is correct |
78 |
Correct |
93 ms |
512 KB |
Output is correct |
79 |
Correct |
66 ms |
640 KB |
Output is correct |
80 |
Correct |
52 ms |
512 KB |
Output is correct |
81 |
Correct |
97 ms |
636 KB |
Output is correct |
82 |
Correct |
64 ms |
632 KB |
Output is correct |
83 |
Correct |
60 ms |
732 KB |
Output is correct |
84 |
Correct |
102 ms |
632 KB |
Output is correct |
85 |
Correct |
128 ms |
632 KB |
Output is correct |
86 |
Correct |
53 ms |
608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
9 ms |
384 KB |
Output is correct |
9 |
Correct |
7 ms |
384 KB |
Output is correct |
10 |
Correct |
8 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
6 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
6 ms |
384 KB |
Output is correct |
21 |
Correct |
6 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
8 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
29 |
Correct |
6 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
256 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
4 ms |
384 KB |
Output is correct |
36 |
Correct |
4 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
5 ms |
384 KB |
Output is correct |
40 |
Correct |
6 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
42 |
Correct |
6 ms |
384 KB |
Output is correct |
43 |
Correct |
5 ms |
384 KB |
Output is correct |
44 |
Correct |
5 ms |
384 KB |
Output is correct |
45 |
Correct |
7 ms |
384 KB |
Output is correct |
46 |
Correct |
20 ms |
640 KB |
Output is correct |
47 |
Correct |
23 ms |
612 KB |
Output is correct |
48 |
Correct |
18 ms |
676 KB |
Output is correct |
49 |
Correct |
357 ms |
952 KB |
Output is correct |
50 |
Correct |
396 ms |
888 KB |
Output is correct |
51 |
Correct |
348 ms |
776 KB |
Output is correct |
52 |
Correct |
182 ms |
760 KB |
Output is correct |
53 |
Correct |
152 ms |
632 KB |
Output is correct |
54 |
Correct |
214 ms |
780 KB |
Output is correct |
55 |
Correct |
188 ms |
632 KB |
Output is correct |
56 |
Correct |
180 ms |
724 KB |
Output is correct |
57 |
Correct |
17 ms |
576 KB |
Output is correct |
58 |
Correct |
22 ms |
640 KB |
Output is correct |
59 |
Correct |
20 ms |
640 KB |
Output is correct |
60 |
Correct |
21 ms |
640 KB |
Output is correct |
61 |
Correct |
183 ms |
756 KB |
Output is correct |
62 |
Correct |
176 ms |
632 KB |
Output is correct |
63 |
Correct |
213 ms |
740 KB |
Output is correct |
64 |
Correct |
22 ms |
512 KB |
Output is correct |
65 |
Correct |
15 ms |
512 KB |
Output is correct |
66 |
Correct |
24 ms |
640 KB |
Output is correct |
67 |
Correct |
16 ms |
640 KB |
Output is correct |
68 |
Correct |
310 ms |
632 KB |
Output is correct |
69 |
Correct |
164 ms |
680 KB |
Output is correct |
70 |
Correct |
170 ms |
664 KB |
Output is correct |
71 |
Correct |
186 ms |
632 KB |
Output is correct |
72 |
Correct |
18 ms |
640 KB |
Output is correct |
73 |
Correct |
21 ms |
640 KB |
Output is correct |
74 |
Correct |
179 ms |
632 KB |
Output is correct |
75 |
Correct |
101 ms |
632 KB |
Output is correct |
76 |
Correct |
71 ms |
632 KB |
Output is correct |
77 |
Correct |
53 ms |
632 KB |
Output is correct |
78 |
Correct |
93 ms |
512 KB |
Output is correct |
79 |
Correct |
66 ms |
640 KB |
Output is correct |
80 |
Correct |
52 ms |
512 KB |
Output is correct |
81 |
Correct |
97 ms |
636 KB |
Output is correct |
82 |
Correct |
64 ms |
632 KB |
Output is correct |
83 |
Correct |
60 ms |
732 KB |
Output is correct |
84 |
Correct |
102 ms |
632 KB |
Output is correct |
85 |
Correct |
128 ms |
632 KB |
Output is correct |
86 |
Correct |
53 ms |
608 KB |
Output is correct |
87 |
Correct |
55 ms |
1116 KB |
Output is correct |
88 |
Correct |
162 ms |
2552 KB |
Output is correct |
89 |
Correct |
553 ms |
7928 KB |
Output is correct |
90 |
Correct |
590 ms |
7800 KB |
Output is correct |
91 |
Correct |
567 ms |
8056 KB |
Output is correct |
92 |
Execution timed out |
2035 ms |
5716 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |