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 January 2007

输出区间最大最小值之差,RMQ

#include<bits/stdc++.h>
using namespace std;
const int N=;
int dmi[N][],dma[N][],f[N];
int n,q,l,r;
void RMQ_init()
{
for(int j=; (<<j)<=n; j++)
for(int i=; i+j-<=n; i++)
dmi[i][j]=min(dmi[i][j-],dmi[i+(<<(j-))][j-]),dma[i][j]=max(dma[i][j-],dma[i+(<<(j-))][j-]);
}
int RMQ(int l,int r)
{
int k=f[r-l+];
return max(dma[l][k],dma[r-(<<k)+][k])-min(dmi[l][k],dmi[r-(<<k)+][k]);
}
int main()
{
f[]=-;
scanf("%d%d",&n,&q);
for(int i=; i<=n; i++)
scanf("%d",&dma[i][]),dmi[i][]=dma[i][],f[i]=((i&(i-))==)?f[i-]+:f[i-];
RMQ_init();
while(q--)
{
scanf("%d%d",&l,&r);
printf("%d\n",RMQ(l,r));
}
return ;
}

线段树

#include<bits/stdc++.h>
using namespace std;
#define lson l,mi,rt<<1
#define rson mi+1,r,rt<<1|1
struct T
{
int ma,mi,l,r;
}tree[];
int h[];
int ma,mi;
void build(int l,int r,int rt)
{
tree[rt].l=l,tree[rt].r=r;
if(l==r)
{
tree[rt].ma=tree[rt].mi=h[l];
return;
}
int mi=(l+r)>>;
build(lson);
build(rson);
tree[rt].ma=max(tree[rt<<].ma,tree[rt<<|].ma);
tree[rt].mi=min(tree[rt<<].mi,tree[rt<<|].mi);
}
void findma(int l,int r,int rt)
{
if(tree[rt].l==l&&tree[rt].r==r)
{
if(tree[rt].ma>ma)ma=tree[rt].ma;
return;
}
int mi=(tree[rt].l+tree[rt].r)>>;
if(mi>=r)
findma(l,r,rt<<);
else if(mi<l)
findma(l,r,rt<<|);
else
findma(lson),findma(rson);
} void findmi(int l,int r,int rt)
{
if(tree[rt].l==l&&tree[rt].r==r)
{
if(tree[rt].mi<mi)mi=tree[rt].mi;
return;
}
int mi=(tree[rt].l+tree[rt].r)>>;
if(mi>=r)
findmi(l,r,rt<<);
else if(mi<l)
findmi(l,r,rt<<|);
else
findmi(lson),findmi(rson);
}
int main()
{
int n,q,i,a,b;
scanf("%d%d",&n,&q);
for(i=;i<=n;i++)
scanf("%d",&h[i]);
build(,n,);
while(q--)
{
ma=;
mi=1e9;
scanf("%d%d",&a,&b);
findma(a,b,),findmi(a,b,);
printf("%d\n",ma-mi);
}
return ;
}

TOJ1698: Balanced Lineup的更多相关文章

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

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

  2. Balanced Lineup(树状数组 POJ3264)

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

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

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

  4. poj 3264 Balanced Lineup (RMQ)

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

  5. poj3264 - Balanced Lineup(RMQ_ST)

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

  6. bzoj 1637: [Usaco2007 Mar]Balanced Lineup

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

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

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

  8. POJ3264 Balanced Lineup

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 44720   Accepted: 20995 ...

  9. POJ 3274 Gold Balanced Lineup

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...

随机推荐

  1. POJ 2288 Islands and Bridges (状压DP,变形)

    题意: 给一个无向图,n个点m条边,每个点有点权,要求找到一条哈密顿路径,使得该路径的f(path)值最大.输出f值,若有多条最大f值的路径,输出路径数量. f值由如下3点累加而来: (1)所有点权之 ...

  2. HDU 4507 吉哥系列故事——恨7不成妻 (数位DP)

    题意: 如果一个整数符合下面3个条件之一,那么我们就说这个整数和7有关: 1.整数中某一位是7: 2.整数的每一位加起来的和是7的整数倍: 3.这个整数是7的整数倍: 给定一个区间[L,R],问在此区 ...

  3. 51nod 1489 蜥蜴和地下室

    题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 哈利喜欢玩角色扮演的电脑游戏<蜥蜴和地下室>.此时,他正在扮演一个魔术 ...

  4. 如何处理错误消息Please install the Linux kernel header files

    Please install the Linux kernel "header" files matching the current kernel 当我启动minilkube时遇 ...

  5. 【转】iOS学习笔记(十七)——文件操作(NSFileManager)

    iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容.iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内.默认 ...

  6. 用fmt标签对EL表达式取整

    本篇文章转载自:https://blog.csdn.net/u013400939/article/details/47948541 一般来说我们是无法实现EL表达式取整的.对于EL表达式的除法而言,他 ...

  7. Java中的ArrayList类和LinkedList

    集合的体系: ----------| Collection 单列集合的根接口----------------| List 如果实现了List接口的集合类,具备的特点: 有序,可重复.--------- ...

  8. javaweb基础(19)_jsp标签

    一.JSP标签介绍 JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护. 二.JSP常用标签 ...

  9. java基础——接口与抽象类的区别

    (1)首先接口和抽象类的设计目的就是不一样的.接口是对动作的抽象,而抽象类是对根源的抽象. (2)对于抽象类,一个类只能继承一个抽象类.但是一个类可以同时实现多个接口. (3)接口是公开的,里面不能有 ...

  10. ReactiveCocoa入门-part1

    作为一个iOS开发者,你写的每一行代码几乎都是在响应某个事件,例如按钮的点击,收到网络消息,属性的变化(通过KVO)或者用户位置的变化(通过CoreLocation).但是这些事件都用不同的方式来处理 ...