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 N 400005
int fen[N],cnt[N];
long long ans;
vector<int> v[N];
bitset<N> vis;
void update(int x,int k){
while(x<N){
fen[x]+=k;
x+=x&-x;
}
}
int sol(int x){
int ans=0;
while(x){
ans+=fen[x];
x-=x&-x;
}
return ans;
}
long long count_swaps(vector<int> s){
int n,i,now,nxt,pos;
n=s.size();
s.insert(s.begin(),0);
for(i=1;i<=n;i++)fen[i]=i&-i;
for(i=1;i<=n;i++)v[(s[i]>0)?s[i]:-s[i]+n].push_back(i);
for(i=1;i<=n;i++){
if(vis[i])continue;
if(s[i]>0)now=s[i],nxt=s[i]+n;
else now=-s[i]+n,nxt=-s[i];
cnt[now]++;
pos=v[nxt][cnt[nxt]++];
ans+=sol(pos)-sol(i);
if(s[i]<0)ans--;
update(i,1);
update(pos,-1);
vis[i]=vis[pos]=true;
}
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... |