Topic Portal: http://poj.org/problem?id=3264
The problem is simply to find the maximum value to find the minimum value.
#include <cstdio>#include<cstring>#include<iostream>using namespacestd;Const intmaxn=50010;intn,q,maxm[maxn][ -],minm[maxn][ -];voidinit () {inti,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]); }}intQueryintLintR) { inti,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]); returnI-J;}intMain () {inti,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;}
[POJ3368] Balanced Lineup