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;
using ll = long long;
#define mp make_pair
#define F first
#define S second
#define pb push_back
const int N = 200010;
int st[4*N];
void Build(int node, int l, int r){
if(l>r)return;
if(l==r){
st[node]=1;
return;
}
int mid=(l+r)/2;
Build(2*node+1, l, mid);
Build(2*node+2, mid+1, r);
st[node]=st[2*node+1]+st[2*node+2];
}
void Update(int node, int l, int r, int ind, int val){
if(l>r || l>ind || r<ind)return;
if(l==r){
st[node]+=val;
return;
}
int mid=(l+r)/2;
Update(2*node+1, l, mid, ind, val);
Update(2*node+2, mid+1, r, ind, val);
st[node]=st[2*node+1]+st[2*node+2];
}
int Get(int node, int l, int r, int L, int R){
if(l>r || l>R || r<L)return 0;
else if(L<=l && R>=r){
return st[node];
}
int mid=(l+r)/2;
return Get(2*node+1, l, mid, L, R)+Get(2*node+2, mid+1, r, L, R);
}
long long count_swaps(std::vector<int> s) {
int n=s.size();
Build(0, 0, n-1);
ll ans=0;
set<pair<int, int>, less<pair<int, int>>> skup;
for(int i=0; i< n; i++){
if(skup.empty()){
skup.insert(mp(s[i], i));
continue;
}
auto ptr = skup.lower_bound(mp(-s[i], -1));
pair<int, int> p=*ptr;
if(ptr==skup.end()){
skup.insert(mp(s[i], i));
}
else if(p.F != -s[i]){
skup.insert(mp(s[i], i));
}
else{
if(s[i]<0)ans++;
ans+=Get(0, 0, n-1, p.S, i);
ans-=2;
Update(0, 0, n-1, i, -1);
Update(0, 0, n-1, p.S, 1);
skup.erase(skup.find(p));
}
}
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... |