This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "books.h"
#include <bits/stdc++.h>
using namespace std;
int vis[1000001],r[1000001],g[1000001],nxt[1000001];
vector <pair <int, pair <int, int>>> e;
long long minimum_walk(vector <int> p, int s){
int n=p.size(),lo=n,hi=0;
iota(nxt,nxt+n,0);
vector <pair <int, int>> tmp;
long long d=0;
for (int i=0;i<n;i++)
if (!vis[i]){
int x=i;
while (!vis[x]){
vis[x]=1;
g[x]=i;
r[i]=max(r[i],x);
d+=abs(x-p[x]);
x=p[x];
}
if (r[i]!=i||i==s){
lo=min(lo,i);
hi=max(hi,r[i]);
}
}
int L=g[s],R=r[g[s]];
while (L>lo||R<hi){
if (nxt[L]!=R){
int j=nxt[L]+1;
nxt[L]=j;
L=min(L,g[j]);
R=max(R,r[g[j]]);
continue;
}
d+=2;
if (L>lo&&r[g[L-1]]>=R+1||R==hi){
R=r[g[L-1]];
L=g[L-1];
continue;
}
L=min(L,g[R+1]);
R=r[g[R+1]];
}
return d;
}
Compilation message (stderr)
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:36:17: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
36 | if (L>lo&&r[g[L-1]]>=R+1||R==hi){
| ~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |