POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264
题目大意:
给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差。
思路:
依旧是线段树水题~
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=50000+10;
const int MAXM=MAXN<<2;
const int INF=0x3fffffff;
int maxv[MAXM],minv[MAXM],a[MAXN]; void build(int k,int L,int R)
{
if(L==R)
{
maxv[k]=a[L];
minv[k]=a[L];
}
else
{
int m=(L+R)>>1;
build(k<<1,L,m);
build((k<<1)+1,m+1,R);
maxv[k]=max(maxv[k<<1],maxv[(k<<1)+1]);
minv[k]=min(minv[k<<1],minv[(k<<1)+1]);
}
} int query_max(int k,int L,int R,int a,int b)
{
int ans=-INF;
if(a<=L && R<=b) return maxv[k];
else
{
int m=(L+R)>>1;
if(a<=m) ans=max(ans,query_max(k<<1,L,m,a,b));
if(m<b) ans=max(ans,query_max((k<<1)+1,m+1,R,a,b));
return ans;
}
} int query_min(int k,int L,int R,int a,int b)
{
int ans=INF;
if(a<=L && R<=b) return minv[k];
else
{
int m=(L+R)>>1;
if(a<=m) ans=min(ans,query_min(k<<1,L,m,a,b));
if(m<b) ans=min(ans,query_min((k<<1)+1,m+1,R,a,b));
return ans;
}
} int main()
{
int n,q;
while(~scanf("%d%d",&n,&q))
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]); build(1,1,n); for(int i=0;i<q;i++)
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",query_max(1,1,n,a,b)-query_min(1,1,n,a,b));
}
}
return 0;
}
POJ 3264 Balanced Lineup 线段树RMQ的更多相关文章
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- 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 ...
- poj 3264 Balanced Lineup 区间极值RMQ
题目链接:http://poj.org/problem?id=3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) alw ...
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
- POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 53703 Accepted: 25237 ...
随机推荐
- H+后台主题UI框架---整理
本篇文章是对H+这种框架进行整理,顺便了解一下标准的代码规范的写法. 一.表单: 1).下面是一个基本表单: 现在来看这个表单的结构: 1.整个表单的外框结构是一个div,至于padding和marg ...
- ORACLE-016:ora-01720 授权选项对于'xxxx'不存在
报错的情形例如以下. A用户:视图V_A B用户:视图V_B,而且用到了V_A C用户:须要用V_B, 授权过程, A用户下: grant select on V_A to B B用户下: grant ...
- ubuntu14.04-安装flash
最近打开搜狐等在线视频,都提示我说flash没有安装,然后就点击安装.进入安装界面 如下所示 然后我们选择版本.之前我一直都是选择版本APT,因为里面标注的是适用于ubuntu10.04+,这时候它会 ...
- web api 特点
webapi有很多特点(我不想用优点这个词),比如说restful,支持路由,简单,类似mvc controller/action的代码编写方式,灵活的托管方式,和web的集成等等. Web API的 ...
- Codefroces Educational Round 26 837 C. Two Seals
C. Two Seals time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- 什么是事件委托?jquery和js怎么去实现?
事件委托又叫事件代理,事件委托就是利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件. js: window.onload = function(){ var oul = docume ...
- offSet和client和scroll
这三个是是js盒模型属性 client clientWidth 内容宽度加上左右padding clientHeight 内容高度加上上下padding clientTop 上边框的宽度 client ...
- sim800c GPRS模块的透传模式
一.透传模式 基于sim800c GPRS模块在建立TCP/IP连接情况下,可以设置进入透传模式,用来接收和发送数据,一旦进入即从串口收到的数据将被打包,然后发送.接收同理. 注意在透传模式下所有的A ...
- UVALive 6527 Counting ones dfs(水
题目链接:点击打开链接 #include <cstdio> #include <vector> using namespace std; typedef long long l ...
- Makefile 文件格式
Makefile包含 目标文件.依赖文件.可运行命令三部分. 每部分的基本格式例如以下: test: prog.o code.o gcc -o test prog.o code.o 当中 ...