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<queue>
#include "shoes.h"
using namespace std;
const int nax = (int)1e6+1;
int n;
struct Segtree{
int t[nax * 3];
void built(int l = 0, int r = n - 1, int k = 1){
if (l == r){
t[k] = 1;
return;
}
int mid = (l+r)/2;
built(l,mid,k*2);
built(mid+1,r,k*2+1);
t[k] = t[k * 2] + t[k * 2 + 1];
}
void trigger(int pos, int l = 0, int r = n - 1, int k = 1){
if (l == r){
t[k] = 0;
return;
}
int mid = (l+r) / 2;
if (pos <= mid) trigger(pos,l,mid,k*2);
else trigger(pos,mid+1,r,k*2+1);
t[k] = t[k * 2] + t[k * 2 + 1];
}
int range_get(int a, int b, int l = 0, int r = n - 1, int k = 1){
if (a > b)return 0;
if (l >= a && r <= b)return t[k];
if(l > b || r < a)return 0;
int mid = (l+r)/2;
return range_get(a,b,l,mid,k*2)+range_get(a,b,mid+1,r,k*2+1);
}
} st;
long long count_swaps(std::vector<int> s) {
n = (int)s.size();
st.built();
vector<int>used(n+1,0);
queue<int>Left[n+1];
queue<int>Right[n+1];
for (int i = 0; i < n; i++){
if (s[i] < 0){
Left[-s[i]].push(i);
}else{
Right[s[i]].push(i);
}
}
long long ans = 0;
for (int i = 0; i < n; i++){
if (used[i])continue;
if (s[i] < 0){
//left
Left[-s[i]].pop();
assert(Right[s[i]*(-1)].size()>0);
int R = Right[-s[i]].front();
Right[-s[i]].pop();
st.trigger(R);
used[i]=1;
used[R]=1;
ans += st.range_get(i+1,R);
st.trigger(i);
}else{
//right
Right[s[i]].pop();
assert(Left[s[i]].size()>0);
int L = Left[s[i]].front();
Left[s[i]].pop();
st.trigger(L);
used[i]=1;
used[L]=1;
ans += st.range_get(i,L);
//printf("query : %d\n", st.range_get(i,L));
st.trigger(i);
}
//printf("%lld\n", ans);
}
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... |