#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long int lld;
int FT[10000];
int sz;
void init(int n){sz=n;
for(int i=0;i<=n;i++)FT[i]=0;
}
void update(int pos){
pos++;
for(;pos<=sz;pos+=((pos)&(-pos))){
FT[pos]++;
}
}
int query(int pos){
pos++;
int ans=0;
for(;pos>0;pos-=((pos)&(-pos))){
ans+=FT[pos];
}return ans;
}
void print(){
for(int i=0;i<sz;i++)cout<<FT[i]<<" ";
cout<<endl;
}
lld analyse(vector<int> v){
int n=v.size();
pair<int,int> arr[n];
for(int i=0;i<n;i++){//cout<<v[i]<<" ";
arr[i].first=v[i];
arr[i].second=i;
}//cout<<endl;
sort(arr,arr+n);
int prev=0;
init(n);
lld ans=0;
for(int i=0;i<=n;i++){
if(i==n ||(i>0 && arr[i].first>arr[i-1].first)){
for(int j=prev;j<i;j++){
ans+=query(arr[j].second);
}
for(int j=prev;j<i;j++){
update(arr[j].second);
}
prev=i;
}
}//print();
return ans;
}
int main(){
int n;
cin>>n;
int C[n];
for(int i=0;i<n;i++){
cin>>C[i];
}
int parent[n];
parent[0]=0;
for(int i=1;i<n;i++){
int x,y;
cin>>x>>y;
x--;
y--;
parent[y]=x;
vector<int> v;
int pnt=y;
while(pnt!=0){
pnt=parent[pnt];
v.push_back(C[pnt]);
C[pnt]=C[y];
}
cout<<analyse(v)<<endl;
}
/*for(int i=0;i<n;i++){
cout<<C[i]<<endl;
}*/
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
3 ms |
420 KB |
Output is correct |
5 |
Correct |
4 ms |
428 KB |
Output is correct |
6 |
Correct |
5 ms |
612 KB |
Output is correct |
7 |
Correct |
4 ms |
616 KB |
Output is correct |
8 |
Correct |
9 ms |
644 KB |
Output is correct |
9 |
Correct |
11 ms |
784 KB |
Output is correct |
10 |
Correct |
10 ms |
852 KB |
Output is correct |
11 |
Correct |
7 ms |
864 KB |
Output is correct |
12 |
Correct |
8 ms |
992 KB |
Output is correct |
13 |
Correct |
8 ms |
1000 KB |
Output is correct |
14 |
Correct |
6 ms |
1012 KB |
Output is correct |
15 |
Correct |
5 ms |
1012 KB |
Output is correct |
16 |
Correct |
5 ms |
1032 KB |
Output is correct |
17 |
Correct |
5 ms |
1040 KB |
Output is correct |
18 |
Correct |
4 ms |
1172 KB |
Output is correct |
19 |
Correct |
8 ms |
1204 KB |
Output is correct |
20 |
Correct |
7 ms |
1204 KB |
Output is correct |
21 |
Correct |
6 ms |
1204 KB |
Output is correct |
22 |
Correct |
5 ms |
1204 KB |
Output is correct |
23 |
Correct |
4 ms |
1204 KB |
Output is correct |
24 |
Correct |
5 ms |
1204 KB |
Output is correct |
25 |
Correct |
4 ms |
1204 KB |
Output is correct |
26 |
Correct |
10 ms |
1204 KB |
Output is correct |
27 |
Correct |
7 ms |
1204 KB |
Output is correct |
28 |
Correct |
7 ms |
1204 KB |
Output is correct |
29 |
Correct |
8 ms |
1204 KB |
Output is correct |
30 |
Correct |
4 ms |
1204 KB |
Output is correct |
31 |
Correct |
4 ms |
1204 KB |
Output is correct |
32 |
Correct |
7 ms |
1204 KB |
Output is correct |
33 |
Correct |
6 ms |
1204 KB |
Output is correct |
34 |
Correct |
6 ms |
1216 KB |
Output is correct |
35 |
Correct |
4 ms |
1228 KB |
Output is correct |
36 |
Correct |
8 ms |
1236 KB |
Output is correct |
37 |
Correct |
5 ms |
1236 KB |
Output is correct |
38 |
Correct |
5 ms |
1236 KB |
Output is correct |
39 |
Correct |
6 ms |
1260 KB |
Output is correct |
40 |
Correct |
6 ms |
1268 KB |
Output is correct |
41 |
Correct |
5 ms |
1268 KB |
Output is correct |
42 |
Correct |
6 ms |
1284 KB |
Output is correct |
43 |
Correct |
7 ms |
1284 KB |
Output is correct |
44 |
Correct |
5 ms |
1284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
3 ms |
420 KB |
Output is correct |
5 |
Correct |
4 ms |
428 KB |
Output is correct |
6 |
Correct |
5 ms |
612 KB |
Output is correct |
7 |
Correct |
4 ms |
616 KB |
Output is correct |
8 |
Correct |
9 ms |
644 KB |
Output is correct |
9 |
Correct |
11 ms |
784 KB |
Output is correct |
10 |
Correct |
10 ms |
852 KB |
Output is correct |
11 |
Correct |
7 ms |
864 KB |
Output is correct |
12 |
Correct |
8 ms |
992 KB |
Output is correct |
13 |
Correct |
8 ms |
1000 KB |
Output is correct |
14 |
Correct |
6 ms |
1012 KB |
Output is correct |
15 |
Correct |
5 ms |
1012 KB |
Output is correct |
16 |
Correct |
5 ms |
1032 KB |
Output is correct |
17 |
Correct |
5 ms |
1040 KB |
Output is correct |
18 |
Correct |
4 ms |
1172 KB |
Output is correct |
19 |
Correct |
8 ms |
1204 KB |
Output is correct |
20 |
Correct |
7 ms |
1204 KB |
Output is correct |
21 |
Correct |
6 ms |
1204 KB |
Output is correct |
22 |
Correct |
5 ms |
1204 KB |
Output is correct |
23 |
Correct |
4 ms |
1204 KB |
Output is correct |
24 |
Correct |
5 ms |
1204 KB |
Output is correct |
25 |
Correct |
4 ms |
1204 KB |
Output is correct |
26 |
Correct |
10 ms |
1204 KB |
Output is correct |
27 |
Correct |
7 ms |
1204 KB |
Output is correct |
28 |
Correct |
7 ms |
1204 KB |
Output is correct |
29 |
Correct |
8 ms |
1204 KB |
Output is correct |
30 |
Correct |
4 ms |
1204 KB |
Output is correct |
31 |
Correct |
4 ms |
1204 KB |
Output is correct |
32 |
Correct |
7 ms |
1204 KB |
Output is correct |
33 |
Correct |
6 ms |
1204 KB |
Output is correct |
34 |
Correct |
6 ms |
1216 KB |
Output is correct |
35 |
Correct |
4 ms |
1228 KB |
Output is correct |
36 |
Correct |
8 ms |
1236 KB |
Output is correct |
37 |
Correct |
5 ms |
1236 KB |
Output is correct |
38 |
Correct |
5 ms |
1236 KB |
Output is correct |
39 |
Correct |
6 ms |
1260 KB |
Output is correct |
40 |
Correct |
6 ms |
1268 KB |
Output is correct |
41 |
Correct |
5 ms |
1268 KB |
Output is correct |
42 |
Correct |
6 ms |
1284 KB |
Output is correct |
43 |
Correct |
7 ms |
1284 KB |
Output is correct |
44 |
Correct |
5 ms |
1284 KB |
Output is correct |
45 |
Correct |
6 ms |
1308 KB |
Output is correct |
46 |
Correct |
21 ms |
1456 KB |
Output is correct |
47 |
Correct |
18 ms |
1456 KB |
Output is correct |
48 |
Correct |
19 ms |
1480 KB |
Output is correct |
49 |
Correct |
515 ms |
1812 KB |
Output is correct |
50 |
Correct |
389 ms |
1892 KB |
Output is correct |
51 |
Correct |
426 ms |
1928 KB |
Output is correct |
52 |
Correct |
211 ms |
1928 KB |
Output is correct |
53 |
Correct |
217 ms |
1968 KB |
Output is correct |
54 |
Correct |
216 ms |
2176 KB |
Output is correct |
55 |
Correct |
191 ms |
2248 KB |
Output is correct |
56 |
Correct |
240 ms |
2324 KB |
Output is correct |
57 |
Correct |
17 ms |
2324 KB |
Output is correct |
58 |
Correct |
20 ms |
2324 KB |
Output is correct |
59 |
Correct |
17 ms |
2324 KB |
Output is correct |
60 |
Correct |
18 ms |
2352 KB |
Output is correct |
61 |
Correct |
250 ms |
2688 KB |
Output is correct |
62 |
Correct |
279 ms |
2688 KB |
Output is correct |
63 |
Correct |
226 ms |
2712 KB |
Output is correct |
64 |
Correct |
17 ms |
2784 KB |
Output is correct |
65 |
Correct |
22 ms |
2784 KB |
Output is correct |
66 |
Correct |
15 ms |
2880 KB |
Output is correct |
67 |
Correct |
15 ms |
2880 KB |
Output is correct |
68 |
Correct |
409 ms |
3248 KB |
Output is correct |
69 |
Correct |
248 ms |
3248 KB |
Output is correct |
70 |
Correct |
192 ms |
3248 KB |
Output is correct |
71 |
Correct |
186 ms |
3248 KB |
Output is correct |
72 |
Correct |
15 ms |
3248 KB |
Output is correct |
73 |
Correct |
18 ms |
3248 KB |
Output is correct |
74 |
Correct |
212 ms |
3296 KB |
Output is correct |
75 |
Correct |
91 ms |
3296 KB |
Output is correct |
76 |
Correct |
68 ms |
3296 KB |
Output is correct |
77 |
Correct |
53 ms |
3440 KB |
Output is correct |
78 |
Correct |
93 ms |
3440 KB |
Output is correct |
79 |
Correct |
69 ms |
3584 KB |
Output is correct |
80 |
Correct |
57 ms |
3584 KB |
Output is correct |
81 |
Correct |
127 ms |
3656 KB |
Output is correct |
82 |
Correct |
93 ms |
3712 KB |
Output is correct |
83 |
Correct |
104 ms |
3712 KB |
Output is correct |
84 |
Correct |
124 ms |
3872 KB |
Output is correct |
85 |
Correct |
97 ms |
3872 KB |
Output is correct |
86 |
Correct |
79 ms |
3872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
3 ms |
420 KB |
Output is correct |
5 |
Correct |
4 ms |
428 KB |
Output is correct |
6 |
Correct |
5 ms |
612 KB |
Output is correct |
7 |
Correct |
4 ms |
616 KB |
Output is correct |
8 |
Correct |
9 ms |
644 KB |
Output is correct |
9 |
Correct |
11 ms |
784 KB |
Output is correct |
10 |
Correct |
10 ms |
852 KB |
Output is correct |
11 |
Correct |
7 ms |
864 KB |
Output is correct |
12 |
Correct |
8 ms |
992 KB |
Output is correct |
13 |
Correct |
8 ms |
1000 KB |
Output is correct |
14 |
Correct |
6 ms |
1012 KB |
Output is correct |
15 |
Correct |
5 ms |
1012 KB |
Output is correct |
16 |
Correct |
5 ms |
1032 KB |
Output is correct |
17 |
Correct |
5 ms |
1040 KB |
Output is correct |
18 |
Correct |
4 ms |
1172 KB |
Output is correct |
19 |
Correct |
8 ms |
1204 KB |
Output is correct |
20 |
Correct |
7 ms |
1204 KB |
Output is correct |
21 |
Correct |
6 ms |
1204 KB |
Output is correct |
22 |
Correct |
5 ms |
1204 KB |
Output is correct |
23 |
Correct |
4 ms |
1204 KB |
Output is correct |
24 |
Correct |
5 ms |
1204 KB |
Output is correct |
25 |
Correct |
4 ms |
1204 KB |
Output is correct |
26 |
Correct |
10 ms |
1204 KB |
Output is correct |
27 |
Correct |
7 ms |
1204 KB |
Output is correct |
28 |
Correct |
7 ms |
1204 KB |
Output is correct |
29 |
Correct |
8 ms |
1204 KB |
Output is correct |
30 |
Correct |
4 ms |
1204 KB |
Output is correct |
31 |
Correct |
4 ms |
1204 KB |
Output is correct |
32 |
Correct |
7 ms |
1204 KB |
Output is correct |
33 |
Correct |
6 ms |
1204 KB |
Output is correct |
34 |
Correct |
6 ms |
1216 KB |
Output is correct |
35 |
Correct |
4 ms |
1228 KB |
Output is correct |
36 |
Correct |
8 ms |
1236 KB |
Output is correct |
37 |
Correct |
5 ms |
1236 KB |
Output is correct |
38 |
Correct |
5 ms |
1236 KB |
Output is correct |
39 |
Correct |
6 ms |
1260 KB |
Output is correct |
40 |
Correct |
6 ms |
1268 KB |
Output is correct |
41 |
Correct |
5 ms |
1268 KB |
Output is correct |
42 |
Correct |
6 ms |
1284 KB |
Output is correct |
43 |
Correct |
7 ms |
1284 KB |
Output is correct |
44 |
Correct |
5 ms |
1284 KB |
Output is correct |
45 |
Correct |
6 ms |
1308 KB |
Output is correct |
46 |
Correct |
21 ms |
1456 KB |
Output is correct |
47 |
Correct |
18 ms |
1456 KB |
Output is correct |
48 |
Correct |
19 ms |
1480 KB |
Output is correct |
49 |
Correct |
515 ms |
1812 KB |
Output is correct |
50 |
Correct |
389 ms |
1892 KB |
Output is correct |
51 |
Correct |
426 ms |
1928 KB |
Output is correct |
52 |
Correct |
211 ms |
1928 KB |
Output is correct |
53 |
Correct |
217 ms |
1968 KB |
Output is correct |
54 |
Correct |
216 ms |
2176 KB |
Output is correct |
55 |
Correct |
191 ms |
2248 KB |
Output is correct |
56 |
Correct |
240 ms |
2324 KB |
Output is correct |
57 |
Correct |
17 ms |
2324 KB |
Output is correct |
58 |
Correct |
20 ms |
2324 KB |
Output is correct |
59 |
Correct |
17 ms |
2324 KB |
Output is correct |
60 |
Correct |
18 ms |
2352 KB |
Output is correct |
61 |
Correct |
250 ms |
2688 KB |
Output is correct |
62 |
Correct |
279 ms |
2688 KB |
Output is correct |
63 |
Correct |
226 ms |
2712 KB |
Output is correct |
64 |
Correct |
17 ms |
2784 KB |
Output is correct |
65 |
Correct |
22 ms |
2784 KB |
Output is correct |
66 |
Correct |
15 ms |
2880 KB |
Output is correct |
67 |
Correct |
15 ms |
2880 KB |
Output is correct |
68 |
Correct |
409 ms |
3248 KB |
Output is correct |
69 |
Correct |
248 ms |
3248 KB |
Output is correct |
70 |
Correct |
192 ms |
3248 KB |
Output is correct |
71 |
Correct |
186 ms |
3248 KB |
Output is correct |
72 |
Correct |
15 ms |
3248 KB |
Output is correct |
73 |
Correct |
18 ms |
3248 KB |
Output is correct |
74 |
Correct |
212 ms |
3296 KB |
Output is correct |
75 |
Correct |
91 ms |
3296 KB |
Output is correct |
76 |
Correct |
68 ms |
3296 KB |
Output is correct |
77 |
Correct |
53 ms |
3440 KB |
Output is correct |
78 |
Correct |
93 ms |
3440 KB |
Output is correct |
79 |
Correct |
69 ms |
3584 KB |
Output is correct |
80 |
Correct |
57 ms |
3584 KB |
Output is correct |
81 |
Correct |
127 ms |
3656 KB |
Output is correct |
82 |
Correct |
93 ms |
3712 KB |
Output is correct |
83 |
Correct |
104 ms |
3712 KB |
Output is correct |
84 |
Correct |
124 ms |
3872 KB |
Output is correct |
85 |
Correct |
97 ms |
3872 KB |
Output is correct |
86 |
Correct |
79 ms |
3872 KB |
Output is correct |
87 |
Correct |
45 ms |
4272 KB |
Output is correct |
88 |
Correct |
137 ms |
5100 KB |
Output is correct |
89 |
Correct |
486 ms |
7788 KB |
Output is correct |
90 |
Correct |
496 ms |
10128 KB |
Output is correct |
91 |
Correct |
459 ms |
12348 KB |
Output is correct |
92 |
Execution timed out |
2066 ms |
13500 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |