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;
#define ll long long
const ll MOD = 1e9+7;
const ll N = 2e5+10;
const ll inf = 1e9+100;
const double eps = 1;
ll n, a[N], t[N*4];
set<ll> L[N], R[N];
ll ans;
bool used[N];
void update(ll tv, ll tl, ll tr, ll l, ll r) {
if(l > r) return;
if(tl == l && tr == r) {
t[tv]++;
return;
}
else {
ll tm =(tl + tr) >> 1;
update(tv * 2, tl, tm, l, min(tm, r));
update(tv * 2 + 1, tm + 1, tr, max(tm + 1, l), r);
}
}
ll get(ll tv, ll tl, ll tr, ll pos) {
ll tm = (tl+tr)>>1;
if(tl == tr && tl == pos) return t[tv];
else {
if(pos <= tm) return t[tv] + get(tv * 2, tl, tm, pos);
else return t[tv] + get(tv * 2 +1, tm + 1, tr, pos);
}
}
long long count_swaps(std::vector<int> s) {
/*for(ll c : s) prllf("%d ", c);
prllf("\n");*/
ans = 0;
n = s.size();
for(ll i = 1;i <= n;i++) {
a[i] = s[i-1];
if(a[i] < 0) L[-a[i]].insert(i);
else R[a[i]].insert(i);
}
/*for(ll i = 1; i <= 2; i++) {
prllf("left %d:", i);
for(ll c : L[i]) prllf("%d ", c);
prllf("\n");
}
for(ll i = 1; i <= 2; i++) {
prllf("right %d:", i);
for(ll c : R[i]) prllf("%d ", c);
prllf("\n");
}*/
for(ll i = 1; i <= n; i++){
if(used[i]) continue;
ll pos;
if(a[i] < 0) {
pos = *R[-a[i]].begin();
L[-a[i]].erase(i);
R[-a[i]].erase(pos);
ans += pos-(i+1) + get(1, 1, n, pos) - get(1, 1, n, i+1);
used[pos] = used[i] = 1;
}
else {
pos = *L[a[i]].begin();
R[a[i]].erase(i);
L[a[i]].erase(pos);
ans += pos - i + get(1, 1, n, pos) - get(1, 1, n, i);
used[pos] = used[i] = 1;
}
if(i+1 <= pos - 1)update(1, 1, n, i+1, pos - 1);
}
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... |