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;
const int MAXN = 2e5+20;
int skip[MAXN];
queue <int> vec[2*MAXN];
int n;
ll ans;
int st[2*MAXN];
int que(int x){
int te = 0;
for(; x>0; x-=x&(-x)) te += st[x];
return te;
}
void upd(int x, int y){
for(; x<=2*MAXN-10; x+=x&(-x)) st[x]+=y;
}
long long count_swaps(vector<int> s) {
n = s.size();
for(int i=0; i<n; i++){
if(s[i] < 0) vec[MAXN-s[i]].push(i);
else vec[s[i]].push(i);
}
//cout << vec[2].front() << "p\n";
for(int i=0; i<n; i++){
if(skip[i]) continue;
if(s[i] < 0){
int idx = vec[-s[i]].front();
skip[idx] = 1;
ans += (ll)(idx-i-1 - (que(idx)-que(i)));
upd(idx, 1);
//cout << i << ' '<< idx << " idx\n";
vec[-s[i]].pop(); vec[MAXN-s[i]].pop();
} else {
int idx = vec[MAXN+s[i]].front();
skip[idx] = 1;
ans += (ll)(idx-i - (que(idx)-que(i)));
upd(idx, 1);
//cout << i << ' '<< idx << " idx\n";
vec[MAXN+s[i]].pop(); vec[s[i]].pop();
}
}
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... |