POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】
Balanced Lineup
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 53703 | Accepted: 25237 | |
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
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
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
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define maxsize 200020
typedef struct
{
int left,right;
int maxn;
int minn;
}Node;
int n,m;
int Max,Min;
int num[maxsize];
Node tree[maxsize*];
inline void buildtree(int root,int left,int right)// 构建线段树
{
int mid;
tree[root].left=left;
tree[root].right=right;// 当前节点所表示的区间
if(left==right)// 左右区间相同,则此节点为叶子,max 应储存对应某个学生的值
{
tree[root].maxn=num[left];
tree[root].minn=num[left];
return;
}
mid=(left+right)/;
//int a,b;// 递归建立左右子树,并从子树中获得最大值
buildtree(*root,left,mid);
buildtree(*root+,mid+,right);
tree[root].maxn=max(tree[root*].maxn,tree[root*+].maxn);
tree[root].minn=min(tree[root*].minn,tree[root*+].minn);
}
inline void find(int root,int left,int right)// 从节点 root 开始,查找 left 和 right 之间的最大值
{
int mid;
//if(tree[root].left>right||tree[root].right<left)// 若此区间与 root 所管理的区间无交集
//return;
if(left==tree[root].left&&tree[root].right==right)// 若此区间包含 root 所管理的区间
{
Max=max(tree[root].maxn,Max);
Min=min(tree[root].minn,Min);
return;
}
mid=(tree[root].left+tree[root].right)/;
if(right<=mid)
find(root*,left,right);
else if(left>mid)
find(root*+,left,right);
else
{
find(root*,left,mid);
find(root*+,mid+,right);
//tree[root].maxn=max(tree[root*2].maxn,tree[root*2+1].maxn);
//tree[root].minn=min(tree[root*2].minn,tree[root*2+1].minn);
//return;
}
} int main()
{
//char c;
int i;
int x,y;
//scanf("d%d",&n,&m);
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=;i<=n;i++)
scanf("%d",&num[i]);
buildtree(,,n);
for(i=;i<=m;i++)
{
//getchar();
Max=-;
Min= ;
scanf("%d%d",&x,&y);
//if(c=='Q')
//printf("%d\n",find(1,x,y));
//else
//{
// num[x]=y;
// update(1,x,y);
//}
find(,x,y);
printf("%d\n",Max-Min);
}
}
return ;
}
POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】的更多相关文章
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- 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 (线段树)
Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...
- POJ - 3264 Balanced Lineup 线段树解RMQ
这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值
题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
随机推荐
- 【java】System成员输入输出功能out、in、err
package System输入输出; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOu ...
- JavaScript类型比较
JavaScript的类型 原始类型: number string boolean null undefined 对象类型: Object function Array Date ... 隐式转换 + ...
- http中的get和post(一)
GET和POST有什么区别?及为什么网上的多数答案都是错的. 如果有人问你,GET和POST,有什么区别?你会如何回答? 我的经历 前几天有人问我这个问题.我说GET是用于获取数据的,POST,一般用 ...
- iOS 数据加密方案
iOS安全攻防(二十三):Objective-C代码混淆 提交用户的隐私数据 一定要使用POST请求提交用户的隐私数据GET请求的所有参数都直接暴露在URL中请求的URL一般会记录在服务器的访问日志中 ...
- jsp程序设计:jstl之JSTL标签库
转载自:http://www.blogjava.net/haizhige/archive/2008/10/26/236783.html,个人进行了一些修改. 前言:写一个taglib一般可以继承Sim ...
- Java I/O---添加属性和有用的接口—FilterlnputStream&FilterOutputStream
0.装饰器模式 Java I/O类库需要多种不同功能的组合,这正是使用装饰器模式的理由所在.这也是Java I/O类库里存在filter(过滤器)类的原因所在,抽象类filter是所有装饰器类的基类. ...
- 测试xss
<script>window.onload=function(){ alert('加载完毕');}</script>
- swig官方go Examples 源码勘误
勘误 在官网下载页面(http://www.swig.org/download.html )下载的swigwin-3.0.12包中go示例源码有个错误(swigwin-3.0.12\Examples\ ...
- Django学习日记02_项目环境
创建一个工程: django-admin.py startproject mySite 将会产生以下文件: mySite/ manage.py mySite/ __init_ ...
- 微信小程序开发之picker选择器组件用法
picker组件时一个从底部弹起的可滚动的选择器(嵌入页面滚动器组件picker-view查看https://mp.weixin.qq.com/debug/wxadoc/dev/component/p ...