# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1030785 | Zicrus | Arranging Shoes (IOI19_shoes) | C++17 | 785 ms | 15196 KiB |
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>
#include "shoes.h"
using namespace std;
typedef long long ll;
ll n;
ll pow2;
vector<ll> seg;
void segSet(ll id, ll val) {
id += pow2;
seg[id] = val;
id /= 2;
while (id) {
seg[id] = seg[2*id] + seg[2*id+1];
id /= 2;
}
}
ll segGet(ll low, ll high) {
low += pow2; high += pow2;
ll ans = 0;
while (low <= high) {
if (low & 1) ans += seg[low++];
if (!(high & 1)) ans += seg[high--];
low /= 2; high /= 2;
}
return ans;
}
ll count_swaps(vector<int> a) {
n = a.size() / 2;
vector<vector<ll>> seenQNeg(n+10);
vector<vector<ll>> seenQPos(n+10);
ll res = 0;
pow2 = 2ll << (ll)ceil(log2(n));
seg = vector<ll>(2*pow2);
for (int i = 0; i < a.size(); i++) {
auto &q = a[i] < 0 ? seenQNeg[-a[i]] : seenQPos[a[i]];
auto &pairQ = a[i] > 0 ? seenQNeg[a[i]] : seenQPos[-a[i]];
if (pairQ.empty()) {
q.insert(q.begin(), i);
}
else {
ll id = pairQ.back();
pairQ.pop_back();
if (a[i] < 0) {
res += abs(id - i) + segGet(id, i);
segSet(i, -1);
segSet(id, 1);
}
else {
res += abs(id - i) - 1 + segGet(id, i);
segSet(i, -1);
segSet(id, 1);
}
}
}
return res;
}
Compilation message (stderr)
# | 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... |