This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll fw[200200];
queue<int> pos[100100], pos2[100100];
bool ch[200200];
void update(int v, int w) {
for (int i = v + 1;i <= 200000;i += i & -i) fw[i] += w;
}
ll query(int v) {
ll ans = 0;
for (int i = v + 1;i > 0;i -= i & -i) ans += fw[i];
return ans;
}
ll count_swaps(vector<int> v) {
ll ans = 0;
int n = (int)v.size();
for (int i = 0;i < n;i++) {
if (v[i] < 0) {
pos2[-v[i]].push(i);
}
else {
pos[v[i]].push(i);
}
}
for (int i = 0;i < n;i++) {
if (ch[i]) continue;
if (v[i] < 0) {
v[i] = -v[i];
int nowpos = i + query(i);
int j = pos[v[i]].front();
ch[j] = 1;
ans += j + query(j) - nowpos - 1;
update(1, 1), update(j + 1, -1);
}
else {
int nowpos = i + query(i);
int j = pos2[v[i]].front();
ch[j] = 1;
ans += j + query(j) - nowpos;
update(1, 1), update(j + 1, -1);
}
pos[v[i]].pop();
pos2[v[i]].pop();
}
return ans;
}
# | 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... |