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];
stack<int> st[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(st[-s[i-1]+n].empty()) st[s[i-1]+n].push(i);
else pos[i] = st[-s[i-1]+n].top(), st[-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(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... |