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<<17;
vi pos[MAX*2];
bool got[MAX*2];
int t[MAX*2];
void update(int u,int v,int c)
{
for (u+=MAX,v+=MAX;u<v;u>>=1,v>>=1)
{
if (u&1)t[u++]+=c;
if (v&1)t[--v]+=c;
}
}
int query(int u)
{
int sum=0;
for (u+=MAX;u>0;u>>=1)sum+=t[u];
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=n;i>=1;i--)
{
if (tab[i]<0)tab[i]=-tab[i]+n;
pos[tab[i]].pb(i);
}
for (int i=1;i<=n*2;i++)
{
//cout<<"ZAWARTOSC "<<i<<"\n";
//for (auto it:pos[i])cout<<it<<" ";
//cout<<"\n";
}
for (int i=1;i<=n-1;i++)
{
if (got[i])continue;
got[i]=true;
int wsk=tab[i];
if (wsk<n)wsk+=n;
else wsk-=n;
while (pos[wsk].back()<i)pos[wsk].pop_back();
got[pos[wsk].back()]=true;
int x=pos[wsk].back();
ans+=x-i-1+(wsk>tab[i])-query(x)+query(i-1);
update(x,n*2,1);
}
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... |