题解 P5097 【[USACO2004OPEN]Cave Cows 2 洞穴里的牛之二】
这题好像没有pascal题解哦,那我来水一发

题目描述:求序列中一段区间的最小值

显然用线段树做啊!

代码:
uses math;//math库是个好东西
var
i,j,n,k,m,ans,p,mid,x,y,id,t:longint;
a,tree:array[0..4000009] of longint;
f:boolean;
procedure build(p,l,r:longint);//建树
var
mid:longint;
begin
if (l=r) then begin tree[p]:=a[l]; exit; end;
mid:=(l+r) div 2;
build(p2,l,mid);
build(p
2+1,mid+1,r);
tree[p]:=min(tree[p+p],tree[p+p+1]);
end;
function query(p,l,r,x,y:longint):longint;//查询
var
mid:longint;
begin
mid:=(l+r) div 2;
if (l=x)and(r=y) then exit(tree[p])
else if y<=mid then exit(query(p2,l,mid,x,y))
else if x>mid then exit(query(p
2+1,mid+1,r,x,y))
else exit(min(query(p2,l,mid,x,mid),query(p2+1,mid+1,r,mid+1,y)));
end;
begin
readln(n,m);
for i:=1 to n do readln(a[i]);
build(1,1,n);
for i:=1 to m do
begin
readln(x,y);
writeln(query(1,1,n,x,y));
end;
end.