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;
using int64 = long long;
vector<int> aib;
void update(int x, int N) {
for(int i = x; i <= N; i += i & -i)
++aib[i];
}
int query(int x) {
int ans = 0;
for(int i = x; i > 0; i -= i & -i)
ans += aib[i];
return ans;
}
int64 count_swaps(vector<int> s) {
int N = s.size();
vector<vector<int>> from_s(N + 1);
const int add = N / 2;
for(int i = 0; i < N; ++i)
from_s[s[i] + add].emplace_back(i);
vector<int> t, ptr(N + 1);
vector<bool> erased(N + 1);
for(int i = 0; i < N; ++i) {
if(erased[i])
continue;
int negative, positive;
if(s[i] > 0)
negative = add - s[i], positive = s[i] + add;
else
negative = s[i] + add, positive = add - s[i];
int left = from_s[negative][ptr[negative]++];
t.emplace_back(left);
erased[left] = true;
int right = from_s[positive][ptr[positive]++];
t.emplace_back(right);
erased[right] = true;
}
vector<vector<int>> from_t(N + 1);
for(int i = 0; i < N; ++i)
from_t[s[t[i]] + add].emplace_back(i);
vector<int> perm(N);
for(int i = 0; i <= N; ++i)
for(int j = 0; j < (int)from_s[i].size(); ++j) {
int poz = from_s[i][j];
perm[poz] = from_t[i][j] + 1;
}
int64 ans = 0;
aib.resize(N + 1);
for(int i = N - 1; i >= 0; --i) {
ans += query(perm[i] - 1);
update(perm[i], N);
}
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... |