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  
时间: 2024-11-06 12:41:11

poj3264 Balanced Lineup 2011-12-20的相关文章

POJ3264 Balanced Lineup RMQ 线段树

求区间内最大数和最小数的差,用两棵线段树,一个维护区间最大值,一个维护区间最小值. #include <stdio.h> #include <vector> #include <math.h> #include <string.h> #include <string> #include <iostream> #include <queue> #include <list> #include <algori

POJ3264——Balanced Lineup(线段树)

本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中,取一段区间,然后在区间中找出最大的数和最小的数字,求这两个数字的差. 分析:按区间取值,很明显使用的线段树.区间大小取200000 * 4 = 8 * 10 ^5; 进行查询的时候,注意直接判断l, r 与mid的关系即可,一开始写的时候直接与tree[root].L判断,多余了, 逻辑不正确. #include <iostream> #include <stdio.h> #inc

poj3264 Balanced Lineup(求区间的最大值与最小值之差)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 37869   Accepted: 17751 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 Joh

POJ-3264 Balanced Lineup(线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 47042   Accepted: 22071 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 Joh

POJ3264 Balanced Lineup 线段树 RMQ ST算法应用

Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 36813 Accepted: 17237 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 de

POJ3264 Balanced Lineup 【线段树】+【单点更新】

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32778   Accepted: 15425 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 Joh

poj3264 Balanced Lineup(树状数组)

题目传送门 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 64655   Accepted: 30135 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 Farm

poj--3264 Balanced Lineup(裸的RMQ)

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 rang

poj3264 Balanced Lineup

题意: 给定Q (1 ≤ Q ≤ 200,000)个数A1,A2 - AQ,,多次求任一区间Ai – Aj中最大数和最小数的差. 思路: 线段树. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 const int MAXN = 65536; 7 const int INF = 0x3f3f3f3f; 8 9 struct no