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>
using namespace std;
typedef long long ll;
ll fenvick[200020];
void fenvick_add(int x, int n, int val)
{
int x1=x;
while(x1<=n)
{
fenvick[x1]+=val;
x1+=((-x1)&x1);
}
}
ll fenvick_sum(int x)
{
int x1=x, rz=0;
while(x1>0)
{
rz+=fenvick[x1];
x1-=((-x1)&x1);
}
return rz;
}
ll count_swaps(vector<int>S)
{
int n=S.size()/2;
stack<int>leve[n+1], desne[n+1];
ll reez=0;
int poooz[n*2+1]={0};
for(int i=0; i<2*n; i++)
{
if(S[i]<0)leve[-S[i]].push(i+1);
else desne[S[i]].push(i+1);
}
for(int i=1; i<=n; i++)
{
while(!leve[i].empty())
{
if(desne[i].top()<leve[i].top())reez++;
poooz[leve[i].top()]=desne[i].top();
poooz[desne[i].top()]=leve[i].top();
desne[i].pop();
leve[i].pop();
}
}
bool check[2*n+1]={false};
for(int i=1; i<=2*n; i++)
{
if(!check[i])
{
fenvick_add(i, 2*n, 1);
fenvick_add(poooz[i], 2*n, 1);
check[i]=true;
check[poooz[i]]=true;
}
else if(check[i])
{
reez+=fenvick_sum(i-1)-fenvick_sum(poooz[i]);
fenvick_add(i, 2*n, -1);
fenvick_add(poooz[i], 2*n, -1);
reez+=fenvick_sum(poooz[i])*2;
}
}
return reez;
}
# | 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... |