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模板题,在这 ...
随机推荐
- 使用puppet
首先配置一个默认文件 只是首次创建的时候才需要重启,后期不需要重启! [root@master manifests]# vim /etc/puppet/manifests/site.pp node d ...
- HTML干货
什么也不想说 <%@ page language="java" import="java.util.*" pageEncoding="utf-8 ...
- Windows编程之进程遍历(C++实现)
Windows编程之进程遍历 PS: 主要扣代码使用,直接滑动到最下面使用. 遍历进程需要几个API,和一个结构体 1.创建进程快照 2.遍历首次进程 3.继续下次遍历 4.进程信息结构体 API 分 ...
- AntData.ORM框架 之 DotnetCore
开源地址:https://github.com/yuzd/AntData.ORM CodeGen使用请参考http://www.cnblogs.com/yudongdong/p/6421312.h ...
- python 爬去拉钩测试招聘信息
代码如下: #coding:utf-8 import time import urllib.request from bs4 import BeautifulSoup file=open(r'meit ...
- Android 执行 adb shell 命令
Android 执行Adb shell 命令大多需要root权限,Android自带的Runtime. getRuntime().exec()容易出错,在网上找到了一个执行adb shell命令的类 ...
- XCopy命令实现增量备份
xcopy XCOPY是COPY的扩展,可以把指定的目录连文件和目录结构一并拷贝,但不能拷贝系统文件:使用时源盘符.源目标路径名.源文件名至少指定一个:选用/S时对源目录下及其子目录下的所有文件进行C ...
- JS获取前天、昨天、今天、明天、后天的时间
GetDateStr = function(AddDayCount) { var dd = new Date(); dd.setDate(dd.getDate()+AddDayCount);//获取A ...
- 纯css提示效果 提示层
<!DOCTYPE HTML PUBLIC "-//W3C//DTD xHTML 1.0 Transitional//EN"><HTML> <HEAD ...
- visual studio相关操作
1.同一个解决方案下的两个项目之间怎么相互调用 在项目的“引用”上右键,添加引用,选你要引用的项目.然后在代码里就能调用引用项目里的某个类的方法了. 2.如果一个项目类型为”类库“的项目要运行,会报如 ...