[POJ3368]Balanced Lineup
题目传送门:http://poj.org/problem?id=3264
这道题就是简单的求最大值求最小值。
#include<cstdio> #include<cstring> #include<iostream> using namespace std; const int maxn=50010; int n,q,maxm[maxn][20],minm[maxn][20]; void init() { int i,j; for (j=1;(1<<j)<=n;j++) for (i=1;i+(1<<j)-1<=n;i++) { maxm[i][j]=max(maxm[i][j-1],maxm[i+(1<<(j-1))][j-1]); minm[i][j]=min(minm[i][j-1],minm[i+(1<<(j-1))][j-1]); } } int query(int l,int r) { int i,j,k=0; while (1<<(k+1)<=r-l+1)k++; i=max(maxm[l][k],maxm[r-(1<<k)+1][k]); j=min(minm[l][k],minm[r-(1<<k)+1][k]); return i-j; } int main() { int i,j,l; scanf("%d%d",&n,&q); memset(minm,0x6f,sizeof(minm)); for (i=1;i<=n;i++) { scanf("%d",&maxm[i][0]); minm[i][0]=maxm[i][0]; } init(); for (i=1;i<=q;i++) { scanf("%d%d",&j,&l); printf("%d\n",query(j,l)); } return 0; }