博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ3264 Balanced Lineup
阅读量:4583 次
发布时间:2019-06-09

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

Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 22573   Accepted: 10499
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 31734251 54 62 2

Sample Output

630

Source

 
//线段树、求区间最大最小值之差
#include 
#include
using namespace std;structnode{
int l,r; int Min,Max;};node st[200000];int N,Q,r_max,r_min;void built(int k,int x,int y){
st[k].l=x; st[k].r=y; if(x==y) {
scanf("%d",&st[k].Max); st[k].Min=st[k].Max; return; } int m=(x+y)>>1; int t=k<<1; built(t,x,m); built(t|1,m+1,y); st[k].Max=st[t].Max>st[t|1].Max?st[t].Max:st[t|1].Max; st[k].Min=st[t].Min
r_max) r_max=st[k].Max; if(st[k].Min
>1; int t=k<<1; if(x>m) {
Qu(t|1,x,y); } else if(y<=m) {
Qu(t,x,y); } else {
Qu(t,x,m); Qu(t|1,m+1,y); }}int main(){
int x,y; while(scanf("%d%d",&N,&Q)!=EOF) {
built(1,1,N); while(Q--) {
r_max=0;r_min=1000000; scanf("%d%d",&x,&y); Qu(1,x,y); printf("%d\n",r_max-r_min); } } return 0;}

转载于:https://www.cnblogs.com/372465774y/archive/2012/07/06/2579928.html

你可能感兴趣的文章
Poj2186Popular Cows
查看>>
TCP之listen&backlog
查看>>
实验室的毕业照
查看>>
核心编程答案(第六章)
查看>>
Spring 3.x jar 包详解 与 依赖关系
查看>>
java线程详解二
查看>>
maven项目导入依赖jar包并打包为可运行的jar包
查看>>
leecode第二十三题(合并K个排序链表)
查看>>
关于Eclipse的unsupported major minor version 51.0 错误
查看>>
2014年目标
查看>>
weblogic启动后 登陆控制台特别慢的问题
查看>>
Spring加载resource时classpath*:与classpath:的区别
查看>>
映射“DataAdapter.TableMappings”
查看>>
activity生命周期
查看>>
动画学习之Music图形绘制
查看>>
2019 2.15模拟赛
查看>>
基于H5 pushState实现无跳转页面刷新
查看>>
关于同余与模运算的总结
查看>>
js中top、clientTop、scrollTop、offsetTop的区别 文字详细说明版
查看>>
【转载】法线贴图Nomal mapping 原理
查看>>