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 <iostream>
#include <vector>
#include <queue>
#include "shoes.h"
using namespace std;
int n, a, b;
long long ans = 0;
vector <queue <int>> v(1e5 + 1);
vector <int> p(2e5, -1);
vector <int> fen(2e5, 0);
void update(int r) {
for (int i = r; i >= 0; i = (i & (i + 1)) - 1) fen[i]++;
}
int query(int x) {
int ret = 0;
for (int i = x; i < 2 * n; i = (i | (i + 1))) ret += fen[i];
return ret;
}
long long count_swaps(vector <int> S) {
n = S.size() / 2;
for (int i = 0; i < 2 * n; i++) {
if (!v[abs(S[i])].size() || S[i] == S[v[abs(S[i])].front()]) {
v[abs(S[i])].push(i);
continue;
}
p[v[abs(S[i])].front()] = i;
v[abs(S[i])].pop();
}
for (int i = 0; i < 2 * n; i++) {
if (p[i] == -1) continue;
a = i + query(i), b = p[i] + query(p[i]);
ans += b - a - 1;
if (S[i] > 0) ans++;
update(p[i]);
}
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... |