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模板题,在这 ...
随机推荐
- mouseout、mouseover和mouseleave、mouseenter区别
今天在使用鼠标事件时,用错了mouseout,于是做个测试总结. 结论: mouseenter:当鼠标移入某元素时触发. mouseleave:当鼠标移出某元素时触发. mouseover:当鼠标移入 ...
- C:数据结构与算法之单链表
单链表相对于顺序表比较难理解,但是比较实用,单链表的插入,删除不需要移动数据元素,只需要一个指针来寻找所需要的元素,还有一个大优点就是不浪费空间,当你想要增加一个结点可以申请(malloc())一个结 ...
- Spring事务管理总结
本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 本文对应慕课网上课程Spring事务管理,详情可查看:点我 1: 概 ...
- MST系列
1.POJ2485 Highways 蛮水的 数组一开始开小了卡了一会儿 我可能是个傻逼 #include<iostream> #include<cstdio> #includ ...
- bzoj 4289: PA2012 Tax
Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边 ...
- css弹性盒子新旧兼容
前言:本篇随笔是对弹性盒子有了解的人来写的这篇文章,具体属性产生的效果这里不做说明,基础的东西去查文档.这里只是总结. 时至今日,css3的flex弹性盒子在移动端基本上都是支持的,但不排除有些些低版 ...
- 模板引擎(smarty)知识点总结
首先我们必须知道使用smarty的流程 1.引入 2.实例化 3.配置 模板目录 编译目录 3.0版本 支持不存在的目录则新建 4.赋值 5.编译显示 某个模板文件(暗示需要哪 ...
- lambda表达式不使用委托(delegate) 用FUNC
lLambda不使用delegate关键字,而使用 Lambda运算符 => goes to l 1.Func<int,string> getInput = (int age ...
- SQL语句 我喜欢上海
select * from [user] wherer name like '上海%'
- java获取properties配置文件值
package me.ilt.Blog.util; import java.io.File; import java.io.FileInputStream; import java.io.IOExce ...