POJ 3264 Balanced Lineup

#include<iostream>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<string.h>
using namespace std;
int num[50024],dp_min[20][50024],dp_max[20][50024];
inline int min( int a,int b )
{
return a>b?b:a;
}
inline int max( int a,int b )
{
return a>b?a:b;
}
void get_RMQ( int n )
{
for( int i=1;i<=n;i++ )
{
dp_min[0][i]=dp_max[0][i]=num[i];
}
int t=(int)( log(double( n ))/log( 2.0 ) );
for( int j=1;j<=t;j++ )
{
for( int i=1;i+( 1<<j )-1<=n;i++ )
{
dp_min[j][i]=min( dp_min[j-1][i],dp_min[j-1][i+(1<<(j-1))] );
dp_max[j][i]=max( dp_max[j-1][i],dp_max[j-1][i+(1<<(j-1))] );
}
}
}
inline int result_RMQ( int left,int right )
{
int k=(int)( log( double(right-left+1) )/log( 2.0 ) );
return max( dp_max[k][left],dp_max[k][right-( 1<<k )+1] )-min( dp_min[k][left],dp_min[k][right-( 1<<k )+1] );
}
int main()
{
int N,Q,left,right;
while( scanf( "%d%d",&N,&Q )==2 )
{
for( int i=1;i<=N;i++ )
scanf("%d",&num[i]);
get_RMQ( N );
for( int i=1;i<=Q;i++ )
{
scanf("%d%d",&left,&right);
printf("%d\n",result_RMQ( left,right ));
}
}
return 0;
}


该题是一道赤裸裸的RMQ的题,题意就是要你求在给定你的区间内求最大与最小的差值。

 

你可能感兴趣的:(poj)