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 ll long long
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define N 100005
ll n,fix[N],ans,f[2*N];
pair< vector<ll>,ll > v1[N],v2[N];
void upd(ll u)
{
u++;
while (u<=2*n)
{
f[u]++;
u+=(u&(-u));
}
return;
}
ll get(ll u,ll v)
{
v++;
ll tans=0;
while (v>0)
{
tans+=f[v];
v-=(v&(-v));
}
while (u>0)
{
tans-=f[u];
u-=(u&(-u));
}
return tans;
}
ll count_swaps(vector<int> a)
{
n=a.size()/2;
for (int i=0; i<2*n; i++)
{
if (a[i]<0)
{
v1[-a[i]].F.pb(i);
if ( v2[-a[i]].F.size()-v2[-a[i]].S>0 )
{
//cout<<i<<" "<<(v2[-a[i]].F)[ v2[-a[i]].S ]<<endl;
fix[i]= (v2[-a[i]].F)[ v2[-a[i]].S ];
fix[ (v2[-a[i]].F)[ v2[-a[i]].S ] ]
=(v2[-a[i]].F)[ v2[-a[i]].S ];
v1[-a[i]].S++;
v2[-a[i]].S++;
}
}
else
{
v2[a[i]].F.pb(i);
if ( v1[a[i]].F.size()-v1[a[i]].S>0 )
{
//cout<<i<<" "<<(v1[a[i]].F)[ v1[a[i]].S ]<<endl;
fix[i]= (v1[a[i]].F)[ v1[a[i]].S ];
fix[ (v1[a[i]].F)[ v1[a[i]].S ] ]
=(v1[a[i]].F)[ v1[a[i]].S ];
v2[a[i]].S++;
v1[a[i]].S++;
}
}
}
for (int i=0; i<2*n; i++)
{
upd(fix[i]);
if (i==fix[i])
continue;
ans+=get(fix[i]+1,i);
if (a[i]<a[fix[i]])
ans++;
}
return ans;
//cout<<ans<<endl;
}
# | 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... |