博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3264——Balanced Lineup(线段树+区间最大值与最小值)
阅读量:2344 次
发布时间:2019-05-10

本文共 2302 字,大约阅读时间需要 7 分钟。

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

就是一个把求区间最大值与求最小值结合起来,我用了两次查询就过了。。。。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 200005#define Mod 10001using namespace std;#define lson l , m , rt << 1#define rson m + 1 , r , rt << 1 | 1const int maxn=222222;int MAX[maxn<<2],MIN[maxn<<2];void PushUp(int rt){ MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]); MIN[rt]=min(MIN[rt<<1],MIN[rt<<1|1]);}void build(int l,int r,int rt){ if(r==l) { scanf("%d",&MAX[rt]); MIN[rt]=MAX[rt]; return; } int m=(l+r)>>1; build(lson); build(rson); PushUp(rt);}int queryma(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R) { return MAX[rt]; } int m=(l+r)>>1; int ret=0; if(L<=m) ret=max(ret,queryma(L,R,lson)); if(R>m) ret=max(ret,queryma(L,R,rson)); return ret;}int querymi(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R) { return MIN[rt]; } int m=(l+r)>>1; int ret=INF; if(L<=m) ret=min(ret,querymi(L,R,lson)); if(R>m) ret=min(ret,querymi(L,R,rson)); return ret;}int main(){ int n,q; while(~scanf("%d%d",&n,&q)) { build(1,n,1); while(q--) { int l,r; scanf("%d%d",&l,&r); printf("%d\n",queryma(l,r,1,n,1)-querymi(l,r,1,n,1)); } } return 0;}

转载地址:http://gpcvb.baihongyu.com/

你可能感兴趣的文章
华为研发工程师编程题----进制转换(pow函数,string.find())
查看>>
华为研发工程师编程题----汽水瓶
查看>>
以下不属于tcp连接断开的状态是?----腾讯2016研发工程师笔试题
查看>>
下面关于ICMP协议的描述中,正确的是()----腾讯2016研发工程师笔试题
查看>>
对于移动平均算法,是计算某变量之前n个数值的算术平均,正确的说法是:----腾讯2016研发工程师在线模拟笔试题
查看>>
某一速率为100M的交换机有20个端口,其一个端口上连着一台笔记本电脑,此电脑从迅雷上下载一部1G的电影需要的时间可能是多久?
查看>>
在linux编程中,以下哪个TCP的套接字选项与nagle算法的开启和关闭有关?----腾讯2016研发工程师在线模拟笔试题
查看>>
某二叉树的先根遍历序列和后根遍历序列正好相反,则该二叉树具有的特征是()----腾讯2016研发工程师在线模拟笔试题
查看>>
win32系统里,下面几个sizeof的运行结果是()----腾讯2016研发工程师在线模拟笔试题
查看>>
若系统中有五台打印机,有多个进程均需要使用两台,规定每个进程一次仅允许申请一台,则在不发生死锁的情况下至多允许______个进程参与竞争
查看>>
关于红黑树和AVL树,以下哪种说法不正确?----腾讯2016研发工程师在线模拟笔试题
查看>>
关于epoll和select的区别,哪些说法是正确的?----腾讯2016研发工程师在线模拟笔试题
查看>>
以下是C++的不同数据类型值的比较语句,请问这些判断语句中作为条件部分的语句编写有问题的有:
查看>>
TCP链接中主动断开链接netstat观察可能出现的状态流转是:----腾讯2016研发工程师在线模拟笔试题
查看>>
以下涉及到内存管理的代码段中,有错误的是:----腾讯2016研发工程师在线模拟笔试题
查看>>
下面哪些特性可能导致代码体积膨胀:----腾讯2016研发工程师在线模拟笔试题
查看>>
const的使用方法----腾讯2016研发工程师笔试题(一)
查看>>
哪些设计模式是降低资源使用率:----腾讯2016研发工程师笔试题(一)
查看>>
n个顶点,m条边的全连通图,至少去掉____边才能构成一棵树?----腾讯2016研发工程师笔试题(一)
查看>>
在序列(22,34,55,77,89,93,99,102,120,140)中,采用二分查找,分别查找77,34,99,所需的查找次数分别为()----腾讯2016研发工程师笔试题(一)
查看>>