#include <bits/stdc++.h>
#define ll long long
#define FOR(i, x, y) for(int i = x; i <= y; ++i)
#define Size(v) (int)v.size()
#define pb push_back
#define eb emplace_back
using namespace std;
struct SegTree {
int n;
vector<int> st;
SegTree(int n) {
this->n = n;
st.resize(4 * n + 1, 0);
}
void update(int pos, int val, int id, int u, int v) {
if (pos < u || v < pos) return;
if (u == v) {
st[id] = val;
return;
}
int mid = (u + v) / 2;
update(pos, val, id * 2, u, mid);
update(pos, val, id * 2 + 1, mid + 1, v);
st[id] = st[id * 2] + st[id * 2 + 1];
}
int query(int l, int r, int id, int u, int v) {
if (l > r) return 0;
if (r < u || v < l) return 0;
if (l <= u && v <= r) return st[id];
int mid = (u + v) / 2;
int lv = query(l, r, id * 2, u, mid);
int rv = query(l, r, id * 2 + 1, mid + 1, v);
return (lv + rv);
}
void update(int pos, int val) {
update(pos, val, 1, 1, n);
}
int query(int l, int r) {
return query(l, r, 1, 1, n);
}
};
const int nax = 4e5 + 5;
// map<int, int> pos, curr;
int curr[nax + 2];
vector<int> appear[nax + 2];
int mp[nax + 2];
bool solved[nax + 2], used[nax + 2];
int64_t count_swaps(vector<int> a) {
int n = Size(a);
// cerr << "here1\n";
SegTree st(n);
// cerr << "?\n";
for (int i = 1; i <= n; ++i) st.update(i, 1);
// cerr << "here3\n";
for (int i = 0; i < n; ++i) {
int idx = a[i];
if (a[i] < 0) idx = nax + a[i];
appear[idx].pb(i + 1);
}
for (int i = 0; i < n; ++i) {
if (used[i + 1]) continue;
used[i + 1] = true;
int idx = -a[i];
if (idx < 0) idx = nax + idx;
mp[i + 1] = appear[idx][curr[idx]];
int idxa = a[i];
if (idxa < 0) idxa = nax + idxa;
++curr[idxa], ++curr[idx];
used[mp[i + 1]] = true;
}
// cerr << "here2\n";
int64_t res = 0;
for (int i = 1; i <= n; ++i) {
if (solved[i]) continue;
solved[i] = true;
int d = mp[i];
solved[d] = true;
if (a[i - 1] < 0) {
res += st.query(i + 1, d - 1);
}
else res += st.query(i, d - 1);
st.update(d, 0);
}
return res;
}
void test() {
int n; cin >> n;
vector<int> a;
for (int i = 0; i < n; ++i) {
int c; cin >> c;
a.pb(c);
}
cout << count_swaps(a) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
8 ms |
9760 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9728 KB |
Output is correct |
7 |
Correct |
7 ms |
9728 KB |
Output is correct |
8 |
Correct |
7 ms |
9728 KB |
Output is correct |
9 |
Correct |
7 ms |
9856 KB |
Output is correct |
10 |
Correct |
8 ms |
9728 KB |
Output is correct |
11 |
Correct |
7 ms |
9728 KB |
Output is correct |
12 |
Correct |
7 ms |
9728 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
7 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9728 KB |
Output is correct |
16 |
Correct |
7 ms |
9728 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
7 ms |
9728 KB |
Output is correct |
19 |
Correct |
7 ms |
9728 KB |
Output is correct |
20 |
Correct |
7 ms |
9728 KB |
Output is correct |
21 |
Correct |
7 ms |
9728 KB |
Output is correct |
22 |
Correct |
7 ms |
9728 KB |
Output is correct |
23 |
Correct |
7 ms |
9728 KB |
Output is correct |
24 |
Correct |
7 ms |
9728 KB |
Output is correct |
25 |
Correct |
7 ms |
9728 KB |
Output is correct |
26 |
Correct |
7 ms |
9728 KB |
Output is correct |
27 |
Correct |
7 ms |
9728 KB |
Output is correct |
28 |
Correct |
7 ms |
9728 KB |
Output is correct |
29 |
Correct |
7 ms |
9728 KB |
Output is correct |
30 |
Correct |
7 ms |
9728 KB |
Output is correct |
31 |
Correct |
7 ms |
9728 KB |
Output is correct |
32 |
Correct |
7 ms |
9728 KB |
Output is correct |
33 |
Correct |
7 ms |
9728 KB |
Output is correct |
34 |
Correct |
7 ms |
9728 KB |
Output is correct |
35 |
Correct |
7 ms |
9728 KB |
Output is correct |
36 |
Correct |
7 ms |
9728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9728 KB |
Output is correct |
7 |
Correct |
7 ms |
9728 KB |
Output is correct |
8 |
Correct |
7 ms |
9728 KB |
Output is correct |
9 |
Correct |
7 ms |
9728 KB |
Output is correct |
10 |
Correct |
7 ms |
9728 KB |
Output is correct |
11 |
Correct |
8 ms |
9728 KB |
Output is correct |
12 |
Correct |
7 ms |
9728 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
7 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9728 KB |
Output is correct |
16 |
Correct |
7 ms |
9728 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
8 ms |
9856 KB |
Output is correct |
19 |
Correct |
8 ms |
9856 KB |
Output is correct |
20 |
Correct |
16 ms |
10496 KB |
Output is correct |
21 |
Correct |
16 ms |
10496 KB |
Output is correct |
22 |
Correct |
116 ms |
16536 KB |
Output is correct |
23 |
Correct |
93 ms |
16620 KB |
Output is correct |
24 |
Correct |
113 ms |
16620 KB |
Output is correct |
25 |
Correct |
110 ms |
16112 KB |
Output is correct |
26 |
Correct |
115 ms |
16620 KB |
Output is correct |
27 |
Correct |
105 ms |
16748 KB |
Output is correct |
28 |
Correct |
116 ms |
16136 KB |
Output is correct |
29 |
Correct |
116 ms |
16112 KB |
Output is correct |
30 |
Correct |
116 ms |
16184 KB |
Output is correct |
31 |
Correct |
118 ms |
16516 KB |
Output is correct |
32 |
Correct |
121 ms |
16112 KB |
Output is correct |
33 |
Correct |
110 ms |
16180 KB |
Output is correct |
34 |
Correct |
115 ms |
16112 KB |
Output is correct |
35 |
Correct |
7 ms |
9728 KB |
Output is correct |
36 |
Correct |
7 ms |
9728 KB |
Output is correct |
37 |
Correct |
118 ms |
16108 KB |
Output is correct |
38 |
Correct |
120 ms |
16240 KB |
Output is correct |
39 |
Correct |
119 ms |
16108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
152 ms |
22284 KB |
Output is correct |
6 |
Correct |
136 ms |
17400 KB |
Output is correct |
7 |
Correct |
156 ms |
22392 KB |
Output is correct |
8 |
Correct |
117 ms |
16132 KB |
Output is correct |
9 |
Correct |
147 ms |
22264 KB |
Output is correct |
10 |
Correct |
148 ms |
19192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
8 ms |
9760 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9728 KB |
Output is correct |
7 |
Correct |
7 ms |
9728 KB |
Output is correct |
8 |
Correct |
7 ms |
9728 KB |
Output is correct |
9 |
Correct |
7 ms |
9856 KB |
Output is correct |
10 |
Correct |
8 ms |
9728 KB |
Output is correct |
11 |
Correct |
7 ms |
9728 KB |
Output is correct |
12 |
Correct |
7 ms |
9728 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
7 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9728 KB |
Output is correct |
16 |
Correct |
7 ms |
9728 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
7 ms |
9728 KB |
Output is correct |
19 |
Correct |
7 ms |
9728 KB |
Output is correct |
20 |
Correct |
7 ms |
9728 KB |
Output is correct |
21 |
Correct |
7 ms |
9728 KB |
Output is correct |
22 |
Correct |
7 ms |
9728 KB |
Output is correct |
23 |
Correct |
7 ms |
9728 KB |
Output is correct |
24 |
Correct |
7 ms |
9728 KB |
Output is correct |
25 |
Correct |
7 ms |
9728 KB |
Output is correct |
26 |
Correct |
7 ms |
9728 KB |
Output is correct |
27 |
Correct |
7 ms |
9728 KB |
Output is correct |
28 |
Correct |
7 ms |
9728 KB |
Output is correct |
29 |
Correct |
7 ms |
9728 KB |
Output is correct |
30 |
Correct |
7 ms |
9728 KB |
Output is correct |
31 |
Correct |
7 ms |
9728 KB |
Output is correct |
32 |
Correct |
7 ms |
9728 KB |
Output is correct |
33 |
Correct |
7 ms |
9728 KB |
Output is correct |
34 |
Correct |
7 ms |
9728 KB |
Output is correct |
35 |
Correct |
7 ms |
9728 KB |
Output is correct |
36 |
Correct |
7 ms |
9728 KB |
Output is correct |
37 |
Correct |
7 ms |
9728 KB |
Output is correct |
38 |
Correct |
7 ms |
9728 KB |
Output is correct |
39 |
Correct |
7 ms |
9728 KB |
Output is correct |
40 |
Correct |
7 ms |
9728 KB |
Output is correct |
41 |
Correct |
8 ms |
9856 KB |
Output is correct |
42 |
Correct |
8 ms |
9856 KB |
Output is correct |
43 |
Correct |
8 ms |
9880 KB |
Output is correct |
44 |
Correct |
8 ms |
9856 KB |
Output is correct |
45 |
Correct |
8 ms |
9856 KB |
Output is correct |
46 |
Correct |
8 ms |
9856 KB |
Output is correct |
47 |
Correct |
8 ms |
9856 KB |
Output is correct |
48 |
Correct |
8 ms |
9856 KB |
Output is correct |
49 |
Correct |
8 ms |
9856 KB |
Output is correct |
50 |
Correct |
8 ms |
9856 KB |
Output is correct |
51 |
Correct |
8 ms |
9728 KB |
Output is correct |
52 |
Correct |
8 ms |
9856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
8 ms |
9760 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9728 KB |
Output is correct |
7 |
Correct |
7 ms |
9728 KB |
Output is correct |
8 |
Correct |
7 ms |
9728 KB |
Output is correct |
9 |
Correct |
7 ms |
9856 KB |
Output is correct |
10 |
Correct |
8 ms |
9728 KB |
Output is correct |
11 |
Correct |
7 ms |
9728 KB |
Output is correct |
12 |
Correct |
7 ms |
9728 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
7 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9728 KB |
Output is correct |
16 |
Correct |
7 ms |
9728 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
7 ms |
9728 KB |
Output is correct |
19 |
Correct |
7 ms |
9728 KB |
Output is correct |
20 |
Correct |
7 ms |
9728 KB |
Output is correct |
21 |
Correct |
7 ms |
9728 KB |
Output is correct |
22 |
Correct |
7 ms |
9728 KB |
Output is correct |
23 |
Correct |
7 ms |
9728 KB |
Output is correct |
24 |
Correct |
7 ms |
9728 KB |
Output is correct |
25 |
Correct |
7 ms |
9728 KB |
Output is correct |
26 |
Correct |
7 ms |
9728 KB |
Output is correct |
27 |
Correct |
7 ms |
9728 KB |
Output is correct |
28 |
Correct |
7 ms |
9728 KB |
Output is correct |
29 |
Correct |
7 ms |
9728 KB |
Output is correct |
30 |
Correct |
7 ms |
9728 KB |
Output is correct |
31 |
Correct |
7 ms |
9728 KB |
Output is correct |
32 |
Correct |
7 ms |
9728 KB |
Output is correct |
33 |
Correct |
7 ms |
9728 KB |
Output is correct |
34 |
Correct |
7 ms |
9728 KB |
Output is correct |
35 |
Correct |
7 ms |
9728 KB |
Output is correct |
36 |
Correct |
7 ms |
9728 KB |
Output is correct |
37 |
Correct |
7 ms |
9728 KB |
Output is correct |
38 |
Correct |
7 ms |
9728 KB |
Output is correct |
39 |
Correct |
7 ms |
9728 KB |
Output is correct |
40 |
Correct |
7 ms |
9728 KB |
Output is correct |
41 |
Correct |
7 ms |
9728 KB |
Output is correct |
42 |
Correct |
7 ms |
9728 KB |
Output is correct |
43 |
Correct |
7 ms |
9728 KB |
Output is correct |
44 |
Correct |
7 ms |
9728 KB |
Output is correct |
45 |
Correct |
8 ms |
9728 KB |
Output is correct |
46 |
Correct |
7 ms |
9728 KB |
Output is correct |
47 |
Correct |
7 ms |
9728 KB |
Output is correct |
48 |
Correct |
7 ms |
9728 KB |
Output is correct |
49 |
Correct |
7 ms |
9728 KB |
Output is correct |
50 |
Correct |
7 ms |
9728 KB |
Output is correct |
51 |
Correct |
7 ms |
9728 KB |
Output is correct |
52 |
Correct |
8 ms |
9856 KB |
Output is correct |
53 |
Correct |
8 ms |
9856 KB |
Output is correct |
54 |
Correct |
16 ms |
10496 KB |
Output is correct |
55 |
Correct |
16 ms |
10496 KB |
Output is correct |
56 |
Correct |
116 ms |
16536 KB |
Output is correct |
57 |
Correct |
93 ms |
16620 KB |
Output is correct |
58 |
Correct |
113 ms |
16620 KB |
Output is correct |
59 |
Correct |
110 ms |
16112 KB |
Output is correct |
60 |
Correct |
115 ms |
16620 KB |
Output is correct |
61 |
Correct |
105 ms |
16748 KB |
Output is correct |
62 |
Correct |
116 ms |
16136 KB |
Output is correct |
63 |
Correct |
116 ms |
16112 KB |
Output is correct |
64 |
Correct |
116 ms |
16184 KB |
Output is correct |
65 |
Correct |
118 ms |
16516 KB |
Output is correct |
66 |
Correct |
121 ms |
16112 KB |
Output is correct |
67 |
Correct |
110 ms |
16180 KB |
Output is correct |
68 |
Correct |
115 ms |
16112 KB |
Output is correct |
69 |
Correct |
7 ms |
9728 KB |
Output is correct |
70 |
Correct |
7 ms |
9728 KB |
Output is correct |
71 |
Correct |
118 ms |
16108 KB |
Output is correct |
72 |
Correct |
120 ms |
16240 KB |
Output is correct |
73 |
Correct |
119 ms |
16108 KB |
Output is correct |
74 |
Correct |
7 ms |
9728 KB |
Output is correct |
75 |
Correct |
7 ms |
9728 KB |
Output is correct |
76 |
Correct |
7 ms |
9728 KB |
Output is correct |
77 |
Correct |
7 ms |
9728 KB |
Output is correct |
78 |
Correct |
152 ms |
22284 KB |
Output is correct |
79 |
Correct |
136 ms |
17400 KB |
Output is correct |
80 |
Correct |
156 ms |
22392 KB |
Output is correct |
81 |
Correct |
117 ms |
16132 KB |
Output is correct |
82 |
Correct |
147 ms |
22264 KB |
Output is correct |
83 |
Correct |
148 ms |
19192 KB |
Output is correct |
84 |
Correct |
7 ms |
9728 KB |
Output is correct |
85 |
Correct |
7 ms |
9728 KB |
Output is correct |
86 |
Correct |
7 ms |
9728 KB |
Output is correct |
87 |
Correct |
7 ms |
9728 KB |
Output is correct |
88 |
Correct |
8 ms |
9856 KB |
Output is correct |
89 |
Correct |
8 ms |
9856 KB |
Output is correct |
90 |
Correct |
8 ms |
9880 KB |
Output is correct |
91 |
Correct |
8 ms |
9856 KB |
Output is correct |
92 |
Correct |
8 ms |
9856 KB |
Output is correct |
93 |
Correct |
8 ms |
9856 KB |
Output is correct |
94 |
Correct |
8 ms |
9856 KB |
Output is correct |
95 |
Correct |
8 ms |
9856 KB |
Output is correct |
96 |
Correct |
8 ms |
9856 KB |
Output is correct |
97 |
Correct |
8 ms |
9856 KB |
Output is correct |
98 |
Correct |
8 ms |
9728 KB |
Output is correct |
99 |
Correct |
8 ms |
9856 KB |
Output is correct |
100 |
Correct |
8 ms |
9856 KB |
Output is correct |
101 |
Correct |
20 ms |
11008 KB |
Output is correct |
102 |
Correct |
20 ms |
10624 KB |
Output is correct |
103 |
Correct |
193 ms |
22648 KB |
Output is correct |
104 |
Correct |
141 ms |
23928 KB |
Output is correct |
105 |
Correct |
179 ms |
21240 KB |
Output is correct |
106 |
Correct |
165 ms |
23544 KB |
Output is correct |
107 |
Correct |
137 ms |
19192 KB |
Output is correct |
108 |
Correct |
140 ms |
19580 KB |
Output is correct |
109 |
Correct |
115 ms |
17776 KB |
Output is correct |
110 |
Correct |
118 ms |
17648 KB |
Output is correct |
111 |
Correct |
175 ms |
22776 KB |
Output is correct |
112 |
Correct |
193 ms |
22524 KB |
Output is correct |
113 |
Correct |
153 ms |
18808 KB |
Output is correct |
114 |
Correct |
149 ms |
23548 KB |
Output is correct |
115 |
Correct |
149 ms |
23544 KB |
Output is correct |