Balanced Lineup

Time Limit: 5000MS Memory Limit: 65536K

Total Submissions: 41548 Accepted: 19514

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

Source

USACO 2007 January Silver

题目大意:农夫JOHN让我们求一个区间上的最大差值(最大值-最小值)

维护两棵线段树即可,一棵max,一棵min,然后求差值,自己第一次摸索写多棵线段树姿势,感觉写的还可以,以后待改进...

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 50001
int maxs[maxn<<2]={0},mins[maxn<<2]={0}; void updata1(int now)
{
maxs[now]=max(maxs[now<<1],maxs[now<<1|1]);
} void updata2(int now)
{
mins[now]=min(mins[now<<1],mins[now<<1|1]);
} void point_change(int l,int r,int now,int loc,int num)
{
if (l==r)
{
maxs[now]=num;
mins[now]=num;
return;
}
int mid=(l+r)>>1;
if (loc<=mid)
point_change(l,mid,now<<1,loc,num);
else
point_change(mid+1,r,now<<1|1,loc,num);
updata1(now);
updata2(now);
} int query(int L,int R,int now,int l,int r,bool f)//f为true为求最大,false为求最小
{
if (L<=l && R>=r)
if (f==true)
return maxs[now];
else
return mins[now];
int mid=(l+r)>>1;
if (f==true)
{
int ans=0;
if (L<=mid)
ans=max(ans,query(L,R,now<<1,l,mid,true));
if (R>mid)
ans=max(ans,query(L,R,now<<1|1,mid+1,r,true));
return ans;
}
else
{
int ans=100000000;
if (L<=mid)
ans=min(ans,query(L,R,now<<1,l,mid,false));
if (R>mid)
ans=min(ans,query(L,R,now<<1|1,mid+1,r,false));
return ans;
}
} int main()
{
int n,m;
scanf("%d%d",&n,&m);
//build(1,n,1);
for (int i=1; i<=n; i++)
{
int x;
scanf("%d",&x);
point_change(1,n,1,i,x);
}
for (int i=1; i<=m; i++)
{
int x,y;
scanf("%d%d",&x,&y);
int answer=query(x,y,1,1,n,true)-query(x,y,1,1,n,false);
printf("%d\n",answer);
}
return 0;
}

BZOJ-1699 Balanced Lineup 线段树区间最大差值的更多相关文章

  1. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  2. POJ3264 Balanced Lineup 线段树区间最大值 最小值

    Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...

  3. hdu4521-小明系列问题——小明序列(线段树区间求最值)

    题意:求最长上升序列的长度(LIS),但是要求相邻的两个数距离至少为d,数据范围较大,普通dp肯定TLE.线段树搞之就可以了,或者优化后的nlogn的dp. 代码为  线段树解法. #include ...

  4. 2017 Wuhan University Programming Contest (Online Round) D. Events,线段树区间更新+最值查询!

    D. Events 线段树区间更新查询区间历史最小值,看似很简单的题意写了两天才写出来. 题意:n个数,Q次操作,每次操作对一个区间[l,r]的数同时加上C,然后输出这段区间的历史最小值. 思路:在线 ...

  5. hdu 1754 I Hate It(线段树区间求最值)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  6. bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树

    1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 772  Solved: 560线 ...

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

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

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

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

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

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

随机推荐

  1. oracle wm_concat(column)函数的使用

    oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oraclewm_concat(column)函数实现字段合并,如果您对oracle wm_concat(c ...

  2. mysqli事务处理demo

    <?php  $mysqli=new mysqli("localhost", "root", "123456", "xsph ...

  3. 18种CSS3loading效果完整版,兼容各大主流浏览器,提供在线小工具使用

    今天把之前分享的两篇博客<CSS3实现10种Loading效果>和 <CSS3实现8种Loading效果[二]>整理了一下.因为之前所分享的各种loading效果都只是做了we ...

  4. K2与OData和Swagger的集成

    最近K2陆续发布了一些好消息,从与Box的集成到今年取得的融资.这儿还有一个:K2近期宣布获得了DData和Swagger REST的支持,保障K2 Appit和Blackpearl的用户能建立基于工 ...

  5. OpenGL 4.3配置教程

    OpenGL 4.3配置教程 下载开发包 需要下载的开发包主要包含如下几个组件:freeglut+glew+ OpenGL.Development.Cookbook+源码+GLM+SOIL. Open ...

  6. 小程序基础02:全局配置app.json

    1.配置 我们使用app.json文件来对来微信小程序进行全局配置. 作用:他决定了页面文件的路径,窗口表现,设置网络超时时间,设置多tab等 每一个小程序页面也可以使用 .json 文件来对本页面的 ...

  7. 大前端时代已经到来!传智播客2015之WEB前端视频教程(全套教程共15G)

    大前端时代已经到来!传智播客2015之WEB前端视频教程(全套教程共15G)大前端时代已经到来!如今,前端开发工程师的职责,不是只有切图.制作网页这么简单哦! G:\传智播客2015-WEB前端视频教 ...

  8. js字符串截取函数slice()、substring()、substr()

    摘要 在js中字符截取函数有常用的三个slice().substring().substr()了,下面我来给大家介绍slice().substring().substr()函数在字符截取时的一些用法与 ...

  9. RocEDU.阅读.写作《图解TCP/IP》

    2015年11月21日 一.对本书的认识 信息通信社会这个词俨然是现代社会的一个名词.人们可以使用各种信息终端随时随地的进行交流,而这种环境正是以来网络才得以实现,使用最为广泛的协议就是TCP/IP协 ...

  10. 大型网站系统架构实践(五)深入探讨web应用高可用方案

    从上篇文章到这篇文章,中间用了一段时间准备,主要是想把东西讲透,同时希望大家给与一些批评和建议,这样我才能有所进步,也希望喜欢我文章的朋友,给个赞,这样我才能更有激情,呵呵. 由于本篇要写的内容有点多 ...