3381: [Usaco2004 Open]Cave Cows 2 洞穴里的牛之二
3381: [Usaco2004 Open]Cave Cows 2 洞穴里的牛之二
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 21 Solved: 18
[Submit][Status][Discuss]
Description
洞窟里有一道长长的通道.它由N(1≤N≤25000)段道尾相连构成,编号分别为1到N.每个通道有一个阈值,其范围在[1,10^9]依次通过i..j的通道,那奶牛的体重指数就不能超过i..j通道中阈值的最小值.贝茜有Q(1≤Q≤25000)个问题,想请教你由i到j的通道的阈值的最小值.
Input
第1行输入N和Q,接下来N行输入每个通道的阈值,之后Q行每行两个整数,对应问题中的i和j(i<j).
Output
对于每个问题,输出其结果.
Sample Input
10 4
75
30
100
38
50
51
52
20
81
5
1 10
3 5
6 9
8 10
75
30
100
38
50
51
52
20
81
5
1 10
3 5
6 9
8 10
Sample Output
5
38
20
5
38
20
5
HINT
Source
题解:RMQ模板题。。。感觉自己水水哒
1 /************************************************************** 2 Problem: 3381 3 User: HansBug 4 Language: Pascal 5 Result: Accepted 6 Time:272 ms 7 Memory:2220 kb 8 ****************************************************************/ 9 10 var 11 i,j,k,l,m,n,h:longint; 12 a:array[0..30000,0..16] of longint; 13 function min(x,y:longint):longint; 14 begin 15 if x<y then min:=x else min:=y; 16 end; 17 begin 18 readln(n,m); 19 h:=trunc(ln(n)/ln(2)+1); 20 for i:=1 to n do readln(a[i,0]); 21 for i:=1 to h do 22 for j:=1 to n-trunc(exp(ln(2)*i))+1 do 23 a[j,i]:=min(a[j,i-1],a[j+trunc(exp(ln(2)*(i-1))),i-1]); 24 25 for i:=1 to m do 26 begin 27 readln(j,k); 28 l:=trunc(ln(k-j+1)/ln(2)); 29 writeln(min(a[j,l],a[k-trunc(exp(ln(2)*l))+1,l])); 30 end; 31 readln; 32 end.