Submission #1037090

#TimeUsernameProblemLanguageResultExecution timeMemory
1037090huutuanAncient Books (IOI17_books)C++14
50 / 100
137 ms183308 KiB
#include "books.h" #include <bits/stdc++.h> using namespace std; const int N=1e6, LG=20; int tl, tr; int vis[N], n, stmin[LG][N], stmax[LG][N]; pair<int, int> ll[N], rr[N]; int check[N]; long long ans=0; int getmin(int l, int r){ int lg=__lg(r-l+1); return min(stmin[lg][l], stmin[lg][r-(1<<lg)+1]); } int getmax(int l, int r){ int lg=__lg(r-l+1); return max(stmax[lg][l], stmax[lg][r-(1<<lg)+1]); } void extend(int &l, int &r){ while (1){ int nl=getmin(l, r), nr=getmax(l, r); if (nl==l && nr==r) break; l=nl; r=nr; } } void dp(int l, int r){ extend(l, r); if (l==tl && r==tr) return; int l1=tl, r1=r, l2=l, r2=tr; if (ll[l].first || ll[l].second){ l1=ll[l].first; } if (rr[r].first || rr[r].second){ r2=rr[r].second; } int c1=check[l]-check[l1]; int c2=check[r2]-check[r]; if (l1==l) c1=1e9; if (r2==r) c2=1e9; ans+=min(c1, c2)*2; if (c1<=c2) return dp(l1, r1); return dp(l2, r2); } long long minimum_walk(vector<int> p, int s) { n=p.size(); for (int i=0; i<n; ++i) if (!vis[i]){ vector<int> cc; int u=i; stmin[0][i]=i; stmax[0][i]=i; int maxl=-1, minr=-1; while (!vis[u]){ if (u<s){ if (maxl==-1 || maxl<u) maxl=u; } if (u>s){ if (minr==-1 || minr>u) minr=u; } vis[u]=1; cc.push_back(u); stmin[0][i]=min(stmin[0][i], u); stmax[0][i]=max(stmax[0][i], u); ans+=abs(u-p[u]); u=p[u]; } if (stmin[0][i]<s && s<stmax[0][i]){ ll[stmin[0][i]+1]=rr[stmax[0][i]-1]={maxl, minr}; }else{ int l=stmin[0][i], r=stmax[0][i]; ++check[l]; --check[r]; } for (int j:cc) stmin[0][j]=stmin[0][i], stmax[0][j]=stmax[0][i]; } partial_sum(check, check+n, check); for (int i=n-1; i>0; --i) check[i]=!check[i-1]; check[0]=0; partial_sum(check, check+n, check); for (int i=1; i<=s; ++i){ if (ll[i].first==0 && ll[i].second==0) ll[i]=ll[i-1]; } for (int i=n-2; i>=s; --i){ if (rr[i].first==0 && rr[i].second==0) rr[i]=rr[i+1]; } for (int i=1; i<LG; ++i) for (int j=0; j+(1<<i)-1<n; ++j){ stmin[i][j]=min(stmin[i-1][j], stmin[i-1][j+(1<<(i-1))]); stmax[i][j]=max(stmax[i-1][j], stmax[i-1][j+(1<<(i-1))]); } tl=0, tr=n-1; while (tl<s && stmin[0][tl]==tl && stmax[0][tl]==tl) ++tl; while (tr>s && stmin[0][tr]==tr && stmax[0][tr]==tr) --tr; dp(s, s); 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...