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;
constexpr int maxn = 2e5, bits = 18, siz = 1 << bits;
int segtree[siz * 2] = { 0 };
void update(int p)
{
while (p > 0)
{
segtree[p]++;
p /= 2;
}
}
ll query(int pos, int l, int r, int gl, int gr)
{
if (l > gr || gl > r) return 0;
if (gl <= l && r <= gr) return segtree[pos];
int mid = (l + r) >> 1;
return query(pos * 2, l, mid, gl, gr) + query(pos * 2 + 1, mid + 1, r, gl, gr);
}
int n;
// Shoe types
set <int> shoes[maxn / 2 + 5][2];
ll count_swaps(vector<int> s) {
n = s.size();
ll output = 0;
for (int i = 0; i < n; i++)
{
if (s[i] > 0) shoes[s[i]][1].insert(i); // Right
else shoes[abs(s[i])][0].insert(i); // Left
}
ll typ, next_pair;
for (ll i = 0; i < n; i++)
{
if (segtree[i + siz]) continue;
typ = (s[i] > 0);
shoes[abs(s[i])][typ].erase(i);
next_pair = *shoes[abs(s[i])][(typ + 1) & 1].begin();
shoes[abs(s[i])][(typ + 1) & 1].erase(next_pair);
// cout << i << " " << next_pair << " " << output << " ";
// cout << query(1, 0, siz - 1, i, next_pair) << "\n";
output += next_pair - i - query(1, 0, siz - 1, i, next_pair) + typ - 1;
// update(i + siz);
update(next_pair + siz);
}
return output;
}
# | 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... |