poj3264 Balanced Lineup 2011-12-20

Balanced Lineup

Time Limit: 5000MSMemory Limit: 65536K

Total Submissions: 20569Accepted: 9552

Case Time Limit: 2000MS

Description

 

For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

 

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

 

Input

 

Line 1: Two space-separated integers, N and Q. 

Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i 

Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.

Output

 

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

 

6 3

1

7

3

4

2

5

1 5

4 6

2 2

Sample Output

 

6

3

0

Source

 

USACO 2007 January Silver

 

__________________________________________

 

给定一串数,多次询问某段区间中最大的数减去最小的数

__________________________________________

 

RMQ,用线段树做的话也是水题。

__________________________________________

  1 Program Stone;
  2 
  3 var i,j,k,n,m,lc,rc,best,sest:longint;
  4 
  5     a,max,min:array[1..1 shl 17]of longint;
  6 
  7  function mmax(a,b:longint):longint;
  8 
  9   begin
 10 
 11    if a>b then mmax:=a else mmax:=b;
 12 
 13   end;
 14 
 15  function mmin(a,b:longint):longint;
 16 
 17   begin
 18 
 19    if a>b then mmin:=b else mmin:=a;
 20 
 21   end;
 22 
 23  
 24 
 25  procedure built(head,tail,num:longint);       //建树
 26 
 27  var i,j,k:longint;
 28 
 29   begin
 30 
 31    if head=tail then begin
 32 
 33                       max[num]:=a[head];min[num]:=a[head];
 34 
 35                       exit;
 36 
 37                      end;
 38 
 39    k:=(head+tail)div 2;
 40 
 41    built(head,k,num*2);
 42 
 43    built(k+1,tail,num*2+1);
 44 
 45    max[num]:=mmax(max[num*2],max[num*2+1]);            //存最大值
 46 
 47    min[num]:=mmin(min[num*2],min[num*2+1]);            //存最小值
 48 
 49   end;
 50 
 51  
 52 
 53  procedure query(head,tail,num:longint);               //询问,线段树操作
 54 
 55  var i,j,k:longint;
 56 
 57   begin
 58 
 59    if (lc<=head)and(tail<=rc) then begin
 60 
 61                                     best:=mmax(best,max[num]);
 62 
 63                                     sest:=mmin(sest,min[num]);
 64 
 65                                     exit;
 66 
 67                                    end;
 68 
 69    k:=(head+tail)div 2;
 70 
 71    if rc<=k then query(head,k,num*2) else
 72 
 73    if lc>k then query(k+1,tail,num*2+1) else
 74 
 75      begin
 76 
 77       query(head,k,num*2);
 78 
 79       query(k+1,tail,num*2+1);
 80 
 81      end;
 82 
 83   end;
 84 
 85 Begin
 86 
 87  readln(n,m);
 88 
 89  for i:=1 to n do readln(a[i]);
 90 
 91  built(1,n,1);
 92 
 93  for i:=1 to m do
 94 
 95   begin
 96 
 97    readln(lc,rc);
 98 
 99    sest:=1000000;best:=0;
100 
101    query(1,n,1);
102 
103    writeln(best-sest);
104 
105   end;
106 
107 end.
108 
109  

 

posted on 2016-03-02 17:37  Yesphet  阅读(133)  评论(0编辑  收藏  举报