Submission #544135

#TimeUsernameProblemLanguageResultExecution timeMemory
544135krit3379Arranging Shoes (IOI19_shoes)C++17
100 / 100
71 ms20336 KiB
#include<bits/stdc++.h> using namespace std; #define N 400005 int fen[N],cnt[N]; long long ans; vector<int> v[N]; bitset<N> vis; void update(int x,int k){ while(x<N){ fen[x]+=k; x+=x&-x; } } int sol(int x){ int ans=0; while(x){ ans+=fen[x]; x-=x&-x; } return ans; } long long count_swaps(vector<int> s){ int n,i,now,nxt,pos; n=s.size(); s.insert(s.begin(),0); for(i=1;i<=n;i++)fen[i]=i&-i; for(i=1;i<=n;i++)v[(s[i]>0)?s[i]:-s[i]+n].push_back(i); for(i=1;i<=n;i++){ if(vis[i])continue; if(s[i]>0)now=s[i],nxt=s[i]+n; else now=-s[i]+n,nxt=-s[i]; cnt[now]++; pos=v[nxt][cnt[nxt]++]; ans+=sol(pos)-sol(i); if(s[i]<0)ans--; update(i,1); update(pos,-1); vis[i]=vis[pos]=true; } 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...