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;
const int mxN = 2e5 + 1;
int seg[4 * mxN];
int n;
void update(int tar, int l = 0, int r = n * 4 - 1, int idx = 1) {
if (l == r) {
seg[idx] ^= 1;
return;
}
int mid = (l + r) >> 1;
if (tar <= mid) update(tar, l, mid, (idx << 1));
else update(tar, mid + 1, r, (idx << 1) + 1);
seg[idx] = seg[(idx << 1)] + seg[(idx << 1) + 1];
return;
}
int query(int tarl, int tarr, int l = 0, int r = n * 4 - 1, int idx = 1) {
if (tarl <= l && r <= tarr) return seg[idx];
else if (tarr < l || r < tarl) return 0;
int mid = (l + r) >> 1;
return query(tarl, min(tarr, mid), l, mid, (idx << 1)) + query(max(tarl, mid + 1), tarr, mid + 1, r, (idx << 1) + 1);
}
long long count_swaps(vector<int> s) {
n = s.size() / 2;
queue<int> q[n + 1];
for (int i = 0; i < n * 2; i++) {
if (s[i] < 0) q[-s[i]].push(i * 2 + 1);
}
for (int i = 0; i < n * 2; i++) update(i * 2 + 1);
long long ans = 0;
for (int i = 0; i < n * 2; i++) {
if (s[i] < 0) continue;
int tar = q[s[i]].front(); q[s[i]].pop();
if (i * 2 + 1 < tar) ans += query(i * 2 + 1, tar - 1);
else ans += query(tar + 1, i * 2);
update(tar);
update(i * 2);
}
return ans;
}
# | 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... |