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;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
#define sz(x) (int) (x).size()
bool active[200200] = { false };
struct segtree {
vi arr;
int size;
segtree(int _size) {
size = 1;
while (size < _size) size *= 2;
arr.assign(2 * size, 0);
for (int i = 0; i < _size; ++i) {
arr[i + size] = i;
}
}
void addOne(int l, int r, int x, int lx, int rx) {
if (l <= lx && rx <= r) { arr[x] += 1; return; }
if (r <= lx || rx <= l) { return; }
int m = (lx + rx) / 2;
addOne(l, r, 2 * x, lx, m);
addOne(l, r, 2*x+1, m, rx);
}
void addOne(int i) {
addOne(0, i, 1, 0, size);
}
int query(int i, int x, int lx, int rx) {
if (rx - lx == 1) return arr[x];
int m = (lx + rx) / 2;
if (i < m) return query(i, 2 * x, lx, m) + arr[x];
else return query(i, 2 * x + 1, m, rx) + arr[x];
}
int query(int i) {
return query(i, 1, 0, size);
}
};
ll count_swaps(vi S) {
int n = sz(S) / 2;
ll ans = 0;
segtree seg(2 * n);
set<pii> shoes;
for (int i = 0; i < 2 * n; ++i) shoes.insert({ S[i], i });
for (int i = 0; i < 2 * n; ++i) {
if (active[i]) continue;
active[i] = true;
int pi = seg.query(i);
int vi = S[i];
auto it = shoes.lower_bound({ -vi, 0 });
int j = it->second;
active[j] = true;
int pj = seg.query(j);
shoes.erase(it);
shoes.erase(shoes.lower_bound({vi, 0}));
ans += (pj - pi);
if (vi < 0) ans -= 1;
seg.addOne(j);
}
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... |