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

 
题解:典型的RMQ问题。线段树的应用。
 
代码:
 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>
#include<stdbool.h> #define rep(i,a,b) for(i=(a);i<=(b);i++)
#define clr(x,y) memset(x,y,sizeof(x))
#define sqr(x) (x*x)
#define LL long long const int INF=0xffffff0; struct {
int L,R;
int minV,maxV;
} tree[]; int i,j,n,q,minV,maxV; int min(int a, int b)
{
if(a<b) return a;
return b;
} int max(int a,int b)
{
if(a>b) return a;
return b;
} void BuildTree(int root,int L,int R)
{
tree[root].L=L;
tree[root].R=R;
tree[root].maxV=-INF;
tree[root].minV=INF; if(L!=R) {
BuildTree(*root,L,(L+R)/);
BuildTree(*root+,(L+R)/+,R);
} } void Insert(int root,int i,int v)
{
int mid; if(tree[root].L==tree[root].R) {
tree[root].maxV=tree[root].minV=v;
return ;
} tree[root].minV=min(tree[root].minV,v);
tree[root].maxV=max(tree[root].maxV,v); mid=(tree[root].L+tree[root].R)/;
if(i<=mid)
Insert(*root,i,v);
else
Insert(*root+,i,v); } void Query(int root,int s,int e)
{
int mid; if(tree[root].minV>=minV && tree[root].maxV<=maxV) return ;
if(tree[root].L==s && tree[root].R==e) {
minV=min(minV,tree[root].minV);
maxV=max(maxV,tree[root].maxV);
return ;
} mid=(tree[root].L+tree[root].R)/; if(e<=mid)
Query(*root,s,e);
else if(s>mid)
Query(*root+,s,e);
else {
Query(*root,s,mid);
Query(*root+,mid+,e);
} } int main()
{
int i,x,y; scanf("%d%d",&n,&q);
BuildTree(,,n);
rep(i,,n) {
scanf("%d",&x);
Insert(,i,x);
} while(q--) {
scanf("%d%d",&x,&y);
minV=INF;
maxV=-INF;
Query(,x,y);
printf("%d\n",maxV-minV);
} return ;
}

[POJ] 3264 Balanced Lineup [线段树]的更多相关文章

  1. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

  2. POJ 3264 Balanced Lineup 线段树RMQ

    http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...

  3. POJ 3264 Balanced Lineup 线段树 第三题

    Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...

  4. POJ 3264 Balanced Lineup (线段树)

    Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...

  5. POJ - 3264 Balanced Lineup 线段树解RMQ

    这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...

  6. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  7. Poj 3264 Balanced Lineup RMQ模板

    题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...

  8. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  9. POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. 智能卡安全机制比较系列(三) MPCOS

    MPCOS是金普斯早期推出的一款多应用支付芯片卡操作系统,支持ISO7816以及PCOS的数据格式和命令.MPCOS具有两级目录文件结构,即MF下可以有一级DF,每个DF下最多可创建63个EF. MP ...

  2. Centos 添加Root用户

    今天,我要描述的是如何在Centos Linux 系统中建立一个和Root账户等权限的用户账户.废话不多说,开始列出必要的操作. 1:首先,我们使用以下命令 进行用户的创建 和 用户密码的初始化. # ...

  3. eclipse js中 选中方法按F3快捷键不能跳转到对应方法的解决方案

    这种情况很可能是m2e-wtp插件没有安装的,安装插件成功后即可解决. m2e-wtp插件安装参照相应随笔.

  4. Annotation(三)——Spring注解开发

    Spring框架的核心功能IoC(Inversion of Control),也就是通过Spring容器进行对象的管理,以及对象之间组合关系的映射.通常情况下我们会在xml配置文件中进行action, ...

  5. Java中BigDecimal的8种舍入模式是怎样的

    Java中BigDecimal的8种舍入模式是怎样的?下面长沙欧柏泰克软件学院和大家一起来学习下吧:  java.math.BigDecimal 不可变的.任意精度的有符号十进制数.BigDecima ...

  6. 真实经纬度(gps)转成百度坐标的js方法

    转:http://www.360doc.com/content/16/0320/14/18636294_543805051.shtml 结果图: <!DOCTYPE html> <h ...

  7. 使用sublime text2怎样新建文件高速生成HTML头部信息?

    前提须要安装Emmet插件.安装完毕后重新启动sublime. 输入下面简写,按Tab. html:4t <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML ...

  8. 在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6564592 在前一篇文章提到,从源代码树下载下 ...

  9. 卸载rpm包提示:error: specifies multiple packages

    –allmatches  Remove all versions of the package which match PACKAGE_NAME. Normally an error is issue ...

  10. Linux 常用系统命令-20160504

    一.显示目录和文件的命令 1.ls(list)  功能说明: 列出目录内容. 语 法 : ls [-1aAbBcCdDfFgGhHiklLmnNopqQrRsStuUvxX][-I < 范 本 ...