#include <iostream>
#include <map>
#include <queue>
using namespace std;
const int N = 2e5 + 5;
long long tree[4 * N];
long long fix[N];
map<int, queue<int>> v;
void update(int i, int L, int R, int p) {
if (L == R) {
tree[i] = 1;
return;
}
int x = 2 * i, y = x + 1;
int m = (L + R) / 2;
if (p <= m) {
update(x, L, m, p);
} else {
update(y, m + 1, R, p);
}
tree[i] = tree[x] + tree[y];
}
long long query(int i, int L, int R, int l, int r) {
if (l > r) return 0;
if (L == l && R == r) {
return tree[i];
}
int m = (L + R) / 2;
return (query(i * 2, L, m, l, min(r, m)) + query(i * 2 + 1, m + 1, R, max(m + 1, l), r));
}
long long count_swaps(vector<int> s) {
long long n = s.size();
s.insert(s.begin(), 0);
for (int i = 1; i <= n; i++) {
v[s[i]].push(i);
}
long long op = 0;
for (int i = 1; i <= n; i++) {
if (query(1, 1, n, i, i)) continue;
long long a = s[i];
int ib = v[-a].front();
v[-a].pop();
v[a].pop();
op += (ib - i - query(1, 1, n, i, ib));
if (a < 0) op--;
update(1, 1, n, ib);
update(1, 1, n, i);
}
return op;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |