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];
queue<int> q[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()/2;
for(int i=n+n;i>=1;i--){
if(q[-s[i-1]+n].empty()) q[s[i-1]+n].push(i);
else pos[i] = q[-s[i-1]+n].front(), q[-s[i-1]+n].pop();
}
long long ans = 0;
for(int i=1;i<=n+n;i++){
if(pos[i] == 0) continue;
int cur_pos1 = i + query(n+n) - query(i);
int cur_pos2 = pos[i] + query(n+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... |