Submission #659059

#TimeUsernameProblemLanguageResultExecution timeMemory
659059BananArranging Shoes (IOI19_shoes)C++17
50 / 100
335 ms296648 KiB
#include "shoes.h" #include <bits/stdc++.h> using namespace std; using ll = long long; #define double long double #define endl '\n' #define sz(a) (int)a.size() #define pb push_back #define fs first #define sc second #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() ll tree[400040], a[100005]; void update(int v, int l, int r, int pos) { if(l==r) { tree[v]=a[l]; } else { int mid=(l+r)/2; if(pos<=mid) { update(2*v, l, mid, pos); } else { update(2*v+1, mid+1, r, pos); } tree[v]=tree[2*v]+tree[2*v+1]; } } int ask(int v, int l, int r, int sl, int sr) { if(sr<l || sl>r){return 0;} if(l==sl && r==sr){return tree[v];} int mid=(l+r)/2; int v1=ask(2*v, l, mid, sl, min(mid, sr)); int v2=ask(2*v+1, mid+1, r, max(mid+1, sl), sr); return v1+v2; } long long count_swaps(vector<int> s) { ll n=sz(s); map<ll, queue<ll>> m; vector<bool> mark(n+5, 0); for(ll i=0;i<n;i++) { m[s[i]].push(i); } ll ans=0; for(ll i=0;i<n;i++) { if(!mark[i]) { if(s[i]<0) { int pos=m[-s[i]].front(); int v=ask(1, 1, 2*n, i+1, pos+1); ans+=(pos-i-1+v); mark[pos]=1; mark[i]=1; m[-s[i]].pop(); m[s[i]].pop(); a[pos+1]=-1; update(1, 1, 2*n, pos+1); } else { int pos=m[-s[i]].front(); int v=ask(1, 1, 2*n, i+1, pos+1); ans+=(pos-i+v); mark[pos]=1; mark[i]=1; m[-s[i]].pop(); m[s[i]].pop(); a[pos+1]=-1; update(1, 1, 2*n, pos+1); } } } 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...