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 ll long long
using namespace std;
struct Segtree {
int n;
vector<int> st;
Segtree(int N) {
n = N;
st = vector<int>(4*n, 0);
}
void change(int p, int l, int r, int i) {
if (l == r) st[p] = 1-st[p];
else {
int m = (l+r)/2;
if (i <= m) change(2*p, l, m, i);
else change(2*p+1, m+1, r, i);
st[p] = st[2*p] + st[2*p+1];
}
}
int sum(int p, int l, int r, int i, int j) {
if (i > j) return 0;
if (l == i && r == j) return st[p];
int m = (l+r)/2;
return sum(2*p, l, m, i, min(m, j)) + sum(2*p+1, m+1, r, max(m+1, i), j);
}
void change(int i) { change(1, 0, n-1, i); }
int sum(int i, int j) { return sum(1, 0, n-1, i, j); }
};
ll count_swaps(vector<int> s) {
int n = (int)s.size();
Segtree St(n);
ll ans = 0;
map<int, int> pos;
for (int i = 0; i < n; ++i) {
if (s[i] < 0) {
if (pos.find(-s[i]) != pos.end()) {
s[i] *= -1;
ans += i - pos[s[i]];
St.change(pos[s[i]]);
ans -= St.sum(pos[s[i]], i);
pos.erase(s[i]);
} else {
pos[s[i]] = i;
St.change(i);
}
} else {
if (pos.find(-s[i]) != pos.end()) {
s[i] *= -1;
ans += i - pos[s[i]] - 1;
St.change(pos[s[i]]);
ans -= St.sum(pos[s[i]], i);
pos.erase(s[i]);
} else {
pos[s[i]] = i;
St.change(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... |