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>
using namespace std;
using i64 = long long;
#define lch id << 1
#define rch id << 1 | 1
const int N = 2e5 + 5;
int bit[N];
int n;
void update(int x, int k) {
for(; x <= n; x += (x & -x))
bit[x] += k;
}
int calc(int x) {
int res = 0;
for(; x; x -= (x & -x))
res += bit[x];
return res;
}
int get(int x) {
return calc(x) - calc(x - 1);
}
i64 count_swaps(vector<int> a) {
i64 ans = 0;
map<int, deque<int>> res;
n = a.size();
for(int i = 0; i < n; i++) {
if(res[-a[i]].empty())
res[a[i]].emplace_back(i);
else {
int l = res[-a[i]].front() + 1;
res[-a[i]].pop_front();
int r = i + 1;
ans += (r - l) - (get(r) - get(l)) - (a[i] > 0);
update(l, 1);
update(r, -1);
}
}
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... |