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"
#include "shoes.h"
#define MAXN 200009
#define pb(x) push_back(x)
#define ppb() pop_back()
using namespace std;
int vis[MAXN],F[MAXN];
vector<int>a[MAXN],b[MAXN];
int get(int x){int res=0;for(;x;x-=x&(-x))res+=F[x];return res;}
void upd(int x){for(;x<MAXN;x+=x&(-x))F[x]++;}
long long count_swaps(std::vector<int> s) {
int n=s.size();long long ans=0;
for(int i=n-1;i>=0;i--){
if(s[i]>0)a[s[i]].pb(i);
else b[-s[i]].pb(i);}
for(int i=0,j;i<n;i++){
if(vis[i])continue;
if(s[i]>0)j=b[s[i]].back();
else j=a[-s[i]].back();
a[abs(s[i])].ppb();b[abs(s[i])].ppb();
ans+=(s[i]>s[j])+j-i-1;vis[j]=1;
ans-=get(j)-get(i);upd(j);
//for(int k=i+1;k<j;k++)ans-=vis[k];
}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... |