poj 3264
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 44594 | Accepted: 20931 | |
| 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
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
Source
简单的区间求最值。
Problem: 3264
User: ksq2013
Memory: 1736K
Time: 3829MS
Language: G++
Result: Accepted
#include<cstdio>
#include<iostream>
using namespace std;
int n,q,maxnum[201000],minnum[201000];
void build(int s,int t,int k)
{
if(!(s^t)){
scanf("%d",&maxnum[k]);
minnum[k]=maxnum[k];
return;
}int m=(s+t)>>1;
build(s,m,k<<1);
build(m+1,t,k<<1|1);
maxnum[k]=max(maxnum[k<<1],maxnum[k<<1|1]);
minnum[k]=min(minnum[k<<1],minnum[k<<1|1]);
}
int query1(int s,int t,int k,int l,int r)
{
if(l<=s&&t<=r)return maxnum[k];
int m=(s+t)>>1,res=0;
if(l<=m)res=query1(s,m,k<<1,l,r);
if(m<r)res=max(res,query1(m+1,t,k<<1|1,l,r));
return res;
}
int query2(int s,int t,int k,int l,int r)
{
if(l<=s&&t<=r)return minnum[k];
int m=(s+t)>>1,res=1000100;
if(l<=m)res=query2(s,m,k<<1,l,r);
if(m<r)res=min(res,query2(m+1,t,k<<1|1,l,r));
return res;
}
int main()
{
scanf("%d%d",&n,&q);
build(1,n,1);
for(;q;q--){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",query1(1,n,1,a,b)-query2(1,n,1,a,b));
}
return 0;
}
poj 3264的更多相关文章
- poj 3264 Balanced Lineup (RMQ)
/******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...
- POJ 3264 Balanced Lineup(RMQ)
点我看题目 题意 :N头奶牛,Q次询问,然后给你每一头奶牛的身高,每一次询问都给你两个数,x y,代表着从x位置上的奶牛到y位置上的奶牛身高最高的和最矮的相差多少. 思路 : 刚好符合RMQ的那个求区 ...
- POJ 3264 RMQ裸题
POJ 3264 题意:n个数,问a[i]与a[j]间最大值与最小值之差. 总结:看了博客,记下了模板,但有些地方还是不太理解. #include<iostream> #include&l ...
- poj 3264 & poj 3468(线段树)
poj 3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0 求任一区间的最大值和最小值的差 #include<io ...
- poj 3264 Balanced Lineup 题解
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Subm ...
- poj 3264 Balanced Lineup【RMQ-ST查询区间最大最小值之差 +模板应用】
题目地址:http://poj.org/problem?id=3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0分析:标 ...
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
- poj 3264(线段树)
http://poj.org/problem?id=3264 初学线段可以做的水题,也是线段树的基础运用.也是我的第一个线段树的题. 题意:在区间范围内的最大值减去最小值 思路:线段树记录下每个区间内 ...
- POJ——3264线段树
题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,00 ...
- POJ 3264 区间最大最小值Sparse_Table算法
题目链接:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total ...
随机推荐
- SharePoint Online 创建门户网站系列之导航
前 言 关于SharePoint Online中的导航,基本是由Html + Css + JavaScript组成效果以及样式,然后在后台列表或者SharePoint Online元数据导航当做数据源 ...
- myeclipse 2015 CI 16发布【附下载】
2015升级版再次来袭! 更新日志: Slack Integration 新版本集成了Slack,你只需要注册一个Slack帐号然后就可以发送和接收代码片段.你甚至不需要登录Slack就可以直接在Ec ...
- 编写高质量的Objective-C代码
点标记语法 属性和幂等方法(多次调用和一次调用返回的结果相同)使用点标记语法访问,其他的情况使用方括号标记语法. 良好的风格: view.backgroundColor = [UIColor or ...
- Android Testing学习02 HelloTesting 项目建立与执行
Android Testing学习02 HelloTesting 项目建立与执行 Android测试,分为待测试的项目和测试项目,这两个项目会生成两个独立的apk,但是内部,它们会共享同一个进程. 下 ...
- 停止运行ExecutorService中的线程
while(true){ try { sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch bloc ...
- iOS开发之邓白氏编码申请流程
要申请企业证书,必须先申请邓白氏编码,在苹果网站有一个免费申请邓白氏编码的链接:https://developer.apple.com/program/enroll/dunsLookupForm.ac ...
- 【问题排查记录】Field 'id' doesn't have a default value;
错误信息: org.springframework.dao.DataIntegrityViolationException: StatementCallback; SQL [delete from t ...
- Atitit.论图片类型 垃圾文件的识别与清理 流程与设计原则 与api概要设计 v2 pbj
Atitit.论图片类型 垃圾文件的识别与清理 流程与设计原则 与api概要设计 v2 pbj 1. 俩个问题::识别垃圾文件与清理策略1 2. 如何识别垃圾图片1 2.1. 体积过小文件<1 ...
- Java注解一谈
阅读目录 1.元注解 2.自定义注解 3.注解处理器 android注解框架解析 我们经常会在java代码里面看到:“@Override”,“@Target”等等样子的东西,这些是什么? 在java里 ...
- YARN中自己总结的几个关键点
以前在Hadoop 1.0中JobTracker主要完成两项功能:资源的管理和作业控制.在集群规模过大的场景下,JobTracker 存在以下不足: 1)JobTracker 单点故障. 2)JobT ...