poj3264 balanced lineup【线段树】
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
两棵树分别保存区间最大最小值即可
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define inf 1e18
using namespace std;
int q, n;
const int maxn = 50005;
int cow[maxn], talltree[maxn << 2], shorttree[maxn<<2];
void tall_pushup(int rt)//更新
{
talltree[rt] = max(talltree[rt << 1], talltree[rt << 1 | 1]);
}
void short_pushup(int rt)
{
shorttree[rt] = min(shorttree[rt<<1], shorttree[rt<<1|1]);
}
void tall_build(int l, int r, int rt)
{
if(l == r){
talltree[rt] = cow[l];
return;
}
int m = (l + r) >> 1;
tall_build(l, m, rt << 1);
tall_build(m + 1, r, rt << 1 | 1);
tall_pushup(rt);
}
void short_build(int l, int r, int rt)
{
if(l == r){
shorttree[rt] = cow[l];
return;
}
int m = (l + r) >> 1;
short_build(l, m, rt << 1);
short_build(m + 1, r, rt << 1 | 1);
short_pushup(rt);
}
/*void pushdown(int rt, int ln, int rn)
{
if(lazy[rt]){
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
tree[rt << 1] += lazy[rt] * ln;
tree[rt << 1 | 1] += lazy[rt] * rn;
lazy[rt] = 0;
}
}
void update(int L, int C, int l, int r, int rt)
{
if(l == r){
tree[rt] += C;
return;
}
int m = (l + r) >>1;
if(L <= m) update(L, C, l, m, rt << 1);
else update(L, C, m + 1, r, rt << 1 | 1);
pushup(rt);
}
void update(int L, int R, int C, int l, int r, int rt)
{
if(L <= l && r <= R){//本区间完全在操作区间内
tree[rt] += C * (r - l + 1);
lazy[rt] += C;
return;
}
int m = (l + r) >> 1;
pushdown(rt, m - l + 1, r - m);
if(L <= m) update(L, R, C, l, m, rt << 1);
if(R > m) update(L, R, C, m + 1, r, rt << 1 | 1);
pushup(rt);
}*/
int tall_query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R){
return talltree[rt];
}
int m = (l + r) >> 1;
//pushdown(rt, m - l + 1, r - m);
int ans = 0;
if(L <= m) ans = max(ans, tall_query(L, R, l, m, rt << 1));
if(R > m) ans = max(ans, tall_query(L, R, m + 1, r, rt << 1 | 1));
return ans;
}
int short_query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R){
return shorttree[rt];
}
int m = (l + r) >> 1;
//pushdown(rt, m - l + 1, r - m);
int ans = inf;
if(L <= m) ans = min(ans, short_query(L, R, l, m, rt << 1));
if(R > m) ans = min(ans, short_query(L, R, m + 1, r, rt << 1 | 1));
return ans;
}
int main()
{
while(scanf("%d%d", &n, &q) != EOF){
for(int i = 1; i <= n; i++){
scanf("%d", &cow[i]);
}
tall_build(1, n, 1);
short_build(1, n, 1);
while(q--){
int a, b;
scanf("%d%d", &a, &b);
cout<<tall_query(a, b, 1, n, 1) - short_query(a, b, 1, n, 1)<<endl;
}
}
return 0;
}
poj3264 balanced lineup【线段树】的更多相关文章
- POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值
题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...
- POJ3264 Balanced Lineup 线段树区间最大值 最小值
Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 772 Solved: 560线 ...
- poj3264 Balanced Lineup(树状数组)
题目传送门 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 64655 Accepted: ...
- 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 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- POJ 3264 Balanced Lineup (线段树)
Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...
随机推荐
- python类和模块区别,python命名空间
在python中,类可以提供模块级别之下的命名空间. 如果一个模块写很多函数,某些函数之间共同完成一组功能,用类会看起来更清晰,在调用时候也会更好,对于ide补全有更小范围的限定提示. 类提供 继承 ...
- 字符串中包含汉字和\u,显示出汉字来
针对py2,py3不需要这样.#coding=utf8import sysreload(sys)sys.setdefaultencoding('utf8') strx2='你好\u4e2d\u56fd ...
- jquery获取元素颜色css('color')的值返回RGB
css代码如下: a, a:link, a:visited { color:#4188FB; } a:active, a:focus, a:hover { color:#FFCC00; } js代码如 ...
- php根据地理坐标获取国家、省份、城市,及周边数据类
功能:当App获取到用户的地理坐标时,可以根据坐标知道用户当前在那个国家.省份.城市,及周边有什么数据. 原理:基于百度Geocoding API 实现,需要先注册百度开发者,然后申请百度AK(密钥) ...
- AngularJS------Error: Cannot find module '@angular-devkit/core'
如图: 解决方法: 进入项目目录下执行以下代码 npm i --save-dev @angular-devkit/core
- phonegap入门–3 Android phonegap 自定义插件DEMO
一.环境要求: 首先需要建立phonegap android 工程,请参考:http://www.cnblogs.com/zhujinguo/p/4369883.html 二.建立java类 ...
- ios开发之--[_NSInlineData objectForKeyedSubscript:]
reason: '-[_NSInlineData objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7fa2049 ...
- wordpress for sae
帮人建个站,准备用sae+wordpess,小研究一下 http://sae.sina.com.cn/?m=apps&a=detail&aid=1 http://wp4sae.org/ ...
- Linux命令之乐--wget
常见参数: 参数 作用 -b 后台下载模式. -P 下载到指定目录. -t 最大尝试次数. -c 断点续传 -p 下载页面内所有资源,包括图片.视频等. -r 递归下载 实例1,下载本站的所有文章 ...
- VS05 VS08 VS10 工程之间的转换
VS05 VS08 VS10 工程之间的转换 安装了VS2010后,用它打开以前的VS2005项目或VS2008项目,都会被强制转换为VS2010的项目,给没有装VS2010的电脑带来不能打开高版本项 ...