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. 通过TTL值判断系统

    我们做网络的渗透测试,开始的时候会ping一下网站判断一下网站使用的系统,默认情况下, UNIX 255 LINUX 64WIN2K/NT 128WINDOWS 32 但是 每经过一个路由器就会减1这 ...

  2. 实现了一个简单的cage变形器

    今天实现了一个简单变形器,可以用一个网格的形状影响另一个网格的形状. 如图,蓝色网格的形状被灰色网格操控. 当前的算法非常简单,就是计算蓝色网格每个点到灰色网格每个点的距离,以距离x次方的倒数作为权重 ...

  3. 设置linux账号的有效时间

    在linux系统中,默认创建的用户的有效期限都是永久的,但有时候,我们需要对某些用户的有效期限做个限定!比如:公司给客户开的ftp账号,用于客户下载新闻稿件的.这个账号是有时间限制的,因为是付费的.合 ...

  4. 微软职位内部推荐-SW Engineer II for Skype

    微软近期Open的职位: We are the Skype Beijing team. Skype division drives the communications strategy for Mi ...

  5. log_bin_trust_function_creators变量解释

    在MySQL主从复制机器的master的数据库中创建function,报出如下错误: Error Code: 1418. This function has none of DETERMINISTIC ...

  6. win7(X64)系统下cuda7.5和VS2013的配置

    &1 安装 cuda7.5文件:链接:http://pan.baidu.com/s/1bU2zIQ 密码:nvyw &2 环境变量 注意:CUDA_PATH是安装好cuda7.5之后会 ...

  7. 那些年我们写过的T-SQL(下篇)(转)

    原文:http://www.cnblogs.com/wanliwang01/p/TSQL_Base04.html   下篇的内容很多都会在工作中用到,尤其是可编程对象,那些年我们写过的存储过程,有木有 ...

  8. 软件开发之路、Step 1 需求分析

    百度百科 需求分析 所谓"需求分析",是指对要解决的问题进行详细的分析,弄清楚问题的要求,包括需要输入什么数据,要得到什么结果,最后应输出什么.可以说,在软件工程当中的“需求分析” ...

  9. CSS 清除浮动的四种方法

    在实际项目中,我们经常会用到float属性来对页面进行布局.当使用float时,意味着该元素已经脱离了文档流,相当于浮于文档之上,不占据空间.但是针对兄弟元素为文字内容时,会占据一定空间,从而产生文字 ...

  10. CSS3 chart

    利用CSS3技术生成统计图. 原理:利用元素的百分比算出旋转度数.类似于斗地主时,手拿扑克牌的形状. 程序源码: <!DOCTYPE html> <html> <head ...