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