Balanced Lineup

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
题意:问你[l r]区间最大值-最小值多少
记得写过 今天给学弟写 自己无聊 写了一遍
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const double PI=acos(-1.0);
const double eps=0.0000000001;
const int N=+;
const int INF=1e9;
int a[N];
struct node{
int l,r;
int maxx,minn;
int val;
}tree[N*];
void build(int left,int right,int pos){
tree[pos].l=left;
tree[pos].r=right;
tree[pos].val=tree[pos].maxx=;
tree[pos].minn=INF;
int mid=(tree[pos].l+tree[pos].r)>>;
if(tree[pos].l==tree[pos].r){
return ;
}
build(left,mid,pos<<);
build(mid+,right,pos<<|);
}
void update(int left,int right,int pos,int x,int y){
int mid=(left+right)>>;
if(tree[pos].l==x&&tree[pos].r==x){
tree[pos].val=y;
tree[pos].minn=y;
tree[pos].maxx=y;
return;
}
if(x>mid)update(mid+,right,pos<<|,x,y);
else
update(left,mid,pos<<,x,y);
tree[pos].maxx=max(tree[pos<<].maxx,tree[pos<<|].maxx);
tree[pos].minn=min(tree[pos<<].minn,tree[pos<<|].minn);
return ; }
int maxx;
int minn;
int query(int pos,int x,int y){
if(x==tree[pos].l&&tree[pos].r==y){
//cout<<4<<endl;
//cout<<tree[pos].maxx<<" "<<tree[pos].minn<<endl;
minn=min(minn,tree[pos].minn);
maxx=max(maxx,tree[pos].maxx);
return ;
}
int mid=(tree[pos].l+tree[pos].r)>>;
if(x>tree[pos].r||y<tree[pos].l)return ;
else if(x>mid)query(pos<<|,x,y);
else if(y<=mid)query(pos<<,x,y);
else{
query(pos<<|,mid+,y);
query(pos<<,x,mid);
}
return ;
}
int main(){
int n,q;
scanf("%d%d",&n,&q);
build(,n,);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
update(,n,,i,a[i]);
}
while(q--){
maxx=;
minn=INF;
//cout<<minn<<" "<<maxx<<endl;
int x,y;
scanf("%d%d",&x,&y);
query(,x,y);
cout<<maxx-minn<<endl;
}
}

POJ 3264 Balanced Lineup (线段树)的更多相关文章

  1. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

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

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

  3. POJ 3264 Balanced Lineup 线段树RMQ

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

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

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

  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. unity3d 各键值对应代码

    KeyCode :KeyCode是由Event.keyCode返回的.这些直接映射到键盘上的物理键.  值        对应键 Backspace     退格键 Delete      Delet ...

  2. Resources.getResourceAsReader 报空指针

    1.maven项目中 可能未编译 导致找不到, 解决方法:mvn clean install -DskipTests -X   编译一下项目 2.可能在 Resources.getResourceAs ...

  3. 如何在Linuxt系统下运行maven项目

    如何在Linuxt系统下运行maven项目 我们知道现在利用MAVEN来管理JAVA项目是非常常见的.比如公司一般都有一个自己的MAVEN仓库,通过MAVEN仓库来解决我们的项目依赖,更加方便的构建项 ...

  4. C# 字符串的入门

    1."@"表示字符串中的"\"不当成转义符. 2.转义符或者特殊处理的一些字符只是针对于代码中直接写出的字符串中,对于程序运行中读取出来的转义符或者特殊处理的字 ...

  5. 回顾Google IO 2016 -Keynote【图解】

    Google IO大会倒计时进行中~~ 两名演奏者在使用高空“古筝”. 最后5秒倒计时~~~~全场轰动~ 倒计时结束,IO大会正式开始.屏幕中,一个人把纯白的唱片放入唱片机中. 然后欢快的音乐响起,台 ...

  6. Codeforces_731F_(前缀和)

    F. Video Cards time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. CAD绘制一个箭头(com接口)

    1 2 3 4 5 6 7 8 //绘制一个箭头  axMxDrawX1.PathMoveToEx(1000, 300, 10, 10, 0);  //设置路径下一点  axMxDrawX1.Path ...

  8. 企业级mysql数据库完全备份、增量备份脚本

    企业完全备份脚本 [root@client ~]# vim /opt/mysql_bak_wanbei.sh #!/bin/bash #MySQL数据库完全备份脚本 #设置登录变量 MY_USER=& ...

  9. zabbix4.0搭建(基于CentOS6.8)

    环境 服务端:188.188.3.241,系统:centos6.8,mysql:5.7.3,php:5.4.9,nginx:1.12.0   一.nginx编译安装 NGINX_VERSION=1.1 ...

  10. 51.percentiles rank以及网站访问时延SLA统计

    主要知识点: percentile_ranks的用法 percentile的优化     一.percentile_ranks的用法 SLA:就是所提供的服务的标准. 比如一个网站的提供的访问延时的S ...