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++) {
if (qry(i)) {
if (s[i] < 0) {
int lpos = i, rpos = R[-s[i]].front();
assert(1);
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(1);
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... |