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;
#include "shoes.h"
using ll = long long;
struct Seg {
int i, j;
int s = 0;
Seg *l, *r;
Seg(int i, int j): i(i), j(j) {
if (j - i == 1) {
l = r = nullptr;
} else {
int k = i + j >> 1;
l = new Seg(i, k);
r = new Seg(k, j);
}
}
void inc(int I) {
if (i <= I and I < j) {
s++;
if (l) l->inc(I), r->inc(I);
}
}
int sum(int I) {
return j <= I ? s : I <= i ? 0 : l->sum(I) + r->sum(I);
}
};
ll count_swaps(vector<int> _seq) {
vector<int> found(_seq.size() * 2 + 5);
vector<ll> seq;
for (int v : _seq) seq.push_back(ll(v) * _seq.size() + ((v > 0) - (v < 0)) * found[v + _seq.size()]++);
unordered_map<ll,int> pos; pos.max_load_factor(0.5);
for (int i = 0; i < seq.size(); i++) pos[seq[i]] = i;
sort(seq.rbegin(), seq.rend(), [&](ll i, ll j) {
int d = min(pos[i], pos[-i]) - min(pos[j], pos[-j]);
return d ? d < 0 : i < j;
});
for (ll& v : seq) v = pos[v];
ll ans = 0;
Seg s(0, seq.size());
for (ll v : seq) ans += s.sum(v), s.inc(v);
return ans;
}
Compilation message (stderr)
shoes.cpp: In constructor 'Seg::Seg(int, int)':
shoes.cpp:14:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
14 | int k = i + j >> 1;
| ~~^~~
shoes.cpp: In function 'll count_swaps(std::vector<int>)':
shoes.cpp:35:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int i = 0; i < seq.size(); i++) pos[seq[i]] = i;
| ~~^~~~~~~~~~~~
# | 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... |