poj 3264 线段树 求区间最大最小值
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
Output
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<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#define INF 0xfffffff
using namespace std;
struct tree
{
int L;
int R;
int minv;
int maxv;
int mid()
{
return (L+R)/;
}
}tree[];
int maxv=-INF;
int minv=INF;
void buildtree(int root,int L,int R)
{
tree[root].L=L;
tree[root].R=R;
tree[root].minv=INF;
tree[root].maxv=-INF;
if(L!=R)
{
buildtree(*root+,L,(L+R)/);
buildtree(*root+,(L+R)/+,R);
}
}
void inse(int root,int i,int v)
{
if(tree[root].L==tree[root].R)
{
tree[root].minv=v;
tree[root].maxv=v;
return ;
}
tree[root].minv=min(tree[root].minv,v);
tree[root].maxv=max(tree[root].maxv,v);
if(i<=tree[root].mid())
inse(*root+,i,v);
else
inse(*root+,i,v);
}
void query(int root ,int s,int e)
{
if(tree[root].minv>minv&&tree[root].maxv<maxv)
return ;
if(tree[root].L==s&&tree[root].R==e)
{
minv=min(minv,tree[root].minv);
maxv=max(maxv,tree[root].maxv);
return ;
}
if(e<=tree[root].mid())
query(*root+,s,e);
else if(s>tree[root].mid())
query(*root+,s,e);
else
{
query(*root+,s,tree[root].mid());
query(*root+,tree[root].mid()+,e);
}
}
int main()
{
int n,q;
int re;
int ss,ee;
scanf("%d%d",&n,&q);
buildtree(,,n);
for(int i=; i<=n; i++)
{
//cout<<"**********"<<endl;
scanf("%d",&re);
inse(,i,re);
}
for(int j=; j<=q; j++)
{
scanf("%d%d",&ss,&ee);
minv=INF;
maxv=-INF;
query(,ss,ee);
printf("%d\n",maxv-minv);
} return ;
}
poj 3264 线段树 求区间最大最小值的更多相关文章
- POJ3264(线段树求区间最大最小值)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41162 Accepted: 19327 ...
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- xdoj-1324 (区间离散化-线段树求区间最值)
思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i] 覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...
- hdu 1754 I Hate It (线段树求区间最值)
HDU1754 I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- POJ - 3264 线段树模板题 询问区间最大最小值
这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...
- POJ 3264 线段树入门解题报告
题意:给n个值, Q次询问, 每次询问给定一个区间, 要求输出该区间最大最小值之差 思路:暴力的话每次询问都要遍历多次for循环一定会超时, 用线段树记录区间的信息(左边界右边界, 该区间最大值最小值 ...
- 【线段树求区间第一个不大于val的值】Lpl and Energy-saving Lamps
https://nanti.jisuanke.com/t/30996 线段树维护区间最小值,查询的时候优先向左走,如果左边已经找到了,就不用再往右了. 一个房间装满则把权值标记为INF,模拟一遍,注意 ...
- POJ——3264线段树
题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,00 ...
- 滑动窗口(poj,线段树维护区间最值)
题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...
随机推荐
- FF与IE对JavaScript和CSS的区别
一.FF与IE对JavaScript的区别 1. document.formName.item("itemName") 问题 说明:IE下,可以使用document.formNam ...
- 浅析Oracle范式的概念(转载)
范式:英文名称是 Normal Form,它是英国人 E.F.Codd(关系数据库的老祖宗)在上个世纪70年代提出关系数据库模型后总结出来的,范式是关系数据库理论的基础,也是我们在设计数据库结构过程中 ...
- mysql的binlog
mysql> show global variables like '%bin%'; +---------------------------------+------------------- ...
- HDU 4036 存疑题目,数论 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=4036 一开始以为需要用斜抛,结果发现只需要用能量守恒定律?+与最大速度的坏土豆速度保持一致 #include & ...
- Android利用ContentProviderOperation添加联系人
Android添加联系人有两种方式: 1. 直接调用插入语句,先插入一个空Item,得到一个id,然后给这个id对应的插入其他信息,如姓名,号码,邮件等: 2. 利用ContentProviderOp ...
- mouseOver与rollOver
区别: 当父容器监听这两个事件,鼠标从父容器移到子容器再移回父容器时,会触发mouseOver.mouseout事件,但是不会触发rollover.rollout事件.
- 【iOS开发】企业版证书($299)In-House方式发布指南 (转)
一.明确几个概念 1.企业版IDP:即iOS Development Enterprise Program.注意是$299/Year那种,并不是$99/Year的那种. 2.In House:是只企业 ...
- Ogre中TerrainSceneManager
转自:http://blog.csdn.net/yanonsoftware/article/details/1103665 TerrainSceneManager是一个OctreeSceneManag ...
- hdu 2055
PS:上课的时候敲的..这道题简单..一次AC,不多说了.. 代码: #include "stdio.h"int main(){ int i,n,y; char x,a[26],b ...
- SQL2008:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。
错误: 解决方案: 1.查看服务是否开启,如果没有开启,请开启服务. 2.服务器名称栏:格式:主机名\实例名或者ip\实例名 如:10.10.4.81\SQLSERVER