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"
typedef long long ll;
using namespace std;
const int N=100005;
int n;
queue<int> ql[N],qr[N];
int bit[N];
void update(int x,int v){for(;x;x-=(x&(-x))) bit[x]+=v;}
int get(int x){int ans=0;for(;x<=n;x+=(x&(-x))) ans+=bit[x];return ans;}
long long count_swaps(std::vector<int> s) {
ll ans=0;
n=(int)s.size();
for(int i=0;i<(int)s.size();i++)
{
if(s[i]<0)
{
s[i]*=-1;
if(qr[s[i]].empty()) ql[s[i]].push(i+1);
else
{
int x=qr[s[i]].front();
x+=get(x);
//cout << i << ": " << x << ' ' << y << endl;
ans+=i-x+1;
update(i+1,1);
update(x-1,-1);
qr[s[i]].pop();
}
}
else
{
if(ql[s[i]].empty()) qr[s[i]].push(i+1);
else
{
int x=ql[s[i]].front();
x+=get(x);
//cout << i << ": " << x << ' ' << y << endl;
ans+=i-x;
update(i+1,1);
update(x-1,-1);
ql[s[i]].pop();
}
}
}
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... |