Submission #433077

#TimeUsernameProblemLanguageResultExecution timeMemory
433077Pichon5Arranging Shoes (IOI19_shoes)C++17
100 / 100
606 ms31684 KiB
#include "shoes.h" #include <bits/stdc++.h> #define pb push_back #define vi vector<int> #define F first #define S second #define ll long long using namespace std; const int tam=200005; ll T[tam]; int n; void update(int pos, int val){ while(pos<=n){ T[pos]+=val; pos+=(pos&-pos); } } ll query(int pos){ ll res=0; while(pos>0){ res+=T[pos]; pos-=(pos&-pos); } return res; } long long count_swaps(vi s) { n=s.size(); map<int,set<int> >M; for(int i=1;i<=n;i++){ update(i,1); M[s[i-1]].insert(i); } ll ans=0; for(int i=0;i<n;i++){ auto it=M[s[i]].find(i+1); if(it==M[s[i]].end())continue; auto A=M[-s[i]].begin(); int b=i+1,e=*A; ll Q=query(e-1)-query(b); ans+=Q; if(s[i]>0)ans++; update(e,-1); M[-s[i]].erase(A); auto I = M[s[i]].begin(); M[s[i]].erase(I); } return ans; } //2
#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...