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;
#define dbg(x) cerr << #x << ": " << x << endl;
struct FenwickTree {
vector<int> tree;
int size;
FenwickTree(int n) {
size = n;
tree.resize(size);
}
void update(int pos, int value) {
for (int i = pos; i < size; i |= i + 1) {
tree[i] += value;
}
}
int query(int l, int r) {
int res = 0;
for (int i = r; i >= 0; i = (i & (i + 1)) - 1) {
res += tree[i];
}
for (int i = l - 1; i >= 0; i = (i & (i + 1)) - 1) {
res -= tree[i];
}
return res;
}
};
ll count_swaps(vector<int> a) {
int n = a.size() / 2;
vector<vector<int>> left_elms(n + 1), right_elms(n + 1);
for (int i = 0; i < n * 2; ++i) {
int x = a[i];
if (x < 0) {
left_elms[-x].push_back(i);
} else {
right_elms[x].push_back(i);
}
}
vector<pair<int, int>> pairs;
ll res = 0;
for (int x = 1; x <= n; ++x) {
for (int i = 0; i < (int)left_elms[x].size(); ++i) {
int l = left_elms[x][i];
int r = right_elms[x][i];
if (l > r) {
swap(l, r);
++res;
}
pairs.emplace_back(l, r);
}
}
sort(pairs.begin(), pairs.end());
vector<int> p(n * 2);
for (int i = 0; i < n; ++i) {
auto [l, r] = pairs[i];
p[l] = 2 * i;
p[r] = 2 * i + 1;
}
FenwickTree tree(n * 2);
for (auto x : p) {
res += tree.query(x + 1, n * 2 - 1);
tree.update(x, 1);
}
return res;
}
# | 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... |