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>
#include "shoes.h"
#define ll int
#define x first
#define y second
using namespace std;
ll n;
vector<ll>a;
vector<ll>t;
void build(ll v,ll tl,ll tr)
{
if (tl==tr)
t[v]=a[tl];
else
{
ll tm=(tl+tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
t[v]=t[2*v]+t[2*v+1];
}
}
void update(ll v,ll tl,ll tr,ll l,ll r,ll new_value)
{
if (tl==l && tr==r)
t[v]=new_value;
else
{
if (l>r)return;
ll tm=(tl+tr)/2;
update(v*2,tl,tm,l,min(tm,r),new_value);
update(v*2+1,tm+1,tr,max(tm+1,l),r,new_value);
t[v]=t[2*v]+t[2*v+1];
}
}
ll get_sum(ll v,ll tl,ll tr,ll l,ll r)
{
if (l>r)return 0;
if (tl==l && tr==r)
return t[v];
ll tm=(tl+tr)/2;
return min(get_sum(v*2,tl,tm,l,min(tm,r)),
get_sum(v*2+1,tm+1,tr,max(l,tm+1),r));
}
long long count_swaps(vector<int> s)
{
n=s.size();
a.resize(n+1,1);
t.resize(4*n+1);
build(1,1,n);
map<ll,deque<ll> >mp;
for (int i=0;i<n;i++)
mp[s[i]].push_back(i);
long long ans=0;
for (ll i=0;i<n;i++)
{
if (a[i]==0)
continue;
ll p=mp[-s[i]].front();
mp[-s[i]].pop_front();
mp[s[i]].pop_front();
update(1,1,n,p,p,0);
update(1,1,n,i,i,0);
a[i]=a[p]=0;
ans+=get_sum(1,1,n,i,p);
if (s[i]>0)ans++;
}
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... |