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;
#define index int med=(s+e)/2,l=2*n+1,r=2*n+2
typedef long long ll;
const int tam=1e5+5;
unordered_map<int,priority_queue<int,vector<int>,greater<int>>> ma;
vector<int> tr(4*tam);
void init(int n, int s, int e){
if(s==e){
tr[n]=1;
return;
}
index;
init(l,s,med); init(r,med+1,e);
tr[n]=tr[s]+tr[r];
}
void update(int n, int s, int e, int pos, int val){
if(s==e){
tr[n]=val;
return;
}
index;
if(pos<=med) update(l,s,med,pos,val);
else update(r,med+1,e,pos,val);
tr[n]=tr[l]+tr[r];
}
int query(int n, int s, int e, int i, int j){
if(i>j) return 0;
if(i<=s && e<=j) return tr[n];
index;
if(j<=med) return query(l,s,med,i,j);
if(i>med) return query(r,med+1,e,i,j);
return query(l,s,med,i,j)+query(r,med+1,e,i,j);
}
long long count_swaps(std::vector<int> s) {
int n=s.size();
for(int i=0;i<n;i++) ma[s[i]].push(i);
init(0,0,n-1);
ll res=0;
vector<bool> vis(n,false);
for(int i=0;i<n;i++){
if(vis[i]) continue;
int j=ma[-s[i]].top();
ma[-s[i]].pop();
vis[j]=true;
int moves=query(0,0,n-1,i+1,j-1)+(s[i]>0);
res+=moves;
update(0,0,n-1,j,0);
//printf("%d : %d -> %d : %d\n",i,s[i],j,s[j]);
//printf("%d ; %d -> %d\n\n",i+1,j-1,moves);
}
return res;
}
# | 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... |