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
using namespace std;
ll BIT[200009];
ll cnt[200009];
vector<ll> A[200009],B[200009];
ll N;
void add(ll p,ll v)
{
while(p <= N)
{
BIT[p] += v;
p += (p & (-p));
}
}
ll sum(ll p)
{
ll ret = 0;
while(p > 0)
{
ret += BIT[p];
p -= (p & (-p));
}
return ret;
}
ll count_swaps(vector<int> S)
{
ll ans = 0,i;
N = S.size();
for(i = 1;i <= N;i++)
{
add(i,1);
if(S[i-1] > 0)
{
A[S[i-1]].push_back(i);
}
else
{
B[-S[i-1]].push_back(i);
}
}
for(i = 1;i <= N;i++)
{
ll nw = cnt[abs(S[i-1])];
if((S[i-1] < 0 && B[abs(S[i-1])][nw] != i) || (S[i-1] > 0 && A[abs(S[i-1])][nw] != i))
{
continue;
}
cnt[abs(S[i-1])]++;
ll pr;
if(S[i-1] < 0)
{
pr = A[abs(S[i-1])][nw];
}
else
{
pr = B[abs(S[i-1])][nw];
ans++;
}
ans += sum(pr)-2;
add(i,-1);
add(pr,-1);
}
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... |