#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;
using llong = long long;
#define BUGO(x) cerr << #x << " = " << x << '\n';
const int maxN = 200'005;
int t[maxN * 4];
int Get(int v, int vl, int vr, int i) {
if (vl == vr)
return t[v];
int vm = vl + (vr - vl) / 2;
if (i <= vm)
return t[v] + Get(v * 2, vl, vm, i);
return t[v] + Get(v * 2 + 1, vm + 1, vr, i);
}
void Add(int v, int vl, int vr, int l, int r, int val) {
if (l > vr || vl > r) return;
if (l <= vl && vr <= r) {
t[v] += val;
return;
}
int vm = vl + (vr - vl) / 2;
Add(v * 2, vl, vm, l, r, val);
Add(v * 2 + 1, vm + 1, vr, l, r, val);
}
vector<int> pos_[maxN * 2];
vector<int>* pos = pos_ + maxN;
long long count_swaps(vector<int> a) {
int n = a.size();
for (int i = 1; i < n; ++i)
Add(1, 0, n - 1, i, i, i);
for (int i = 0; i < n; ++i)
pos[a[i]].push_back(i);
for (int i = -n; i <= n; ++i)
reverse(pos[i].begin(), pos[i].end());
llong ans = 0;
set<int> s;
for (int i = 0; i < n; ++i)
s.insert(i);
for (int i = 0; i < n; i += 2)
{
int ai = a[*s.begin()];
int ai1 = a[*next(s.begin())];
if (ai == -ai1 && ai < 0) {
s.erase(s.begin(), next(s.begin(), 2));
pos[ai].pop_back();
pos[ai1].pop_back();
continue;
}
int j = pos[-ai].back();
s.erase(s.begin());
s.erase(s.find(j));
pos[-ai].pop_back();
pos[ai].pop_back();
ans += (Get(1, 0, n - 1, j) - (i + 1));
// if (i + 1 <= j - 1)
Add(1, 0, n - 1, 0, j - 1, 1);
if (ai > 0)
++ans;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9692 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9692 KB |
Output is correct |
6 |
Correct |
5 ms |
9628 KB |
Output is correct |
7 |
Correct |
5 ms |
9692 KB |
Output is correct |
8 |
Correct |
5 ms |
9696 KB |
Output is correct |
9 |
Correct |
7 ms |
9692 KB |
Output is correct |
10 |
Correct |
6 ms |
9660 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9696 KB |
Output is correct |
13 |
Correct |
5 ms |
9676 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9696 KB |
Output is correct |
16 |
Correct |
6 ms |
9696 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Correct |
5 ms |
9696 KB |
Output is correct |
20 |
Correct |
6 ms |
9640 KB |
Output is correct |
21 |
Correct |
5 ms |
9676 KB |
Output is correct |
22 |
Correct |
5 ms |
9688 KB |
Output is correct |
23 |
Correct |
5 ms |
9676 KB |
Output is correct |
24 |
Correct |
5 ms |
9696 KB |
Output is correct |
25 |
Correct |
6 ms |
9676 KB |
Output is correct |
26 |
Correct |
6 ms |
9676 KB |
Output is correct |
27 |
Correct |
5 ms |
9692 KB |
Output is correct |
28 |
Correct |
5 ms |
9676 KB |
Output is correct |
29 |
Correct |
6 ms |
9676 KB |
Output is correct |
30 |
Correct |
6 ms |
9692 KB |
Output is correct |
31 |
Correct |
5 ms |
9676 KB |
Output is correct |
32 |
Correct |
5 ms |
9676 KB |
Output is correct |
33 |
Correct |
5 ms |
9676 KB |
Output is correct |
34 |
Correct |
5 ms |
9676 KB |
Output is correct |
35 |
Correct |
5 ms |
9700 KB |
Output is correct |
36 |
Correct |
5 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9692 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
5 ms |
9596 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
5 ms |
9676 KB |
Output is correct |
11 |
Correct |
5 ms |
9672 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9700 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9696 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9828 KB |
Output is correct |
19 |
Correct |
7 ms |
9836 KB |
Output is correct |
20 |
Correct |
15 ms |
11136 KB |
Output is correct |
21 |
Correct |
15 ms |
11232 KB |
Output is correct |
22 |
Correct |
119 ms |
24828 KB |
Output is correct |
23 |
Correct |
105 ms |
24172 KB |
Output is correct |
24 |
Correct |
140 ms |
24868 KB |
Output is correct |
25 |
Correct |
120 ms |
24288 KB |
Output is correct |
26 |
Correct |
135 ms |
24768 KB |
Output is correct |
27 |
Correct |
149 ms |
24532 KB |
Output is correct |
28 |
Correct |
118 ms |
24520 KB |
Output is correct |
29 |
Correct |
157 ms |
24480 KB |
Output is correct |
30 |
Correct |
125 ms |
24428 KB |
Output is correct |
31 |
Correct |
127 ms |
24740 KB |
Output is correct |
32 |
Correct |
150 ms |
24464 KB |
Output is correct |
33 |
Correct |
152 ms |
24316 KB |
Output is correct |
34 |
Correct |
120 ms |
24504 KB |
Output is correct |
35 |
Correct |
5 ms |
9676 KB |
Output is correct |
36 |
Correct |
5 ms |
9676 KB |
Output is correct |
37 |
Correct |
126 ms |
24496 KB |
Output is correct |
38 |
Correct |
133 ms |
24508 KB |
Output is correct |
39 |
Correct |
125 ms |
24400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9696 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
151 ms |
29960 KB |
Output is correct |
6 |
Correct |
140 ms |
25060 KB |
Output is correct |
7 |
Correct |
150 ms |
29924 KB |
Output is correct |
8 |
Correct |
131 ms |
24472 KB |
Output is correct |
9 |
Correct |
153 ms |
29908 KB |
Output is correct |
10 |
Correct |
176 ms |
26908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9692 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9692 KB |
Output is correct |
6 |
Correct |
5 ms |
9628 KB |
Output is correct |
7 |
Correct |
5 ms |
9692 KB |
Output is correct |
8 |
Correct |
5 ms |
9696 KB |
Output is correct |
9 |
Correct |
7 ms |
9692 KB |
Output is correct |
10 |
Correct |
6 ms |
9660 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9696 KB |
Output is correct |
13 |
Correct |
5 ms |
9676 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9696 KB |
Output is correct |
16 |
Correct |
6 ms |
9696 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Correct |
5 ms |
9696 KB |
Output is correct |
20 |
Correct |
6 ms |
9640 KB |
Output is correct |
21 |
Correct |
5 ms |
9676 KB |
Output is correct |
22 |
Correct |
5 ms |
9688 KB |
Output is correct |
23 |
Correct |
5 ms |
9676 KB |
Output is correct |
24 |
Correct |
5 ms |
9696 KB |
Output is correct |
25 |
Correct |
6 ms |
9676 KB |
Output is correct |
26 |
Correct |
6 ms |
9676 KB |
Output is correct |
27 |
Correct |
5 ms |
9692 KB |
Output is correct |
28 |
Correct |
5 ms |
9676 KB |
Output is correct |
29 |
Correct |
6 ms |
9676 KB |
Output is correct |
30 |
Correct |
6 ms |
9692 KB |
Output is correct |
31 |
Correct |
5 ms |
9676 KB |
Output is correct |
32 |
Correct |
5 ms |
9676 KB |
Output is correct |
33 |
Correct |
5 ms |
9676 KB |
Output is correct |
34 |
Correct |
5 ms |
9676 KB |
Output is correct |
35 |
Correct |
5 ms |
9700 KB |
Output is correct |
36 |
Correct |
5 ms |
9676 KB |
Output is correct |
37 |
Correct |
8 ms |
9676 KB |
Output is correct |
38 |
Correct |
8 ms |
9692 KB |
Output is correct |
39 |
Correct |
8 ms |
9676 KB |
Output is correct |
40 |
Correct |
9 ms |
9656 KB |
Output is correct |
41 |
Correct |
8 ms |
9804 KB |
Output is correct |
42 |
Correct |
7 ms |
9832 KB |
Output is correct |
43 |
Correct |
7 ms |
9816 KB |
Output is correct |
44 |
Correct |
6 ms |
9832 KB |
Output is correct |
45 |
Correct |
6 ms |
9804 KB |
Output is correct |
46 |
Correct |
8 ms |
9832 KB |
Output is correct |
47 |
Correct |
6 ms |
9804 KB |
Output is correct |
48 |
Correct |
6 ms |
9832 KB |
Output is correct |
49 |
Correct |
9 ms |
9804 KB |
Output is correct |
50 |
Correct |
6 ms |
9832 KB |
Output is correct |
51 |
Correct |
6 ms |
9828 KB |
Output is correct |
52 |
Correct |
6 ms |
9804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9692 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9692 KB |
Output is correct |
6 |
Correct |
5 ms |
9628 KB |
Output is correct |
7 |
Correct |
5 ms |
9692 KB |
Output is correct |
8 |
Correct |
5 ms |
9696 KB |
Output is correct |
9 |
Correct |
7 ms |
9692 KB |
Output is correct |
10 |
Correct |
6 ms |
9660 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9696 KB |
Output is correct |
13 |
Correct |
5 ms |
9676 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9696 KB |
Output is correct |
16 |
Correct |
6 ms |
9696 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Correct |
5 ms |
9696 KB |
Output is correct |
20 |
Correct |
6 ms |
9640 KB |
Output is correct |
21 |
Correct |
5 ms |
9676 KB |
Output is correct |
22 |
Correct |
5 ms |
9688 KB |
Output is correct |
23 |
Correct |
5 ms |
9676 KB |
Output is correct |
24 |
Correct |
5 ms |
9696 KB |
Output is correct |
25 |
Correct |
6 ms |
9676 KB |
Output is correct |
26 |
Correct |
6 ms |
9676 KB |
Output is correct |
27 |
Correct |
5 ms |
9692 KB |
Output is correct |
28 |
Correct |
5 ms |
9676 KB |
Output is correct |
29 |
Correct |
6 ms |
9676 KB |
Output is correct |
30 |
Correct |
6 ms |
9692 KB |
Output is correct |
31 |
Correct |
5 ms |
9676 KB |
Output is correct |
32 |
Correct |
5 ms |
9676 KB |
Output is correct |
33 |
Correct |
5 ms |
9676 KB |
Output is correct |
34 |
Correct |
5 ms |
9676 KB |
Output is correct |
35 |
Correct |
5 ms |
9700 KB |
Output is correct |
36 |
Correct |
5 ms |
9676 KB |
Output is correct |
37 |
Correct |
5 ms |
9676 KB |
Output is correct |
38 |
Correct |
5 ms |
9676 KB |
Output is correct |
39 |
Correct |
7 ms |
9676 KB |
Output is correct |
40 |
Correct |
6 ms |
9676 KB |
Output is correct |
41 |
Correct |
5 ms |
9596 KB |
Output is correct |
42 |
Correct |
6 ms |
9676 KB |
Output is correct |
43 |
Correct |
5 ms |
9676 KB |
Output is correct |
44 |
Correct |
5 ms |
9676 KB |
Output is correct |
45 |
Correct |
5 ms |
9672 KB |
Output is correct |
46 |
Correct |
6 ms |
9676 KB |
Output is correct |
47 |
Correct |
6 ms |
9700 KB |
Output is correct |
48 |
Correct |
5 ms |
9676 KB |
Output is correct |
49 |
Correct |
5 ms |
9676 KB |
Output is correct |
50 |
Correct |
6 ms |
9696 KB |
Output is correct |
51 |
Correct |
6 ms |
9676 KB |
Output is correct |
52 |
Correct |
6 ms |
9828 KB |
Output is correct |
53 |
Correct |
7 ms |
9836 KB |
Output is correct |
54 |
Correct |
15 ms |
11136 KB |
Output is correct |
55 |
Correct |
15 ms |
11232 KB |
Output is correct |
56 |
Correct |
119 ms |
24828 KB |
Output is correct |
57 |
Correct |
105 ms |
24172 KB |
Output is correct |
58 |
Correct |
140 ms |
24868 KB |
Output is correct |
59 |
Correct |
120 ms |
24288 KB |
Output is correct |
60 |
Correct |
135 ms |
24768 KB |
Output is correct |
61 |
Correct |
149 ms |
24532 KB |
Output is correct |
62 |
Correct |
118 ms |
24520 KB |
Output is correct |
63 |
Correct |
157 ms |
24480 KB |
Output is correct |
64 |
Correct |
125 ms |
24428 KB |
Output is correct |
65 |
Correct |
127 ms |
24740 KB |
Output is correct |
66 |
Correct |
150 ms |
24464 KB |
Output is correct |
67 |
Correct |
152 ms |
24316 KB |
Output is correct |
68 |
Correct |
120 ms |
24504 KB |
Output is correct |
69 |
Correct |
5 ms |
9676 KB |
Output is correct |
70 |
Correct |
5 ms |
9676 KB |
Output is correct |
71 |
Correct |
126 ms |
24496 KB |
Output is correct |
72 |
Correct |
133 ms |
24508 KB |
Output is correct |
73 |
Correct |
125 ms |
24400 KB |
Output is correct |
74 |
Correct |
7 ms |
9676 KB |
Output is correct |
75 |
Correct |
6 ms |
9676 KB |
Output is correct |
76 |
Correct |
6 ms |
9696 KB |
Output is correct |
77 |
Correct |
5 ms |
9676 KB |
Output is correct |
78 |
Correct |
151 ms |
29960 KB |
Output is correct |
79 |
Correct |
140 ms |
25060 KB |
Output is correct |
80 |
Correct |
150 ms |
29924 KB |
Output is correct |
81 |
Correct |
131 ms |
24472 KB |
Output is correct |
82 |
Correct |
153 ms |
29908 KB |
Output is correct |
83 |
Correct |
176 ms |
26908 KB |
Output is correct |
84 |
Correct |
8 ms |
9676 KB |
Output is correct |
85 |
Correct |
8 ms |
9692 KB |
Output is correct |
86 |
Correct |
8 ms |
9676 KB |
Output is correct |
87 |
Correct |
9 ms |
9656 KB |
Output is correct |
88 |
Correct |
8 ms |
9804 KB |
Output is correct |
89 |
Correct |
7 ms |
9832 KB |
Output is correct |
90 |
Correct |
7 ms |
9816 KB |
Output is correct |
91 |
Correct |
6 ms |
9832 KB |
Output is correct |
92 |
Correct |
6 ms |
9804 KB |
Output is correct |
93 |
Correct |
8 ms |
9832 KB |
Output is correct |
94 |
Correct |
6 ms |
9804 KB |
Output is correct |
95 |
Correct |
6 ms |
9832 KB |
Output is correct |
96 |
Correct |
9 ms |
9804 KB |
Output is correct |
97 |
Correct |
6 ms |
9832 KB |
Output is correct |
98 |
Correct |
6 ms |
9828 KB |
Output is correct |
99 |
Correct |
6 ms |
9804 KB |
Output is correct |
100 |
Correct |
6 ms |
9804 KB |
Output is correct |
101 |
Correct |
19 ms |
11624 KB |
Output is correct |
102 |
Correct |
19 ms |
11204 KB |
Output is correct |
103 |
Correct |
218 ms |
30208 KB |
Output is correct |
104 |
Correct |
124 ms |
29656 KB |
Output is correct |
105 |
Correct |
172 ms |
27492 KB |
Output is correct |
106 |
Correct |
135 ms |
29868 KB |
Output is correct |
107 |
Correct |
148 ms |
25428 KB |
Output is correct |
108 |
Correct |
141 ms |
25832 KB |
Output is correct |
109 |
Correct |
125 ms |
24532 KB |
Output is correct |
110 |
Correct |
121 ms |
24524 KB |
Output is correct |
111 |
Correct |
157 ms |
29200 KB |
Output is correct |
112 |
Correct |
187 ms |
28816 KB |
Output is correct |
113 |
Correct |
174 ms |
25280 KB |
Output is correct |
114 |
Correct |
150 ms |
29912 KB |
Output is correct |
115 |
Correct |
138 ms |
29892 KB |
Output is correct |