B. Balanced Lineup

B. Balanced Lineup

Time Limit: 5000ms

Case Time Limit: 5000ms

Memory Limit: 65536KB

64-bit integer IO format: %lld      Java class name: Main

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

解题:RMQ

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <climits>
 7 #include <algorithm>
 8 #include <cmath>
 9 #define LL long long
10 using namespace std;
11 int mn[50010][32],mx[50010][32],d[50010];
12 int main(){
13     int n,m,x,y,i,j;
14     while(~scanf("%d %d",&n,&m)){
15         for(i = 0; i < n; i++){
16             scanf("%d",d+i);
17         }
18         memset(mn,0,sizeof(mn));
19         memset(mx,0,sizeof(mx));
20         for(i = n-1; i >= 0; i--){
21             mn[i][0] = mx[i][0] = d[i];
22             for(j = 1; i+(1<<j)-1 < n; j++){
23                 mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
24                 mx[i][j] = max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);
25             }
26         }
27         for(i = 0; i < m; i++){
28             scanf("%d %d",&x,&y);
29             if(x > y) swap(x,y);
30             int r = y - x + 1;
31             r = log2(r);
32             int theMax,theMin;
33             theMax = max(mx[x-1][r],mx[y-(1<<r)][r]);
34             theMin = min(mn[x-1][r],mn[y-(1<<r)][r]);
35             printf("%d\n",theMax-theMin);
36         }
37     }
38     return 0;
39 }

B. Balanced Lineup,布布扣,bubuko.com

时间: 2024-12-28 14:18:49

B. Balanced Lineup的相关文章

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

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 总是喜欢做一些非凡的事,当然这次照相也不例外.他只给一部分牛照相,并且这一组牛的阵容必须是"

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

poj 3264 Balanced Lineup

题目链接:http://poj.org/problem?id=3264 题目大意:就是给你一串数,问你最大数和最小数的差值....... 思路:最基本的线段树,只需要建树和查询,修改都省啦,但是查询要写两个,一个查询最大值,一个查询最小值......然后就能AC掉.....但是话说poj把它分类到RMQ中.... code: #include<cstdio> #include<cmath> #include<algorithm> #include<iostream

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

bzoj1702[Usaco2007 Mar]Gold Balanced Lineup 平衡的队列*

bzoj1702[Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 题意: N头牛,一共K种特色.每头牛有多种特色.[i,j]段被称为balanced当且仅当K种特色在[i,j]内拥有次数相同.求最大的[i,j]段长度.n≤100000,k≤30. 题解: 得到式子:a[i][l]-a[j][l]=a[i][l-1]-a[j][l-1],l在2..k之间,移项得a[i][l]-a[i][l-1]=a[j][l]-a[j][l-1],l在2..k之间,故可以定义一个

bzoj1637[Usaco2007 Mar]Balanced Lineup*

bzoj1637[Usaco2007 Mar]Balanced Lineup 题意: n头牛,第i头牛位置为ai,种族为bi(只能为0,1),求一个区间(按数轴位置),使得区间两端牛距离差最大且两种种族牛数相等.n≤50000. 题解: 按位置排序.然后利用前缀和sum[i][0]-sum[j-1][0]=sum[i][1]-sum[j-1][1],sum[i][0]-sum[i][1]=sum[j-1][0]-sum[j-1][1],故用个set维护sum[i][0]-sum[i][1]的值,

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