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>
#define ll long long int
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define piii pair<pii,pii>
#define precise cout<<fixed<<setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int MAX=1<<18;
bool got[MAX*2];
int t[MAX*2];
set<int>a[MAX],b[MAX];
void update(int u,int c)
{
for (t[u+=MAX]=c;u>1;u>>=1)
t[u>>1]=t[u]+t[u^1];
}
int query(int u,int v)
{
int sum=0;
for (u+=MAX,v+=MAX+1;u<v;u>>=1,v>>=1)
{
if (u&1)sum+=t[u++];
if (v&1)sum+=t[--v];
}
return sum;
}
ll count_swaps(vi tab2)
{
ll ans=0;
int n=sz(tab2);
int tab[n+1];
for (int i=0;i<=n;i++)tab[i]=0;
for (int i=1;i<=n;i++)tab[i]=tab2[i-1];
for (int i=1;i<=n;i++)
{
if (tab[i]<0)b[-tab[i]].ins(i);
else a[tab[i]].ins(i);
}
for (int i=1;i<=n;i++)update(i,1);
for (int i=1;i<=n;i++)
{
if (got[i])continue;
got[i]=true;
if (tab[i]>0)
{
int wsk=*b[tab[i]].lower_bound(i);
b[tab[i]].erase(wsk);
got[wsk]=true;
ans+=query(1,wsk)-query(1,i);
update(wsk,0);
}
else
{
int wsk=*a[-tab[i]].lower_bound(i);
a[-tab[i]].erase(wsk);
got[wsk]=true;
ans+=query(1,wsk)-query(1,i)-1;
update(wsk,0);
}
}
return ans;
}
/*
int main()
{
cout<<count_swaps({2, 1, -1, -2});
}
*/
# | 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... |