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 sz(x) (int)x.size()
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define pb push_back
#define lowb(x) (x&(-x))
#define ALL(x) x.begin(),x.end()
#define pii pair<int,int>
#define f first
#define s second
const int maxn=2e5+5;
int pos[maxn];
#define ll long long
vector<int> v[maxn];
int bit[maxn];
void upd(int x,int v){
while(x<maxn){
bit[x]+=v;
x+=lowb(x);
}
}
int query(int x){
int ans=0;
while(x){
ans+=bit[x];
x-=lowb(x);
}
return ans;
}
long long count_swaps(std::vector<int> s) {
int n=sz(s)/2;
int cur=0;
REP(i,2*n){
if(s[i]<0){
v[-s[i]].pb(i);
}
}
REP1(i,n) reverse(ALL(v[i]));
vector<pii> vv;
REP(i,2*n){
if(s[i]>0){
int z=v[s[i]].back();
v[s[i]].pop_back();
int t=i;
if(z>t) swap(z,t);
vv.pb({z,t});
}
}
sort(ALL(vv));
REP(i,n){
auto x=vv[i];
if(s[x.f]>0) swap(x.f,x.s);
pos[x.f]=2*i,pos[x.s]=2*i+1;
}
ll ans=0;
REP(i,2*n){
ans+=i-query(pos[i]);
upd(pos[i]+1,1);
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:34:6: warning: unused variable 'cur' [-Wunused-variable]
34 | int cur=0;
| ^~~
# | 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... |