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;
ll tree[200100];
ll tam;
void update(ll pos,ll val){
while(pos<=tam){
tree[pos]+=val;
pos+=(pos&-pos);
}
}
ll query(ll pos){
ll curr=0;
while(pos>0){
curr+=tree[pos];
pos-=(pos&-pos);
}
return curr;
}
unordered_map<ll,set<ll> > mp;
long long count_swaps(std::vector<int> s) {
tam=s.size();
bool vis[tam];
memset(vis,false,sizeof vis);
for(int i=0;i<tam;i++){
mp[s[i]].insert(i);
}
set<ll>::iterator it,it1;
ll ans=0;
for(int i=0;i<tam-1;i++){
if(vis[i])continue;
vis[i]=true;
mp[s[i]].erase(i);
if(s[i]>0){
it=mp[(s[i]*-1)].begin();
mp[s[i]*-1].erase(it);
ll aux=*it;
ll curr=i+query(i+1ll);
ll curr1=aux+query(aux+1ll);
ll currans=curr1-curr;
vis[aux]=true;
update(i+1,1);
update(aux+1,-1);
ans+=currans;
}
else{
it=mp[s[i]*-1].begin();
mp[s[i]*-1].erase(it);
ll aux=*it;
ll curr=i+query(i+1ll);
ll curr1=aux+query(aux+1ll);
ll currans=curr1-(curr+1);
ans+=currans;
vis[aux]=true;
//cout<<currans<<endl;
if(currans!=0){
update(i+2,1);
update(aux+1,-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... |