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 "shoes.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 4e5 + 6e3;
int f[MAXN];
void add(int x, int y){
x++;
while (x < MAXN){
f[x] += y;
x += (-x & x);
}
}
void add(int l, int r, int y){
if (l > r) return;
add(l, y);
add(r + 1, -y);
}
int get(int x){
x++;
int sum = 0;
while (x > 0){
sum += f[x];
x -= (-x & x);
}
return sum;
}
int get(int l, int r){
return get(r) - get(l - 1);
}
int code(int val){
return (val < 0 ? -val : val + 200000);
}
queue<int> q[MAXN];
long long count_swaps(vector<int> a) {
int n = a.size();
for (int i = 0; i < n; i++){
q[code(a[i])].push(i);
}
ll ans = 0;
for (int i = 0; i < n; i++){
if (get(i, i)) continue;
int x = code(a[i]), y = code(-a[i]);
int pos1 = i, pos2 = q[y].front();
ans += pos2 - pos1 - 1 + (a[i] > 0);
ans -= get(pos1, pos2);
add(pos1, 1);
add(pos2, 1);
q[x].pop();
q[y].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... |