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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
// segtree or pbds ig
vector<ll> seg;
void psh(ll v){
seg[v*2] += seg[v];
seg[v*2+1] += seg[v];
seg[v]=0;
}
void update(ll v,ll l,ll r,ll tl,ll tr){
if(l > r) return;
if(tl==l && tr == r) {
seg[v]++;
return;
}
ll tm=(tl+tr)/2;
update(v*2,l,min(r,tm),tl,tm);
update(v*2+1,max(l,tm+1),r,tm+1,tr);
}
ll query(ll v,ll tl,ll tr,ll s){
if(tl==tr && tl==s) return seg[v];
psh(v);
ll tm=(tl+tr)/2;
if(s <= tm) return query(v*2,tl,tm,s);
else return query(v*2+1,tm+1,tr,s);
}
long long count_swaps(std::vector<int> s) {
set<pair<ll,ll>> st; // val, pos
ll cnt=0,n=s.size();
seg.resize(4*(n+10),0);
for(ll i = 0;i < s.size();i++){
auto lb = st.lower_bound({-s[i],-1});
if(lb == st.end() || lb->first!=-s[i]){
st.insert({s[i],i});
continue;
}
auto p = *lb;
st.erase(lb);
ll add = query(1,1,n,p.second+1);
cnt+=i-(p.second+add)-1;
update(1,p.second+1,i+1,1,n);
if(s[i]<0) cnt++;
}
return cnt;
}
/*
*/
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:43:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(ll i = 0;i < s.size();i++){
| ~~^~~~~~~~~~
# | 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... |