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>
using namespace std;
const int maxn = 1e5 + 7;
long long n, BIT[maxn];
queue<long long> q[maxn];
void Update(long long u, long long v)
{
for(; u < maxn; u += u & -u) BIT[u]+= v;
}
long long Get(long long u)
{
long long ans = 0;
for(; u > 0; u -= u & -u) ans += BIT[u];
return ans;
}
long long count_swaps(vector<int> vt)
{
long long ans = 0;
n = vt.size() / 2;
for(long long i = 0; i < n * 2; i++)
{
if(vt[i] > 0)
{
q[vt[i]].push(i);
}
Update(i + 1, 1);
}
long long cnt = 1;
for(long long i = 0; i < n * 2; i++)
{
if(vt[i] < 0)
{
long long gau = Get(i + 1);
ans += gau - cnt;
cnt++;
Update(1, 1);
Update(i + 1, -1);
long long posr = q[-vt[i]].front();
q[-vt[i]].pop();
long long meo = Get(posr + 1);
ans += meo - cnt;
cnt++;
Update(1, 1);
Update(posr + 1, -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... |