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 <vector>
#include <set>
#define N 100005
#define pb push_back
#define ls p<<1
#define rs p<<1|1
#define vi std::vector<int>
#define rep(i, a, b) for(int i = a; i < b; i++)
int t[N << 2];
void build(int p, int l, int r){
if(l == r){
t[p] = 1;
return;
}
int m = (l + r) >> 1;
build(ls, l, m);
build(rs, m+1, r);
t[p] = t[ls] + t[rs];
}
void upd(int p, int l, int r, int x, int v){
t[p] += v;
if(l == r){
return;
}
int m = (l+r) >> 1;
if(x <= m)upd(ls, l, m, x, v);
else upd(rs, m+1, r, x, v);
}
int query(int p, int l, int r, int x, int y){
if(x > y)return 0;
if(l >= x && r <= y)return t[p];
int m = (l+r) >> 1, ans = 0;
if(x <= m)ans = query(ls, l, m, x, y);
if(y > m)ans += query(rs, m+1, r, x, y);
return ans;
}
std::set<int>b[N];
long long count_swaps(std::vector<int> s) {
int n = s.size();
build(1, 0, n-1);
rep(i, 1, n+1)b[i].clear();
vi a;
rep(i, 0, n){
if(s[i] < 0)a.pb(i);
else b[s[i]].insert(i);
}
long long ans = 0;
for(auto &x : a){
int i = -s[x], v;
auto it = b[i].lower_bound(x);
if(it != b[i].begin()){
it--;
}
int k = *it;
b[i].erase(it);
if(k < x){
v = query(1, 0, n-1, k+1, x-1);
ans += v + 1;
}
else{
v = query(1, 0, n-1, x+1, k-1);
ans += v;
}
upd(1, 0, n-1, k, -1);
}
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... |