RMQ..

-------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
 
#define rep( i , n ) for( int i = 0 ; i < n ; ++i )
#define clr( x , c ) memset( x , c , sizeof( x ) )
 
using namespace std;
 
const int INF = int( 1e8 );
const int maxn = 50000 + 5;
const int maxlog = 18;
 
int Max[ maxn ][ maxlog ] , Min[ maxn ][ maxlog ];
int n;
 
inline int read() {
    char c = getchar();
while( ! isdigit( c ) ) c = getchar();
int ans = 0;
while( isdigit( c ) ) {
   ans = ans * 10 + c - '0';
c = getchar();
}
return ans;
}
 
void RMQ_init() {
for( int i = 1 ; ( 1 << i ) <= n ; ++i )
   for( int j = 0; j + ( 1 << i ) <= n ; j++ )
       Max[ j ][ i ] = max( Max[ j ][ i - 1 ] , Max[ j + ( 1 << ( i - 1 ) ) ][ i - 1 ] ) ,
       Min[ j ][ i ] = min( Min[ j ][ i - 1 ] , Min[ j + ( 1 << ( i - 1 ) ) ][ i - 1 ] );
       
}
 
int Query( int l , int r ) {
int MIN = INF , MAX = -INF;
int log = 0;
while( ( 1 << ( log + 1 ) ) <= r - l + 1 ) log++;
MAX = max( Max[ l ][ log ] , Max[ r - ( 1 << log ) + 1 ][ log ] );
MIN = min( Min[ l ][ log ] , Min[ r - ( 1 << log ) + 1 ][ log ] );
return MAX - MIN;
}
 
int main() {
freopen( "test.in" , "r" , stdin );
int q;
cin >> n >> q;
rep( i , n ) 
   Max[ i ][ 0 ] = Min[ i ][ 0 ] = read();
   
RMQ_init();
while( q-- ) {
int l = read() - 1, r = read() - 1;
printf( "%d\n" , Query( l , r ) );
}
return 0;
}

-------------------------------------------------------------------------------

1699: [Usaco2007 Jan]Balanced Lineup排队

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 1038  Solved: 641
[Submit][Status][Discuss]

Description

每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的选择和所有牛的身高 (1 <= 身高 <= 1,000,000). 他想知道每一组里面最高和最低的牛的身高差别. 注意: 在最大数据上, 输入和输出将占用大部分运行时间.

Input

* 第一行: N 和 Q. * 第2..N+1行: 第i+1行是第i头牛的身高.

* 第N+2..N+Q+1行: 两个整数, A 和 B (1 <= A <= B <= N), 表示从A到B的所有牛.

Output

*第1..Q行: 所有询问的回答 (最高和最低的牛的身高差), 每行一个.

Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0

HINT

Source

BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )的更多相关文章

  1. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队

    1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. ...

  2. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MB Description 每天,农夫 John ...

  3. ST表 || RMQ问题 || BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队 || Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup

    题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解: ST表板子 代码: #include<cstdio> #include<cstring&g ...

  4. BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树

    题意:链接 方法:线段树 解析: 题意即题解. 多次询问区间最大值与最小值的差.显然直接上线段树或者rmq维护区间最值就可以. 代码: #include <cstdio> #include ...

  5. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队【st表||线段树】

    要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; c ...

  6. 【BZOJ】1699: [Usaco2007 Jan]Balanced Lineup排队(rmq/树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1699 我是用树状数组做的..rmq的st的话我就不敲了.. #include <cstdio& ...

  7. 【BZOJ】1699 [Usaco2007 Jan]Balanced Lineup排队

    [算法]线段树 #include<cstdio> #include<cctype> #include<algorithm> using namespace std; ...

  8. BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 933  Solved: 56 ...

  9. bzoj1699[Usaco2007 Jan]Balanced Lineup排队*&bzoj1636[Usaco2007 Jan]Balanced Lineup*

    bzoj1699[Usaco2007 Jan]Balanced Lineup排队 bzoj1636[Usaco2007 Jan]Balanced Lineup 题意: 询问区间最大值减区间最小值的差. ...

随机推荐

  1. Day4_代码重用与函数

    知识点速记: 重用代码的方法:脚本包含require().include(); 全局配置文件php.ini(auto_prepend_file/auto_append_file); 目录配置文件.ht ...

  2. 使用AFNetworking请求新浪微博数据接口出错解决办法

    在使用AFNetworking请求新浪微博数据接口时会出这样的错误,如 这样的错误说明,AFNetworking无法处理这样的数据格式.所以,我们需要修改AFNetworking中的一些接收数据格式. ...

  3. Let the Balloon Rise(map)

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  4. 转换成CSV文件、Word、Excel、PDF等的方法--读取CSV文件的方法

    1. 转换成CSV文件: http://www.dotnetgallery.com/lab/resource93-Export-to-CSV-file-from-Data-Table-in-Aspne ...

  5. oracle添加用户及权限

    CREATE USER qdcenter 用户名 IDENTIFIED BY qdcenter   密码 DEFAULT TABLESPACE data1  默认表空间 TEMPORARY TABLE ...

  6. javascript模块化开发编程

    随着网站的不断迭代更新,js代码越来越多,那么问题来了 代码比较乱 命名出现冲突 文件依赖比较繁杂 为了解决以上问题,模块化开发出现了 1.一个简单的demo,维护和扩展模块 模块的维护和扩展一定要遵 ...

  7. matlab GUI之常用对话框(三)-- dialog \ errordlg \ warndlg \ helpdlg \ msgbox \questdlg

    常用的对话框(三) 1.普通对话框  dialog 调用格式: h=dialog('PropertyName','PropertyValue'......) %普通对话框 h=dialog( ]); ...

  8. 错误:类Byte是公共的,应在名为Byte.java 的文件中声明public class Byte{}一个错误

      解决:文件名是xyz,那你的这个类名也应该是xyz.

  9. 对“xxx”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们。

    在程序中调用C++链接库中的回调函由于没有考虑生命周期,直接写委托回随机的被gc给回收掉导致报这个错误 错误的程序: private void InitPlateIdentify() { try { ...

  10. 经常会用到的js函数

    //获取样式function getStyle(obj,attr){ if(obj.currentStyle){  return obj.currentStyle[attr];  }else{  re ...