POJ_3264_Balanced Lineup

很裸的RMQ~

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
#define MAXN 50005
int maxsum[MAXN][20],minsum[MAXN][20],n,q;
void RMQ()
{
    for(int j=1;(1<<j)<=n;++j)
        for(int i=1;i+(1<<j)-1<=n;++i)
        {
            maxsum[i][j]=max(maxsum[i][j-1],maxsum[i+(1<<(j-1))][j-1]);
            minsum[i][j]=min(minsum[i][j-1],minsum[i+(1<<(j-1))][j-1]);
        }
}
int main()
{
    int i,j,k,L,R;
   while(~scanf("%d%d",&n,&q))
   {
       for(i=1;i<=n;++i)
       {
           scanf("%d",&maxsum[i][0]);
           minsum[i][0]=maxsum[i][0];
       }
       RMQ();
       while(q--)
       {
           scanf("%d%d",&L,&R);
           int k=(int)((log(R-L+1.0))/log(2.0));
           int maxval=max(maxsum[L][k],maxsum[R-(1<<k)+1][k]);
           int minval=min(minsum[L][k],minsum[R-(1<<k)+1][k]);
           printf("%d\n",maxval-minval);
       }
   }
   return 0;
}

 

posted @ 2013-05-29 20:28  小仪在努力~  阅读(133)  评论(0编辑  收藏  举报