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代码:
 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
#include <string>
#include <string.h>
#include <queue>
#include <vector>
#include <set>
#include <cmath>
#define inf 0x7fffffff
using namespace std;
const int maxn=;
struct b{
int imax,imin;
}tree[maxn*+];
int n,q,x,y,a[maxn+];
void build(int p,int l,int r){
if(l==r) {tree[p].imax=a[l],tree[p].imin=a[l];return ;}
int mid=(l+r)/;
build(p*,l,mid);
build(p*+,mid+,r);
tree[p].imax=max(tree[p*].imax,tree[p*+].imax);
tree[p].imin=min(tree[p*].imin,tree[p*+].imin);
}
int findmin(int p,int l,int r,int x,int y){
if(x<=l&&r<=y) return tree[p].imin;
int mid=(l+r)/;
if(x>mid) return findmin(p*+,mid+,r,x,y);
if(y<=mid) return findmin(p*,l,mid,x,y);
return min(findmin(p*+,mid+,r,x,y),findmin(p*,l,mid,x,y));
}
int findmax(int p,int l,int r,int x,int y){
if(x<=l&&r<=y) return tree[p].imax;
int mid=(l+r)/;
if(x>mid) return findmax(p*+,mid+,r,x,y);
if(y<=mid) return findmax(p*,l,mid,x,y);
return max(findmax(p*+,mid+,r,x,y),findmax(p*,l,mid,x,y));
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
build(,,n);
for(int i=;i<=q;i++){
scanf("%d%d",&x,&y);
printf("%d\n",findmax(,,n,x,y)-findmin(,,n,x,y));
}
return ;
}
 

poj3264_Balanced Lineup的更多相关文章

  1. poj-3264-Balanced Lineup

    poj   3264  Balanced Lineup link: http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS ...

  2. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  3. Balanced Lineup(树状数组 POJ3264)

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 40493 Accepted: 19035 Cas ...

  4. D:Balanced Lineup

    总时间限制: 5000ms 内存限制: 65536kB描述For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always lin ...

  5. 三部曲一(数据结构)-1022-Gold Balanced Lineup

    Gold Balanced Lineup Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Othe ...

  6. poj 3264 Balanced Lineup (RMQ)

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

  7. poj3264 - Balanced Lineup(RMQ_ST)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 45243   Accepted: 21240 ...

  8. bzoj 1637: [Usaco2007 Mar]Balanced Lineup

    1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer John ...

  9. BZOJ-1699 Balanced Lineup 线段树区间最大差值

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...

随机推荐

  1. BZOJ 3624 [Apio2008]免费道路:并查集 + 生成树 + 贪心【恰有k条特殊路径】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3624 题意: 给你一个无向图,n个点,m条边. 有两种边,种类分别用0和1表示. 让你求一 ...

  2. Struts2与ServletAPI解耦

    什么是与Servlet API解耦? 为了避免与servlet API耦合在一起,方便Action做单元测试, Struts2对HttpServletRequest,HttpSession,和Serv ...

  3. OpenCV——PS 滤镜算法之极坐标变换到平面坐标

    // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...

  4. PHP 正则表达示

    PHP 正则表达示 php如何使用正则表达式 正则表达式基本元字符 #正则表达式示例 ^:匹配输入字符串开始的位置.如果设置了 RegExp 对象的 Multiline 属性,^ 还会与“\n”或“\ ...

  5. IHE-PIX 备注

    IHE给出了各个Actor之间如何通讯的建议: 1.       应用程序通讯时必须用MLLP包装或者解析. 2.       客户端建立连接后,服务器端必须用此连接进行应答.客户端可以继续用此连接启 ...

  6. WPF DelegateCommand CanExecute

     private DelegateCommand searchCommand;         public DelegateCommand SearchCommand         {       ...

  7. 微信开放平台开发-授权、全网发布(PHP)

    这两天做了微信开发平台的开发,梳理下... 浙江百牛信息技术bainiu.ltd整理发布于博客园 先看看授权的流程: 第一步:接收component_verify_ticket: 1.微信服务器每隔1 ...

  8. 五 akka streams kafka

    (转载 https://doc.akka.io/docs/akka-stream-kafka/current/home.html) 一: Akka Streams Kafka, also known ...

  9. Docker入门(一):简介

    这个<Docker入门>系列文档,是我根据Docker官网(https://docs.docker.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家 ...

  10. Brute-Force-Attack on Triple-DES with Reduced Key Space

    题目地址:https://www.mysterytwisterc3.org/en/challenges/level-ii/brute-force-attack-on-triple-des-with-r ...