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;
ll count_swaps(vector<int> s) {
int n = s.size() / 2;
vector<int> bit(2 * n + 1);
auto add = [&](int p, int v) {
for (int i = p + 1; i <= 2 * n; i += i & -i) {
bit[i] += v;
}
};
auto sum = [&](int p) {
int ans = 0;
for (int i = p; i > 0; i -= i & -i) {
ans += bit[i];
}
return ans;
};
ll ans = 0;
queue<int> a[n + 1][2];
for (int i = 0; i < 2 * n; i++) {
int q = s[i] > 0;
if (s[i] < 0) {
s[i] = -s[i];
}
if (!a[s[i]][!q].empty()) {
int p = a[s[i]][!q].front();
a[s[i]][!q].pop();
ans += (i - p - 1) - (sum(i) - sum(p)) + !q;
add(i, 1);
add(p, -1);
} else {
a[s[i]][q].push(i);
}
}
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... |