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. Android代码规范----按钮单击事件的四种写法

    [前言] 按钮少的时候用第三种的匿名内部类会比较快,比如写demo测试的时候或者登陆界面之类. 按钮多的时候一般选择第四种写法. 一.第一种写法:在XML文件中声明onClick属性(很少用) 在XM ...

  2. 注册其它地区Apple ID

    不同地区Apple Id需求 iPhone 最近换上了iPhone,想要下载其它国家地区的游戏,需要登录该国的Apple Id才能在itunes里下载. 下面记录一下我注册日本地区的Apple Id过 ...

  3. [转]在Eclipse中使用JUnit4进行单元测试(高级篇)

    通过前2篇文章,您一定对JUnit有了一个基本的了解,下面我们来探讨一下JUnit4中一些高级特性. 一.高级Fixture 上一篇文章中我们介绍了两个Fixture标注,分别是@Before和@Af ...

  4. Eclipse快捷键列表大全

    from: http://hi.baidu.com/lzycsd/item/c6febccceacc173c44941684 from: http://www.open-open.com/bbs/vi ...

  5. 介绍linux下利用编译bash设置root账号共用的权限审计设置

    在日常运维工作中,公司不同人员(一般是运维人员)共用root账号登录linux服务器进行维护管理,在不健全的账户权限审计制度下,一旦出现问题,就很难找出源头,甚是麻烦!在此,介绍下利用编译bash使不 ...

  6. [py]编码 Unicode utf-8

    什么是字符集 在介绍字符集之前,我们先了解下为什么要有字符集.我们在计算机屏幕上看到的是实体化的文字,而在计算机存储介质中存放的实际是二进制的比特流.那么在这两者之间的转换规则就需要一个统一的标准,否 ...

  7. 安装mysql-connector-python

    安装mysql-connector-python 1.下载. wget http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connec ...

  8. 浅谈JS事件冒泡

    今天要跟大家谈的是事件冒泡,这个事件呢,也是两面性的,有时候给我们带来bug,有时候优点也很明显.我们就一起来看看它的真面目.  首先看看事件冒泡是什么? 事件冒泡 :当一个元素接收到事件的时候 会把 ...

  9. 如何使用Iveely的数据存储引擎 Iveely Database

    Iveely 数据存储引擎是为Iveely 搜索引擎提供数据存储的机制. 适用于:频繁数据插入.数据读取.数据更改或者删除数据不适合Iveely Database,存储结构是按照搜索引擎数据存储要求( ...

  10. windows API 开发飞机订票系统 图形化界面 (一)

    去年数据结构课程设计的作品,c语言实现,图形化界面使用windows API实现. 首发在我csdn博客:http://blog.csdn.net/u013805360/article/details ...