poj3264_Balanced Lineup

Balanced Lineup

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 42349   Accepted: 19917
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线段树维护区间最大值、最小值,然后相减。AC代码:
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <map>
 5 #include <string>
 6 #include <string.h>
 7 #include <queue>
 8 #include <vector>
 9 #include <set>
10 #include <cmath>
11 #define inf 0x7fffffff
12 using namespace std;
13 const int maxn=50000;
14 struct b{
15 int imax,imin;
16 }tree[maxn*4+100];
17 int n,q,x,y,a[maxn+100];
18 void build(int p,int l,int r){
19      if(l==r) {tree[p].imax=a[l],tree[p].imin=a[l];return ;}
20      int mid=(l+r)/2;
21      build(p*2,l,mid);
22      build(p*2+1,mid+1,r);
23      tree[p].imax=max(tree[p*2].imax,tree[p*2+1].imax);
24      tree[p].imin=min(tree[p*2].imin,tree[p*2+1].imin);
25 }
26 int findmin(int p,int l,int r,int x,int y){
27      if(x<=l&&r<=y) return tree[p].imin;
28      int mid=(l+r)/2;
29      if(x>mid) return findmin(p*2+1,mid+1,r,x,y);
30      if(y<=mid) return findmin(p*2,l,mid,x,y);
31      return min(findmin(p*2+1,mid+1,r,x,y),findmin(p*2,l,mid,x,y));
32 }
33 int findmax(int p,int l,int r,int x,int y){
34      if(x<=l&&r<=y) return tree[p].imax;
35      int mid=(l+r)/2;
36      if(x>mid) return findmax(p*2+1,mid+1,r,x,y);
37      if(y<=mid) return findmax(p*2,l,mid,x,y);
38      return max(findmax(p*2+1,mid+1,r,x,y),findmax(p*2,l,mid,x,y));
39 }
40 int main()
41 {
42     scanf("%d%d",&n,&q);
43     for(int i=1;i<=n;i++)
44         scanf("%d",&a[i]);
45         build(1,1,n);
46     for(int i=1;i<=q;i++){
47         scanf("%d%d",&x,&y);
48         printf("%d\n",findmax(1,1,n,x,y)-findmin(1,1,n,x,y));
49     }
50     return 0;
51 }
 
时间: 2024-12-24 12:56:25

poj3264_Balanced Lineup的相关文章

POJ3264_Balanced Lineup(线段树/单点更新)

解题报告 题意: 求区间内最大值和最小值的差值. 思路: 裸线段树,我的线段树第一发.区间最值. #include <iostream> #include <cstring> #include <cstdio> #define inf 99999999 #define LL long long using namespace std; LL minn[201000],maxx[201000]; void update(LL root,LL l,LL r,LL p,LL

poj 3264Balanced Lineup

poj 3264Balanced Lineup 题意:求 一段 区间 的 最大值和最小值 的差值 题解:线段树 碎碎念:某种意义上说,第一道自己手写的线段树,总之蛮好~ #include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std; const int MAXN = 50000+10; const int INF = 1000000;

poj 3264 Balanced Lineup RMQ线段树实现

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36613   Accepted: 17141 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(ST算法求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

bzoj 1637: [Usaco2007 Mar]Balanced Lineup

1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer John 决定给他的奶牛们照一张合影,他让 N (1 ≤ N ≤ 50,000) 头奶牛站成一条直线,每头牛都有它的坐标(范围: 0..1,000,000,000)和种族(0或1). 一直以来 Farmer John 总是喜欢做一些非凡的事,当然这次照相也不例外.他只给一部分牛照相,并且这一组牛的阵容必须是"

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

bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块

1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MB Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的

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

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