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>
#include "shoes.h"
using namespace std;
#define ll long long
int n, BIT[100005];
queue<int> l[100005], r[100005];
void upd(ll val, int idx){
for(int i = idx; i <= n; i += (i & -i)) BIT[i] += val;
}
ll qry(int idx){
ll ans = 0;
for(int i = idx; i > 0; i -= (i & -i)) ans += BIT[i];
return ans;
}
ll count_swaps(vector<int> s){
n = s.size();
ll ans = 0;
for(int i = 1; i <= n; i++){
int sz = s[i - 1];
if(sz < 0){
sz *= -1;
if(!r[sz].empty()){
int need = r[sz].front(); r[sz].pop();
ans += qry(i) - qry(need - 1);
upd(1, need);
}else {
l[sz].push(i);
upd(1, i);
}
}else{
if(!l[sz].empty()){
int need = l[sz].front(); l[sz].pop();
ans += qry(i) - qry(need - 1);
upd(1, need);
}else{
r[sz].push(i);
upd(1, 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... |