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 ...
随机推荐
- Ajax异步刷新地址栏url改变(利用Html5 history.pushState实现)
早些时候在博客园参阅了不少资料,然后决定入驻博客园分享自己的开发心得,最近准备转方向筹备着辞职交接工作,所以有点忙碌,搁置了一个月才匆匆写下这么一篇随笔,希望能给大家带来一点帮助吧,资料和学识有限,如 ...
- 关于web软件信息安全问题资料的整理(四)
整理出了几点解决方案 1.修护漏洞.对于防护的一方来看,如果先于攻击一方发现Web系统中存在的漏洞,尽早修复它们,就可以防患于未然,获得最低的防护成本.漏洞的修复方式并不是一定要依靠修改网页代码才可以 ...
- 如何申请Autodesk ReCap 360 photo的云币(Cloud Credit)
在之前的博客中我介绍过Autodesk的照片建模云服务—Autodesk ReCap 360 photo,通过Autodesk ReCap 360 photo,你可以非常方便的通过照片生成三维模型.如 ...
- 转使用chrome命令行:disable-web-security 实现浏览器跨域
注意chrome 48 版本后此方法就不能用了 推荐一个新方式来实现本地跨域调试线上资源,搜索chrome插件 Allow-Control-Allow-Origin: *.https://chrome ...
- android加固系列—4.加固前先学会破解,无源码调试apk
[版权所有,转载请注明出处.出处:http://www.cnblogs.com/joey-hua/p/5138585.html] 项目关键java代码为,将tv设置为从jni读取的字符串,这里的破解内 ...
- RecyclerView的基本使用
1.布局文件中使用 <android.support.v7.widget.RecyclerView android:id="@+id/recycleview" android ...
- swift 2.2 语法 (中)
前言: 1.此文中的语法会根据Swift的升级变动而更新. 2.如果需要请移步 -> swift2.2 语法(上).swift 2.2语法(下) 函数 和C语言一样,swift也有函数,性质和我 ...
- Android 多媒体播放API简介
本文调用android的媒体播放器实现一些音乐播放操作 项目布局: <LinearLayout xmlns:android="http://schemas.android.com/ap ...
- Android 系统API实现数据库的增删改查和SQLite3工具的使用
在<Android SQL语句实现数据库的增删改查>中介绍了使用sql语句来实现数据库的增删改查操作,本文介绍Android 系统API实现数据库的增删改查和SQLite3工具的使用. 系 ...
- iOS-UIScrollView和UIPageControl的综合实力,滚动图,轮播图
本代码主要实现图片之间的切换 目录结构 代码 ViewController.m文件 #import "ViewController.h" @interface ViewContro ...