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] SharePoint 错误集 1
1. Delete a site collection · Run command : Remove-SPSite –Identity http://ent132.sharepoint.hp.com/ ...
- Android 实现图片画画板
本文主要讲述了Android 实现图片画画板 设计项目布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk ...
- 【Android】监听Notification被清除
前言 一般非常驻的Notification是可以被用户清除的,如果能监听被清除的事件就可以做一些事情,比如推送重新计数的问题. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www ...
- IntelliJ IDEA 使用总结[zz]
本文转自:http://cowboy-bebop.iteye.com/blog/1035550,仅做稍微整理,转载请注明出处. 1. IDEA内存优化 因机器本身的配置而配置: \IntelliJ I ...
- 安卓+servlet+MySql 查询+插入(汉字乱码解决)
问题: 安卓程序,通过servlet连接MySQL数据库,并实现查询和插入(修改,删除类似). 其中遇到的最大的问题是:汉字乱码问题(查询条件有汉字乱码.servlet的汉字到数据乱码.安卓通过ser ...
- SQL Server字符串左匹配
在SQL Server中经常会用到模糊匹配字符串的情况,最简单的办法就是使用like关键字(like语法http://msdn.microsoft.com/en-us/library/ms179859 ...
- [转]正确配置Linux系统ulimit值的方法
在Linux下面部署应用的时候,有时候会遇上Socket/File: Can't open so many files的问题:这个值也会影响服务器的最大并发数,其实Linux是有文件句柄限制的,而且L ...
- SVN Unable to connect to a repository at UR
背景: 1. SVN服务器:VisualSVN-Server-2.5.5: 2. SVN客户端:TortoiseSVN-1.7.6.22632-x64-svn-1.7. ...
- 地图编辑器V1
功能:1. 拖放地图元素至地图编辑器中2. 可设置参考图3. 背景图可平铺,中央,左上角等放置4. 地图上的元素可旋转,可删除,可拖动5. 保存地图数据时同时生成XML格式的地图信息 -------- ...
- 学习OpenStack之 (2):Cinder LVM 配置
0.背景 OpenStack 中的实例是不能持久化的,cinder服务重启,实例消失.如果需要挂载 volume,需要在 volume 中实现持久化.Cinder提供持久的块存储,目前仅供给虚拟机挂载 ...