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;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pi;
typedef vector<pi> pii;
typedef tuple<int,int,int> tii;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define LSOne(s) ((s)&(-s))
ll INF=1e18+10;
int inf=1e9+10;
ll M=1e9+7;
vi ft;
int n;
void update(int p){
while(p<n+1){
int k=LSOne(p);
ft[p]+=1;
p+=k;
}
}
int count(int p){
int ans=0;
while(p>0){
int k=LSOne(p);
ans+=ft[p];
p-=k;
}
return ans;
}
long long count_swaps(std::vector<int> s) {
n=s.size();
ll ans=0;
vector<set<int>> a(n+1),b(n+1);
ft.resize(n+1,0);
vi d(n,0);
REP(i,0,n){
if(s[i]<0)a[abs(s[i])].insert(i);
else b[s[i]].insert(i);
}
REP(i,0,n){
if(d[i]==1)continue;
d[i]=1;
if(s[i]<0){
a[abs(s[i])].erase(i);
auto it=b[abs(s[i])].begin();
ans+=*it-i;
ans--;
ans-=count(*it+1)-count(i);
update(*it+1);
b[abs(s[i])].erase(*it);
d[*it]=1;
}
else{
b[abs(s[i])].erase(i);
auto it=a[abs(s[i])].begin();
ans+=*it-i;
ans-=count(*it+1)-count(i);
update(*it+1);
a[abs(s[i])].erase(*it);
d[*it]=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... |