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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
int seg[400005];
ll ans;
bool ed[400005];
vector<int> s;
queue<int> has[400005];
tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ft[400005];
inline int ls(int x) { return x & -x; }
pair<int, int> qry(int p, int x) {
pair<int, int> r = make_pair(0, 0);
p = s.size() - p + 2;
for (; p; p -= ls(p)) {
r.first += ft[p].order_of_key(x);
r.second += ft[p].size() - ft[p].order_of_key(x);
}
return r;
}
void upd(int p, int v) {
p = s.size() - p + 2;
for (; p <= s.size(); p += ls(p)) ft[p].insert(v);
}
ll count_swaps(vector<int> _s) {
s = _s;
for (int i = 0; i < s.size(); i++) {
if (!has[-s[i] + s.size() / 2].empty()) {
seg[i] = has[-s[i] + s.size() / 2].front();
ed[i] = 1;
if (s[i] < 0) ans++;
has[-s[i] + s.size() / 2].pop();
} else has[s[i] + s.size() / 2].push(i);
}
for (int i = 0; i < s.size(); i++) {
if (!ed[i]) continue;
pair<int, int> tmp = qry(seg[i] + 1, seg[i]);
upd(i, seg[i]);
ans += (ll)tmp.first + (ll)tmp.second * 2ll;
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'void upd(int, int)':
shoes.cpp:31:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (; p <= s.size(); p += ls(p)) ft[p].insert(v);
| ~~^~~~~~~~~~~
shoes.cpp: In function 'll count_swaps(std::vector<int>)':
shoes.cpp:36:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
shoes.cpp:44:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i = 0; i < s.size(); 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... |