# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
420457 | QCFium | Arranging Shoes (IOI19_shoes) | C++14 | 363 ms | 27088 KiB |
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>
int ri() {
int n;
scanf("%d", &n);
return n;
}
struct SegTree {
int n;
std::vector<int> data;
SegTree (int n_) {
for (n = 1; n < n_; n <<= 1);
data.resize(n << 1);
for (int i = 0; i < n_; i++) data[i + n] = 1;
for (int i = n; --i; ) data[i] = data[i << 1] + data[i << 1 | 1];
}
void add(int i, int val) {
for (i += n; i; i >>= 1) data[i] += val;
}
int sum(int l, int r) {
int res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (r & 1) res += data[--r];
if (l & 1) res += data[l++];
}
return res;
}
};
long long count_swaps(std::vector<int> a) {
int n = a.size();
std::map<int, std::vector<int> > index;
for (int i = 0; i < n; i++) index[a[i]].push_back(i);
for (auto &i : index) std::reverse(i.second.begin(), i.second.end());
SegTree tree(n);
std::vector<bool> alive(n, true);
int res = 0;
for (int i = 0; i < n; i++) {
if (!alive[i]) continue;
int t = index[-a[i]].back();
index[-a[i]].pop_back();
index[a[i]].pop_back();
alive[i] = false;
alive[t] = false;
tree.add(i, -1);
tree.add(t, -1);
res += tree.sum(i, t);
if (a[i] > 0) res++;
}
return res;
}
Compilation message (stderr)
# | 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... |