poj 3264 RMP

题目链接:http://poj.org/problem?id=3264

#include<cstdio>
#include<iostream>
#include<cmath>
#define M 21474836
using namespace std;
int n,x,y,maxn[50005][20],minn[50005][20],s[50005][20];
int max1,min1,q;
int max(int a,int b){
    return a>b?a:b;
}
int min(int a,int b){
    return a>b?b:a;
}
int main(){
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
    for(int j=1;j<=(int)((log((float) n))/log(2.0)+1);j++) minn[i][j]=M;
    for(int i=1;i<=n;i++){
        scanf("%d",&maxn[i][0]);
        minn[i][0]=maxn[i][0];
    }
    for(int j=1;j<20;j++)//RMP; 
    for(int i=1;i<=n;i++)
        if(i+(1<<j)-1<=n){
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]);//maxn[i][j]存储从i开始长度为2^j区间的max; 
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);//minn[i][j]存储从i开始长度为2^j区间的min;
        }
    for(int i=1;i<=q;i++){
        scanf("%d%d",&x,&y);
        int k=(int) ((log((float)(y-x+1)))/log(2.0));
        max1=max(maxn[x][k],maxn[y-(1<<k)+1][k]);
        min1=min(minn[x][k],minn[y-(1<<k)+1][k]);
        printf("%d\n",max1-min1);
    }
    return 0;
}
View Code

 

posted @ 2016-11-15 20:33  qg1  阅读(90)  评论(0编辑  收藏  举报