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>
#define ll long long
#define pb push_back
#define F first
#define S second
#define coy cout<<"YES\n"
#define con cout<<"NO\n"
#define co1 cout<<"-1\n"
#define sc(x) scanf("%lld",&x)
#define all(x) x.begin(),x.end()
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int SI=5e6+7;
ll INF=8e18+7;
int MOD=1e9+7;
ll n,a[SI],vis[SI],st[SI];
void update(int x,int l,int r,int in)
{
if (l==r)
{
st[x]++;
return ;
}
int mid=(l+r)/2;
if(in<=mid)
update(2*x,l,mid,in);
else update(2*x+1,mid+1,r,in);
st[x]=st[x*2]+st[2*x+1];
}
int qu(int x,int l,int r,int ql,int qr)
{
if (ql<=l&&r<=qr)
return st[x];
if (ql>r||qr<l)
return 0;
int mid=(l+r)/2;
return qu(2*x,l,mid,ql,qr)+qu(2*x+1,mid+1,r,ql,qr);
}
map <ll,ll> la,lai,lan;
int root(int x)
{
if (vis[x]==0)
return x;
if(vis[lai[x]]==0)
return lai[x];
return lai[x]=root(lai[x]);
}
long long count_swaps(std::vector<int> s) {
n=s.size()/2;
for (int i=n*2-1;i>=0;i--)
{
if (la[s[i]])
lai[i]=la[s[i]];
if (la[-s[i]])
lan[i]=la[-s[i]];
la[s[i]]=i;
}
ll ans=0;
for(int i=0;i<2*n;i++)
{
if (vis[i])
continue;
if (s[i]>0)
{
int u=lan[i];
u=root(u);
ans+=u-i;
vis[u]=1;
//cout << i<<" " <<u<<"\n";
ans-=qu(1,1,2*n,i+1,u+1);
update(1,1,2*n,u+1);
}
else
{
int u=lan[i];
u=root(u);
ans+=u-i-1;
ans-=qu(1,1,2*n,i+1,u+1);
vis[u]=1;
update(1,1,2*n,u+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... |