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 <vector>
#include <cstdint>
#include <iostream>
#include <algorithm>
#define N 200005
int fw[N], posraw[N<<1];
int *pos = posraw+N;
void upd(int p, int k) { for (++p; p < N; p+=p&-p) fw[p] += k; }
int qry(int p) { if(p<0)return 0;int z = 0; for (++p; p; p-=p&-p) z += fw[p]; return z; }
int64_t count_swaps(std::vector<int> v)
{
int64_t z = 0;
int n = v.size() >> 1, f = 0;
for (int i = 0; i < (int)v.size(); ++i) pos[v[i]] = i;
for (int i = 0; i < 2 * n; ++i, ++f)
{
if (qry(i) - qry(i-1) == 1) continue;
int corresponding = pos[-v[i]] + (qry(2*n) - qry(pos[-v[i]]));
z += corresponding - i*2 - 1 + (v[i] > 0);
upd(pos[-v[i]], 1);
}
return z;
}
#if 0
int main()
{
std::cout << count_swaps({2, 1, -1, -2});
}
#endif
# | 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... |