Submission #1062890

#TimeUsernameProblemLanguageResultExecution timeMemory
1062890Ahmed57Arranging Shoes (IOI19_shoes)C++17
10 / 100
1 ms600 KiB
#include "bits/stdc++.h" using namespace std; int seg[400001]; void build(int p,int l,int r){ if(l==r){ seg[p] = 0; return ; } int md = (l+r)/2; build(p*2,l,md); build(p*2+1,md+1,r); seg[p] = seg[p*2]+seg[p*2+1]; } void update(int p,int l,int r,int idx,int val){ if(l==r){ seg[p]+=val; return ; } int md = (l+r)/2; if(idx<=md)update(p*2,l,md,idx,val); else update(p*2+1,md+1,r,idx,val); seg[p] = seg[p*2]+seg[p*2+1]; } int query(int p,int l,int r,int lq,int rq){ if(l>=lq&&r<=rq)return seg[p]; if(r<lq||l>rq)return 0; int md = (l+r)/2; return query(p*2,l,md,lq,rq)+query(p*2+1,md+1,r,lq,rq); } long long count_swaps(vector<int> s){ build(1,0,s.size()); int n = s.size(); map<int,stack<int>> v; long long ans = 0; for(int i = 0;i<n;i++){ if(v[-s[i]].size()==0){ v[s[i]].push(i);update(1,0,n,i,1);} else{ ans+=(i-query(1,0,n,0,v[-s[i]].top())); update(1,0,n,v[-s[i]].top(),1); v[-s[i]].pop(); if(s[i]<0)ans++; } } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...