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. VS的代码分析工具

    来自:[译]Visual Studio 2008 Code Metrics http://www.cnblogs.com/live41/archive/2010/02/08/1665627.html ...

  2. Python基础 - 获取N天前的日期

    获取今天前第N天的日期 from datetime import datetime from datetime import timedelta def get_date(days=N): retur ...

  3. SQLServer 语句-创建索引

    语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名)WITH FILLFACTOR = 填充因子值0~100GO /*实例*/USE 库名GOIF EXISTS (SELECT * ...

  4. python爬虫中文网页cmd打印出错问题解决

    问题描述 用python写爬虫,很多时候我们会先在cmd下先进行尝试. 运行爬虫之后,肯定的,我们想看看爬取的结果. 于是,我们print... 运气好的话,一切顺利.但这样的次数不多,更多地,我们会 ...

  5. CSS 动画之九-会呼吸的信封

    新年已经到来,各个网站都举办着各种不同类型的活动,'会呼吸的信封'有可能就是你遇到的其中一种.其实就是一个信封的样式,在封口处加上开合开合的动画效果,吸引用户去打开这个信封,点击后可能会送红包,优惠券 ...

  6. JS案例之7——瀑布流布局(2)

    这个例子与上一篇类似,唯一的区别是排序的方式有差别.上一篇是在高度最小的列里插入内容,这个案例是按顺序放置内容. 两种方法各有优缺点.第一种需要在图片内容加载完成的情况下有效.这个例子不需要在wind ...

  7. Linux操作系统里查看所有用户

    Xwindows界面的就不说了. 1.Linux里查看所有用户 linux里,并没有像windows的net user,net localgroup这些方便的命令来管理用户. (1)在终端里.其实只需 ...

  8. BigDecimal 使用方法详解

    BigDecimal 使用方法详解 博客分类: java基础 bigdecimalmultiplyadddivide  BigDecimal 由任意精度的整数非标度值 和 32 位的整数标度 (sca ...

  9. 讽刺的是,我在linux下使用最多的命令,竟然是windows的

    $ history | awk '{print $2}' | sort | uniq -c | sort -nr | head dir vi echo cd vim jobs gcc ls less ...

  10. 数据挖掘系列(2)--关联规则FpGrowth算法

    上一篇介绍了关联规则挖掘的一些基本概念和经典的Apriori算法,Aprori算法利用频繁集的两个特性,过滤了很多无关的集合,效率提高不少,但是我们发现Apriori算法是一个候选消除算法,每一次消除 ...