#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool com(pair<int,int> a, pair<int,int> b){
return min(a.first,a.second)<min(b.first,b.second);
}
vector<int> tree;
vector<int> ind;
void build(int l, int r, int p){
if(l == r)ind[l] = p;
else{
build(l,(l+r)/2,p*2);
build((l+r)/2+1,r,p*2+1);
}
}
void add(int r, int p, int lb, int rb){
if(rb <= r){tree[p]++;return;}
if(lb > r)return;
add(r,p*2,lb,(rb+lb)/2);
add(r,p*2+1,(rb+lb)/2+1,rb);
}
int check(int p){
if(p == 0)return 0;
return check(p/2)+tree[p];
}
ll count_swaps(vector<int> s) {
int n = s.size()/2;
tree.resize(8*n);
ind.resize(n*2+1);
build(0,n*2-1,1);
vector<vector<pair<int,int>>> shoe(n*2+1);
vector<pair<int,int>> seg;
vector<int> num(n*2+1,0);
for(int i = 0; i < n*2; ++i){
if(num[abs(s[i])] == (int)shoe[abs(s[i])].size()){
shoe[abs(s[i])].push_back({s[i],i});
continue;
}
if(shoe[abs(s[i])][num[abs(s[i])]].first == s[i]){
shoe[abs(s[i])].push_back({s[i],i});
continue;
}
if(s[i] > 0)seg.push_back({i,shoe[abs(s[i])][num[abs(s[i])]].second});
else seg.push_back({shoe[abs(s[i])][num[abs(s[i])]].second,i});
num[abs(s[i])]++;
}
sort(seg.begin(),seg.end(),com);
ll ans = 0;
for(int i = 0; i < (int)seg.size(); ++i){
int x = max(seg[i].first,seg[i].second);
int y = x+check(ind[x]);
add(x,1,0,n*2-1);
ans+=y-(i*2+1);
ans+=seg[i].first < seg[i].second;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
256 KB |
Output is correct |
21 |
Correct |
1 ms |
256 KB |
Output is correct |
22 |
Correct |
1 ms |
256 KB |
Output is correct |
23 |
Correct |
0 ms |
256 KB |
Output is correct |
24 |
Correct |
1 ms |
256 KB |
Output is correct |
25 |
Correct |
0 ms |
256 KB |
Output is correct |
26 |
Correct |
1 ms |
256 KB |
Output is correct |
27 |
Correct |
1 ms |
256 KB |
Output is correct |
28 |
Correct |
0 ms |
256 KB |
Output is correct |
29 |
Correct |
1 ms |
256 KB |
Output is correct |
30 |
Correct |
1 ms |
256 KB |
Output is correct |
31 |
Correct |
1 ms |
256 KB |
Output is correct |
32 |
Correct |
1 ms |
256 KB |
Output is correct |
33 |
Correct |
0 ms |
256 KB |
Output is correct |
34 |
Correct |
1 ms |
256 KB |
Output is correct |
35 |
Correct |
1 ms |
256 KB |
Output is correct |
36 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
6 ms |
1792 KB |
Output is correct |
21 |
Correct |
6 ms |
1792 KB |
Output is correct |
22 |
Correct |
64 ms |
14316 KB |
Output is correct |
23 |
Correct |
64 ms |
14316 KB |
Output is correct |
24 |
Correct |
67 ms |
14316 KB |
Output is correct |
25 |
Correct |
57 ms |
14700 KB |
Output is correct |
26 |
Correct |
65 ms |
14316 KB |
Output is correct |
27 |
Correct |
61 ms |
14180 KB |
Output is correct |
28 |
Correct |
65 ms |
14828 KB |
Output is correct |
29 |
Correct |
62 ms |
14828 KB |
Output is correct |
30 |
Correct |
63 ms |
14828 KB |
Output is correct |
31 |
Correct |
64 ms |
14316 KB |
Output is correct |
32 |
Correct |
63 ms |
14900 KB |
Output is correct |
33 |
Correct |
59 ms |
14700 KB |
Output is correct |
34 |
Correct |
63 ms |
14828 KB |
Output is correct |
35 |
Correct |
0 ms |
256 KB |
Output is correct |
36 |
Correct |
1 ms |
256 KB |
Output is correct |
37 |
Correct |
62 ms |
14836 KB |
Output is correct |
38 |
Correct |
63 ms |
14828 KB |
Output is correct |
39 |
Correct |
62 ms |
14828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
81 ms |
16880 KB |
Output is correct |
6 |
Correct |
73 ms |
14960 KB |
Output is correct |
7 |
Correct |
83 ms |
16756 KB |
Output is correct |
8 |
Correct |
67 ms |
14576 KB |
Output is correct |
9 |
Correct |
79 ms |
16756 KB |
Output is correct |
10 |
Correct |
80 ms |
15468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
256 KB |
Output is correct |
21 |
Correct |
1 ms |
256 KB |
Output is correct |
22 |
Correct |
1 ms |
256 KB |
Output is correct |
23 |
Correct |
0 ms |
256 KB |
Output is correct |
24 |
Correct |
1 ms |
256 KB |
Output is correct |
25 |
Correct |
0 ms |
256 KB |
Output is correct |
26 |
Correct |
1 ms |
256 KB |
Output is correct |
27 |
Correct |
1 ms |
256 KB |
Output is correct |
28 |
Correct |
0 ms |
256 KB |
Output is correct |
29 |
Correct |
1 ms |
256 KB |
Output is correct |
30 |
Correct |
1 ms |
256 KB |
Output is correct |
31 |
Correct |
1 ms |
256 KB |
Output is correct |
32 |
Correct |
1 ms |
256 KB |
Output is correct |
33 |
Correct |
0 ms |
256 KB |
Output is correct |
34 |
Correct |
1 ms |
256 KB |
Output is correct |
35 |
Correct |
1 ms |
256 KB |
Output is correct |
36 |
Correct |
0 ms |
256 KB |
Output is correct |
37 |
Correct |
1 ms |
256 KB |
Output is correct |
38 |
Correct |
0 ms |
256 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
512 KB |
Output is correct |
42 |
Correct |
1 ms |
512 KB |
Output is correct |
43 |
Correct |
1 ms |
512 KB |
Output is correct |
44 |
Correct |
1 ms |
512 KB |
Output is correct |
45 |
Correct |
1 ms |
512 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
512 KB |
Output is correct |
48 |
Correct |
1 ms |
512 KB |
Output is correct |
49 |
Correct |
1 ms |
384 KB |
Output is correct |
50 |
Correct |
1 ms |
512 KB |
Output is correct |
51 |
Correct |
1 ms |
384 KB |
Output is correct |
52 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
256 KB |
Output is correct |
21 |
Correct |
1 ms |
256 KB |
Output is correct |
22 |
Correct |
1 ms |
256 KB |
Output is correct |
23 |
Correct |
0 ms |
256 KB |
Output is correct |
24 |
Correct |
1 ms |
256 KB |
Output is correct |
25 |
Correct |
0 ms |
256 KB |
Output is correct |
26 |
Correct |
1 ms |
256 KB |
Output is correct |
27 |
Correct |
1 ms |
256 KB |
Output is correct |
28 |
Correct |
0 ms |
256 KB |
Output is correct |
29 |
Correct |
1 ms |
256 KB |
Output is correct |
30 |
Correct |
1 ms |
256 KB |
Output is correct |
31 |
Correct |
1 ms |
256 KB |
Output is correct |
32 |
Correct |
1 ms |
256 KB |
Output is correct |
33 |
Correct |
0 ms |
256 KB |
Output is correct |
34 |
Correct |
1 ms |
256 KB |
Output is correct |
35 |
Correct |
1 ms |
256 KB |
Output is correct |
36 |
Correct |
0 ms |
256 KB |
Output is correct |
37 |
Correct |
0 ms |
256 KB |
Output is correct |
38 |
Correct |
1 ms |
256 KB |
Output is correct |
39 |
Correct |
0 ms |
256 KB |
Output is correct |
40 |
Correct |
0 ms |
256 KB |
Output is correct |
41 |
Correct |
1 ms |
256 KB |
Output is correct |
42 |
Correct |
0 ms |
256 KB |
Output is correct |
43 |
Correct |
1 ms |
256 KB |
Output is correct |
44 |
Correct |
0 ms |
256 KB |
Output is correct |
45 |
Correct |
0 ms |
256 KB |
Output is correct |
46 |
Correct |
1 ms |
256 KB |
Output is correct |
47 |
Correct |
0 ms |
256 KB |
Output is correct |
48 |
Correct |
1 ms |
256 KB |
Output is correct |
49 |
Correct |
1 ms |
256 KB |
Output is correct |
50 |
Correct |
1 ms |
384 KB |
Output is correct |
51 |
Correct |
1 ms |
384 KB |
Output is correct |
52 |
Correct |
1 ms |
512 KB |
Output is correct |
53 |
Correct |
1 ms |
512 KB |
Output is correct |
54 |
Correct |
6 ms |
1792 KB |
Output is correct |
55 |
Correct |
6 ms |
1792 KB |
Output is correct |
56 |
Correct |
64 ms |
14316 KB |
Output is correct |
57 |
Correct |
64 ms |
14316 KB |
Output is correct |
58 |
Correct |
67 ms |
14316 KB |
Output is correct |
59 |
Correct |
57 ms |
14700 KB |
Output is correct |
60 |
Correct |
65 ms |
14316 KB |
Output is correct |
61 |
Correct |
61 ms |
14180 KB |
Output is correct |
62 |
Correct |
65 ms |
14828 KB |
Output is correct |
63 |
Correct |
62 ms |
14828 KB |
Output is correct |
64 |
Correct |
63 ms |
14828 KB |
Output is correct |
65 |
Correct |
64 ms |
14316 KB |
Output is correct |
66 |
Correct |
63 ms |
14900 KB |
Output is correct |
67 |
Correct |
59 ms |
14700 KB |
Output is correct |
68 |
Correct |
63 ms |
14828 KB |
Output is correct |
69 |
Correct |
0 ms |
256 KB |
Output is correct |
70 |
Correct |
1 ms |
256 KB |
Output is correct |
71 |
Correct |
62 ms |
14836 KB |
Output is correct |
72 |
Correct |
63 ms |
14828 KB |
Output is correct |
73 |
Correct |
62 ms |
14828 KB |
Output is correct |
74 |
Correct |
1 ms |
256 KB |
Output is correct |
75 |
Correct |
0 ms |
256 KB |
Output is correct |
76 |
Correct |
1 ms |
256 KB |
Output is correct |
77 |
Correct |
1 ms |
256 KB |
Output is correct |
78 |
Correct |
81 ms |
16880 KB |
Output is correct |
79 |
Correct |
73 ms |
14960 KB |
Output is correct |
80 |
Correct |
83 ms |
16756 KB |
Output is correct |
81 |
Correct |
67 ms |
14576 KB |
Output is correct |
82 |
Correct |
79 ms |
16756 KB |
Output is correct |
83 |
Correct |
80 ms |
15468 KB |
Output is correct |
84 |
Correct |
1 ms |
256 KB |
Output is correct |
85 |
Correct |
0 ms |
256 KB |
Output is correct |
86 |
Correct |
1 ms |
384 KB |
Output is correct |
87 |
Correct |
1 ms |
384 KB |
Output is correct |
88 |
Correct |
1 ms |
512 KB |
Output is correct |
89 |
Correct |
1 ms |
512 KB |
Output is correct |
90 |
Correct |
1 ms |
512 KB |
Output is correct |
91 |
Correct |
1 ms |
512 KB |
Output is correct |
92 |
Correct |
1 ms |
512 KB |
Output is correct |
93 |
Correct |
1 ms |
384 KB |
Output is correct |
94 |
Correct |
1 ms |
512 KB |
Output is correct |
95 |
Correct |
1 ms |
512 KB |
Output is correct |
96 |
Correct |
1 ms |
384 KB |
Output is correct |
97 |
Correct |
1 ms |
512 KB |
Output is correct |
98 |
Correct |
1 ms |
384 KB |
Output is correct |
99 |
Correct |
1 ms |
512 KB |
Output is correct |
100 |
Correct |
1 ms |
512 KB |
Output is correct |
101 |
Correct |
8 ms |
1920 KB |
Output is correct |
102 |
Correct |
8 ms |
1792 KB |
Output is correct |
103 |
Correct |
105 ms |
16624 KB |
Output is correct |
104 |
Correct |
85 ms |
16492 KB |
Output is correct |
105 |
Correct |
94 ms |
15212 KB |
Output is correct |
106 |
Correct |
81 ms |
16884 KB |
Output is correct |
107 |
Correct |
79 ms |
14700 KB |
Output is correct |
108 |
Correct |
78 ms |
14828 KB |
Output is correct |
109 |
Correct |
63 ms |
15212 KB |
Output is correct |
110 |
Correct |
63 ms |
14828 KB |
Output is correct |
111 |
Correct |
95 ms |
16492 KB |
Output is correct |
112 |
Correct |
95 ms |
15984 KB |
Output is correct |
113 |
Correct |
87 ms |
15144 KB |
Output is correct |
114 |
Correct |
86 ms |
16760 KB |
Output is correct |
115 |
Correct |
80 ms |
16760 KB |
Output is correct |