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 "shoes.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 55;
ll t[N];
void upd (int i, int v) {
while (i < N) {
t[i] += v;
i |= (i + 1);
}
}
ll getP (int i) {
ll res = 0;
while (i >= 0) {
res += t[i];
i &= (i + 1);
--i;
}
return res;
}
ll get (int l, int r) {
if (l > r) return 0;
// assert (l <= r);
return getP (r) - getP (l - 1);
}
vector <int> v[N * 2];
ll count_swaps (vector<int> s) {
int n = s.size();
for (int i = n - 1 ; i >= 0 ; -- i) {
upd (i, 1);
v[s[i] + N].push_back (i);
}
ll ans = 0;
for (int i = 0 ; i < n ; ++ i) {
if (get (i, i) == 0) continue;
int x = s[i];
int y = -s[i];
assert (v[x + N].size() && v[y + N].size());
int j = v[y + N].back();
assert (i < j);
v[y + N].pop_back();
assert (v[x + N].back() == i);
v[x + N].pop_back();
ans += get (i + 1, j - 1);
if (x > 0) ans++;
upd (j, -1);
upd (i, -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... |