3401: [Usaco2009 Mar]Look Up 仰望

Time Limit: 3 Sec  Memory Limit: 128 MB
Submit: 136  Solved: 81
[Submit][Status][Discuss]

Description

约翰的N(1≤N≤105)头奶牛站成一排,奶牛i的身高是Hi(l≤Hi≤1,000,000).现在,每只奶牛都在向左看齐.对于奶牛i,如果奶牛j满足i<j且Hi<Hj,我们可以说奶牛i可以仰望奶牛j.    求出每只奶牛离她最近的仰望对象.

Input

 
    第1行输入N,之后每行输入一个身高.

Output

 
    共N行,按顺序每行输出一只奶牛的最近仰望对象.如果没有仰望对象,输出0.

Sample Input

6
3
2
6
1
1
2

Sample Output

3
3
0
6
6
0

HINT

 

Source

Silver

题解:再一次请出我神奇的小线段树——用线段树实现求在某某位置之后大于某值的最靠左位置,用了一个比较神奇的分治,具体如程序(发现线段是是个乱搞神器啊有木有)

 var
i,j,k,l,m,n:longint;
a,b:array[..] of longint;
function max(x,y:longint):longint;inline;
begin
if x>y then max:=x else max:=y;
end;
function min(x,y:longint):longint;inline;
begin
if x<y then min:=x else min:=y;
end;
procedure built(z,x,y:longint);inline;
begin
if x=y then
begin
read(a[z]);
b[x]:=a[z];
end
else
begin
built(z*,x,(x+y) div );
built(z*+,(x+y) div +,y);
a[z]:=max(a[z*],a[z*+]);
end;
end;
function approach(z,x,y,l,r,t:longint):longint;inline;
var a1:longint;
begin
if l>r then exit();
if a[z]<=t then exit();
if x=y then
begin
if a[z]>t then exit(x);
end;
a1:=approach(z*,x,(x+y) div ,l,min(r,(x+y) div ),t);
if a1<> then exit(a1);
exit(approach(z*+,(x+y) div +,y,max((x+y) div +,l),r,t));
end;
begin
readln(n);
built(,,n);
for i:= to n do
writeln(approach(,,n,i+,n,b[i])); end.
05-04 06:55