BZOJ-1699 Balanced Lineup 线段树区间最大差值
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 线段树区间最大差值的更多相关文章
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- POJ3264 Balanced Lineup 线段树区间最大值 最小值
Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...
- hdu4521-小明系列问题——小明序列(线段树区间求最值)
题意:求最长上升序列的长度(LIS),但是要求相邻的两个数距离至少为d,数据范围较大,普通dp肯定TLE.线段树搞之就可以了,或者优化后的nlogn的dp. 代码为 线段树解法. #include ...
- 2017 Wuhan University Programming Contest (Online Round) D. Events,线段树区间更新+最值查询!
D. Events 线段树区间更新查询区间历史最小值,看似很简单的题意写了两天才写出来. 题意:n个数,Q次操作,每次操作对一个区间[l,r]的数同时加上C,然后输出这段区间的历史最小值. 思路:在线 ...
- hdu 1754 I Hate It(线段树区间求最值)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 772 Solved: 560线 ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
随机推荐
- Java语法基础(三)----选择结构的if语句、switch语句
[前言] 流程控制语句: 在一个程序执行的过程中,各条语句的执行顺序对程序的结果是有直接影响的.也就是说程序的流程对运行结果有直接的影响.所以,我们必须清楚每条语句的执行流程.而且,很多时候我们要通过 ...
- Jenkins学习七:Jenkins的授权和访问控制
默认的Jenkins不包含任何的安全检查,任何人可以修改Jenkins设置,job和启动build等.显然地在大规模的公司需要多个部门一起协调工作的时候,没有任何安全检查会带来很多的问题. 在系统管理 ...
- Oracle导入导出dmp文件
目 录 目 录...3 1 说明...3 2 导出dmp文件...3 3 导入dmp文件...5 3.1 环境准备...5 ...
- 010医疗项目-模块一:用户添加的实现(Dao,Service,Action,增加页面调试,提交页面调试)
要实现的效果:
- 项目移植将eclipse里面的项目移植到intellij idea里面
怎么关联多个库 . A B C D,A依赖BC,D依赖A,怎么搞? 注意: as和idea里面,project是工作空间的意思,这里面model才是项目. 打开主项目D 打开已经存在的model 导 ...
- UI设计师零基础入门到精通精品视频教程【155课高清完整版】
[福吧资源网分享]课程是非常完整的,也是非常零基础的,适合任何学员,有需要的可以下载看看!课程目录:第1章 Adobe Photoshop CS6课时1 Adobe Photoshop CS6入门基础 ...
- [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表
4.4 Given a binary tree, design an algorithm which creates a linked list of all the nodes at each de ...
- [CareerCup] 10.4 Find All Duplicates Elements 寻找所有的重复项
10.4 You have an array with all the numbers from 1 to N, where N is at most 32,000. The array may ha ...
- initializer for conditional binding must have optional type not AVAudioPlayer
if let buttonBeep = self.setupAudioPlayerWithFile("ButtonTap", type: "wav") { ...
- python机器学习《入门》
写在前面的废话: 好吧,不得不说鱼C的markdown文本编辑器挺不错的,功能齐全.再次感谢小甲鱼哥哥的python视频让我去年大三下学期的时候入门了编程,爱上了编程这门语言,由于是偏冷门的统计学,在 ...