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

简单的区间求最值。

Problem: 3264

User: ksq2013

Memory: 1736K

Time: 3829MS

Language: G++

Result: Accepted

#include<cstdio>
#include<iostream>
using namespace std;
int n,q,maxnum[201000],minnum[201000];
void build(int s,int t,int k)
{
if(!(s^t)){
scanf("%d",&maxnum[k]);
minnum[k]=maxnum[k];
return;
}int m=(s+t)>>1;
build(s,m,k<<1);
build(m+1,t,k<<1|1);
maxnum[k]=max(maxnum[k<<1],maxnum[k<<1|1]);
minnum[k]=min(minnum[k<<1],minnum[k<<1|1]);
}
int query1(int s,int t,int k,int l,int r)
{
if(l<=s&&t<=r)return maxnum[k];
int m=(s+t)>>1,res=0;
if(l<=m)res=query1(s,m,k<<1,l,r);
if(m<r)res=max(res,query1(m+1,t,k<<1|1,l,r));
return res;
}
int query2(int s,int t,int k,int l,int r)
{
if(l<=s&&t<=r)return minnum[k];
int m=(s+t)>>1,res=1000100;
if(l<=m)res=query2(s,m,k<<1,l,r);
if(m<r)res=min(res,query2(m+1,t,k<<1|1,l,r));
return res;
}
int main()
{
scanf("%d%d",&n,&q);
build(1,n,1);
for(;q;q--){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",query1(1,n,1,a,b)-query2(1,n,1,a,b));
}
return 0;
}

poj 3264的更多相关文章

  1. poj 3264 Balanced Lineup (RMQ)

    /******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...

  2. POJ 3264 Balanced Lineup(RMQ)

    点我看题目 题意 :N头奶牛,Q次询问,然后给你每一头奶牛的身高,每一次询问都给你两个数,x y,代表着从x位置上的奶牛到y位置上的奶牛身高最高的和最矮的相差多少. 思路 : 刚好符合RMQ的那个求区 ...

  3. POJ 3264 RMQ裸题

    POJ 3264 题意:n个数,问a[i]与a[j]间最大值与最小值之差. 总结:看了博客,记下了模板,但有些地方还是不太理解. #include<iostream> #include&l ...

  4. poj 3264 & poj 3468(线段树)

    poj 3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0 求任一区间的最大值和最小值的差 #include<io ...

  5. poj 3264 Balanced Lineup 题解

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Subm ...

  6. poj 3264 Balanced Lineup【RMQ-ST查询区间最大最小值之差 +模板应用】

    题目地址:http://poj.org/problem?id=3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0分析:标 ...

  7. Poj 3264 Balanced Lineup RMQ模板

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

  8. poj 3264(线段树)

    http://poj.org/problem?id=3264 初学线段可以做的水题,也是线段树的基础运用.也是我的第一个线段树的题. 题意:在区间范围内的最大值减去最小值 思路:线段树记录下每个区间内 ...

  9. POJ——3264线段树

    题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,00 ...

  10. POJ 3264 区间最大最小值Sparse_Table算法

    题目链接:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total ...

随机推荐

  1. MJRefresh自定义刷新动画

    [一]常见用法 最原始的用法,耦合度低,但是不能统一管理.我们需要在每一个控制器都写以下代码,很繁琐,以后项目修改起来更繁琐,得一个控制器一个控制器的去定位.修改. 1.1 使用默认刷新(耦合度底,但 ...

  2. 【C语言】外部函数和内部函数

    目录 [外部函数]  [内部函数] 1.外部函数  定义的函数能被本文件和其它文件访问(默认). 注:不允许有同名的外部函数. 2.内部函数  定义的函数只能被本文件访问,其它文件不能访问. 注:允许 ...

  3. 三种经典iPhone上网络抓包方法详解

    此文章来自:听云博客 很多时候需要网络抓包分析,在iPhone上抓包稍有不同,下面介绍三种常用的方式.分析工具以wireshark为例. 一.最简单的方式:用PC作为热点,在PC上抓包 优点:简单 缺 ...

  4. GET和POST请求

    GET与POST请求 简介 GET请求解释及语法格式 POST请求简介及语法 GET请求代码 POST请求代码 GET请求解释及语法格式: 网络请求默认是get 网络请求有很多种:GET查 POST改 ...

  5. 【Android】OPlayer升级Vitamio到4.1

    前言 很久没有更新OPlayer,还是使用旧版Vitamio 3.0版本(新版已经到4.1),这次更新下. 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.c ...

  6. new ActiveXObject("Scripting.FileSystemObject") 时抛出异常 .

    使用JScript读写本地文件时,会使用Scripting.FileSystemObject控件. IE默认是不允许运行这类“未标记为安全执行脚本的ActiveX控件”的. 因此执行下行代码时: fs ...

  7. CVEH项目观察与思考

    2013-07-01 项目进展: 从启动至今已有三个星期,但是进展甚慢,取得的进展有: A. 封装成库,和HB调用库的接口有些进展,但进未完成 B. 整个框架,类视图,调用视图,只有两三层的进展: C ...

  8. 编写一个Java应用程序,该应用程序包括2个类:Print类和主类E。Print 类里有一个方法output()功能是输出100 ~ 999之间的所有水仙花数(各位数字的 立方和等于这个三位数本身,如: 371 = 33 + 73 + 13。)在主类E的main方法中来 测试类Print

    package zuoye; public class print { void output() { System.out.println("100-999之间的水仙花数是:") ...

  9. 0007《SQL必知必会》笔记03-汇总与分组数据

    1.有些时候需要数据的汇总值,而不是数据本身,比如对某些数据求和.计数.求最大最小值.求平均值,因此就有了5个聚集函数:AVE().COUNT().MAX().MIN().SUM(): (1)求平均值 ...

  10. tomcat常用配置

    一. 增加内存,防止JVM内存溢出 1. 以服务的方式启动时 进入"tomcat安装路径\bin"目录下,打开service.bat文件,找到"--JvmOptions ...