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()
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){
pos[i]=2*cur;
v[-s[i]].pb(cur);
++cur;
}
}
REP1(i,n) reverse(ALL(v[i]));
REP(i,2*n){
if(s[i]>0){
pos[i]=2*v[s[i]].back()+1;
v[s[i]].pop_back();
}
}
ll ans=0;
REP(i,2*n){
ans+=i-query(pos[i]);
upd(pos[i]+1,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... |