直接写个RMQ就能过。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Maxn 60010
using namespace std;
int maxnum[Maxn][],minnum[Maxn][],n,Log[Maxn+];
int Log2(int x)
{
int num=;
x/=;
while(x)
{
num++;
x/=;
}
return num;
}
void RMQ()
{
int i,j;
for(j=;j<;j++)
for(i=;i<=n;i++)
if(i+(<<j)-<=n)
{
maxnum[i][j]=Max(maxnum[i][j-],maxnum[i+(<<(j-))][j-]);
minnum[i][j]=Min(minnum[i][j-],minnum[i+(<<(j-))][j-]);
}
}
int main()
{
int i,j,q,a,b;
for(i=;i<=;i++)
Log[i]=Log2(i);
while(scanf("%d%d",&n,&q)!=EOF)
{ for(i=;i<=n;i++)
{
scanf("%d",&a);
maxnum[i][]=minnum[i][]=a;
}
RMQ();
for(i=;i<=q;i++)
{
scanf("%d%d",&a,&b);
int k=Log[b-a+];
printf("%d\n",Max(maxnum[a][k],maxnum[b-(<<k)+][k])-Min(minnum[a][k],minnum[b-(<<k)+][k]));
}
}
return ;
}

poj 3264 RMQ的更多相关文章

  1. POJ 3264 RMQ裸题

    POJ 3264 题意:n个数,问a[i]与a[j]间最大值与最小值之差. 总结:看了博客,记下了模板,但有些地方还是不太理解. #include<iostream> #include&l ...

  2. poj 3264 RMQ 水题

    题意:找到一段数字里最大值和最小值的差 水题 #include<cstdio> #include<iostream> #include<algorithm> #in ...

  3. POJ 3264 RMQ水题

    题目大意就是有很多牛.告诉你每只牛的高度.然后有很多个询问.输出该区间内的最大身高差.也就是用RMQ求最大值最小值.貌似还可以用线段树.然而,我还不会线段树.....T_T 可能是太多组数据了.cin ...

  4. poj 3264(RMQ或者线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 42929   Accepted: 20184 ...

  5. POJ 3264 RMQ问题 用dp解决

    #include <cstdio> #include <cstring> #include <iostream> using namespace std; ; #d ...

  6. poj 3264 Balanced Lineup (RMQ)

    /******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...

  7. POJ 3264 Balanced Lineup(RMQ)

    点我看题目 题意 :N头奶牛,Q次询问,然后给你每一头奶牛的身高,每一次询问都给你两个数,x y,代表着从x位置上的奶牛到y位置上的奶牛身高最高的和最矮的相差多少. 思路 : 刚好符合RMQ的那个求区 ...

  8. Poj 3264 Balanced Lineup RMQ模板

    题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...

  9. 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 ...

随机推荐

  1. HDU 4919 Exclusive or (数论 or 打表找规律)

    Exclusive or 题目链接: http://acm.hust.edu.cn/vjudge/contest/121336#problem/J Description Given n, find ...

  2. HDU 5512 Pagodas (2015沈阳现场赛,找规律+gcd)

    Pagodas Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  3. Spring入门(5)-自动装配Bean属性

    Spring入门(5)-自动装配Bean属性 本文介绍如何装配Bean属性. 0. 目录 ByName ByType constructor 默认自动装配 混合使用自动装配和显示装配 1. ByNam ...

  4. BPL插件框架的二种实现

    1)非RTTI方式适用于所有的DELPHI版本 unit untMain; interface uses Windows, Messages, SysUtils, Classes, Graphics, ...

  5. [iOS基础控件 - 6.10] Notification 通知机制

    A.定义      iOS程序都有一个NSNotificationCenter的单例对象,用来负责发布不同对象之间的通知      任何对象都能够在NSNotificationCenter发布通知,发 ...

  6. oracle的substr和replace

    //我个人做的是更新表中某个字段下的所有内容带有中文括号的信息变为英文括号,具体做法如下 update 表名 set 列名 =replace(要修改的字段名,要替换掉的内容,要替换上去的新内容) su ...

  7. 对于一个网站,如何禁止直接从浏览器Web browser中访问js文件

    比如有一个网站,https://testsystem.infotest.com 在这个网站的内容文件目录下面,有一个scripts文件夹,该文件夹中有一个js文件,比如lukeTest.js文件 这样 ...

  8. final static T

    /** * An empty table instance to share when the table is not inflated. */ static final Entry<?,?& ...

  9. CSS3之背景剪裁Background-clip

    CSS3之背景剪裁Background-clip是CSS3中新添加的内容.这个属性还是比较简单的,主要分五个属性值:border.padding.content.no-clip和text.下面将针对这 ...

  10. 离散信号MATLAB频谱分析程序

    from http://blog.csdn.net/u012129372/article/details/26565611 %FFT变换,获得采样数据基本信息,时域图,频域图 %这里的向量都用行向量, ...