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 n;
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) {
n = a.size();
set<pair<int, int>> s;
for (int i = 0; i < n; i++){
int x = code(a[i]);
q[x].push(i);
s.insert(make_pair(q[x].front(), a[i]));
}
ll ans = 0;
while (!s.empty()) {
int val = s.begin()->second;
s.erase(s.begin());
int x = code(val), y = code(-val);
s.erase(make_pair(q[y].front(), -val));
int pos1 = q[x].front(), pos2 = q[y].front();
pos1 += get(pos1), pos2 += get(pos2);
if (pos1 > pos2) swap(pos1, pos2);
// cout << pos1 << ' ' << pos2 << endl;
ans += pos2 - pos1 - 1 + (val > 0);
add(pos1 + 1, pos2 - 1, 1);
q[x].pop();
q[y].pop();
if (!q[x].empty()) s.insert(make_pair(q[x].front(), val));
if (!q[y].empty()) s.insert(make_pair(q[y].front(), -val));
}
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... |