我用了一个st表记录bfs上的时间戳,每次找区间中最小的那个为根,然后递归的建树。。。为什么st表要开到60w才能过呢???附上代码program aaaa;
var a,lg:array[0..600300] of longint; f:array[0..600300,0..30] of longint; n,i,j:longint;
function min(xa,xb:longint):longint; begin if xa<xb then exit(xa) else exit(xb); end;
function query(xa,xb:longint):longint; var len:longint; begin len:=lg[xb-xa+1]; exit(min(f[xa,len],f[xb-(1 shl len)+1,len])); end;
procedure work(l,r:longint); var now:longint; begin now:=query(l,r); write(a[now],' '); if a[now]>l then work(l,a[now]-1); if a[now]<r then work(a[now]+1,r); end;
begin readln(n); for i:=1 to n do read(a[i]); fillchar(f,sizeof(f),0); for i:=1 to n do f[a[i],0]:=i; lg[1]:=0; for i:=2 to n do if i and (i-1)=0 then lg[i]:=lg[i-1]+1 else lg[i]:=lg[i-1]; for i:=1 to lg[n] do for j:=1 to n do f[j,i]:=min(f[j,i-1],f[j+(1 shl (i-1)),i-1]); work(1,n); end.