This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// M
#include<bits/stdc++.h>
#include "shoes.h"
using namespace std;
const int N = 100005 * 2;
int M[N], F[N];
vector < int > V[N][2];
inline void Add(int i, int val)
{
for (i = N - i - 5; i < N; i += i & - i)
F[i] += val;
}
inline int Get(int i)
{
int rt = 0;
for (i = N - i - 5; i; i -= i & - i)
rt += F[i];
return rt;
}
long long count_swaps(vector < int > A)
{
int n = (int)A.size() / 2;
for (int i = 0; i < n + n; i ++)
V[abs(A[i])][A[i] > 0].push_back(i);
int m = 0;
for (int i = 1; i <= n; i ++)
{
assert(V[i][0].size() == V[i][1].size());
for (int j = 0; j < (int)V[i][0].size(); j ++)
m ++, A[V[i][0][j]] = -m, A[V[i][1][j]] = m;
}
long long tot = 0;
memset(M, -1, sizeof(M));
for (int i = 0; i < n + n; i ++)
{
if (M[abs(A[i])] == -1)
Add(i, 1), M[abs(A[i])] = i;
else
{
if (A[i] < 0)
tot ++;
tot += Get(M[abs(A[i])]) - 1;
Add(i, 1);
}
}
return tot;
}
# | 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... |