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;
#define ll long long
const ll maxn=2e5+5;
ll fen[maxn];
bool vis[maxn];
void upd(ll x){
if (x==0) return;
while (x<maxn){
fen[x]++;
x+=(x&-x);
}
}
ll query(ll x){
ll ans=0;
while (x){
ans+=fen[x];
x-=(x&-x);
}
return ans;
}
ll query(ll x, ll y){
return query(y)-query(x-1);
}
queue<ll> adj[maxn];
ll n;
long long count_swaps(std::vector<int> v){
n=v.size();
for (int i=0;i<n;i++){
adj[v[i]+100000].emplace(i);
}
ll ans=0;
for (int i=0;i<n;i++){
if (vis[i]) continue;
ll ele=v[i];
ll inv=-v[i];
ll pos=adj[inv+100000].front();
adj[inv+100000].pop();
adj[ele+100000].pop();
ans+=(pos-i-1)-query(i+1,pos);
ans+=(ele>inv);
upd(i),upd(pos);
vis[i]=vis[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... |