#include <bits/stdc++.h>
#include "shoes.h"
using namespace std;
const int mxN = 2e5 + 100;
int seg[mxN * 4], lazy[mxN * 4];
void build(int node, int start, int end){
if(start == end){
seg[node] = start;
return;
}
int mid = start + (end - start) / 2;
build(node * 2 + 1, start, mid);
build(node * 2 + 2, mid + 1, end);
}
void push(int node, int start, int end){
if(start == end){
seg[node] += lazy[node];
lazy[node] = 0;
return;
}
seg[node] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
lazy[node * 2 + 2] += lazy[node];
lazy[node] = 0;
}
int query(int node, int start, int end, int idx){
push(node, start, end);
if(start == end) return seg[node];
int mid = start + (end - start) / 2;
if(idx <= mid) return query(node * 2 + 1, start, mid, idx);
return query(node * 2 + 2, mid + 1, end, idx);
}
void upd(int node, int start, int end, int l, int r){
push(node, start, end);
if(start < l || end > r) return;
if(start >= l && end <= r){
lazy[node] = 1;
push(node, start, end);
return;
}
int mid = start + (end - start) / 2;
upd(node * 2 + 1, start, mid, l, r);
upd(node * 2 + 2, mid + 1, end, l, r);
seg[node] = seg[node * 2 + 1] + seg[node * 2 + 2];
}
long long count_swaps(std::vector<int> s) {
int n = (int) s.size();
long long ans = 0;
set<int> pos, neg;
build(0, 0, n - 1);
for(int i = 0; i < n; ++i){
if(s[i] < 0) neg.insert(i);
else pos.insert(i);
}
for(int i = 0; i < n; ++i){
auto lbn = neg.lower_bound(s[i]);
auto lbp = pos.lower_bound(s[i]);
if(*lbn != s[i] && *lbp != s[i]) continue;
if(s[i] > 0){
int tp = *neg.begin();
int org = query(0, 0, n - 1, tp);
ans += query(0, 0, n - 1, i) - org;
upd(0, 0, n - 1, i + 1, tp - 1);
neg.erase(tp);
}else{
int tp = *pos.begin();
int org = query(0, 0, n - 1, tp);
ans += query(0, 0, n - 1, i) - org - 1;
upd(0, 0, n - 1, i, tp);
pos.erase(tp);
}
}
return ans;
}
/*
int main(){
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
cout << count_swaps({-2, 2, 2, -2, -2, 2}) << endl;
//count_swaps({-1, -2, 1, 2});
return 0;
}*/
# | 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... |