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>
#define flag(_a) cout << "Still alive at flag " << _a << '\n';
#define dbg(_a, _b) cout << "var " << _a << " -> " << _b << '\n';
#define dbg_itv(_l, _r, _val) cout << "itv (" << _l << ", " << _r << ") -> " << _val << '\n';
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 2;
ll n;
ll bit[maxn];
vector<queue<int> > L, R;
ll lb(ll x) {return x & (-x);}
void modify(ll x, ll val) {
for (; x <= n; x += lb(x)) {
bit[x] += val;
}
}
ll qry(ll x) {
ll sum = 0;
for (; x > 0; x -= lb(x)) {
sum += bit[x];
}
return sum;
}
ll count_swaps(vector<int> ss) {
vector<int> s = {0};
for (auto& i : ss) s.emplace_back(i);
n = (int)ss.size();
L.resize(n/2+1);
R.resize(n/2+1);
for (int i = 1; i <= n; i++) {
if (s[i] < 0) L[-s[i]].push(i);
else R[s[i]].push(i);
}
for (int i = 1; i <= n; i++) {
modify(i, 1);
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
// assert(qry(i+1));
if (qry(i)) {
int j = abs(s[i]);
int l = L[j].front(); L[j].pop();
int r = R[j].front(); R[j].pop();
if (r < l) ans += qry(l - 1) - qry(r - 1);
else ans += qry(r - 1) - qry(l);
modify(l, -1);
modify(r, -1);
}
}
/*
for (int i = 1; i <= n; i++) {
if (qry(i)) {
if (s[i] < 0) {
int lpos = i, rpos = R[-s[i]].front();
assert(L[-s[i]].front() == lpos);
L[-s[i]].pop(), R[-s[i]].pop();
ans += qry(rpos - 1) - qry(lpos);
modify(lpos, -1), modify(rpos, -1);
} else {
int lpos = L[s[i]].front(), rpos = i;
assert(R[s[i]].front() == rpos);
L[s[i]].pop(), R[s[i]].pop();
ans += qry(lpos - 1) - qry(rpos - 1);
modify(lpos, -1), modify(rpos, -1);
}
}
}
*/
return ans;
}
/*
2
2 1 -1 -2
*/
# | 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... |