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;
int pos[200002], t[200002], cur[200002];
int query(int i){
int ans = 0;
for(;i>0;i-=(i&-i)) ans += t[i];
return ans;
}
void update(int i){
for(;i<=200000;i+=(i&-i)) t[i]++;
}
long long count_swaps(vector<int> s){
int n = s.size();
for(int i=n;i>=1;i--){
int v = s[i-1];
if(v < 0) v = -v;
if(cur[v] == 0) cur[v] = i;
else pos[i] = cur[v], cur[v] = 0;
}
long long ans = 0;
for(int i=1;i<=n;i++){
if(pos[i] == 0) continue;
int cur_pos1 = i + query(2*n) - query(i);
int cur_pos2 = pos[i] + query(2*n) - query(pos[i]);
ans += cur_pos2 - cur_pos1 - 1;
if(s[i-1] > 0) ans++;
update(pos[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... |