POJ 3264
To find the difference between the maximum and the minimum in a certain interval
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <algorithm>5#include <cmath>6 #defineMAXN 500057 using namespacestd;8 intN,M,A[MAXN];9 intma[maxn][ -],mi[maxn][ -];Ten voidPre () One { A for(intI=1; i<=n;i++) mi[i][0]=a[i],ma[i][0]=A[i]; - for(intj=1;(1<<J) <=n;j++) - for(intI=1; i+ (1<<J)-1<=n;i++) theMi[i][j]=min (mi[i][j-1],mi[i+ (1<< (J-1))][j-1]), -Ma[i][j]=max (ma[i][j-1],ma[i+ (1<< (J-1))][j-1]); - } - intQueryintLintR) + { - intK=trunc (log2 (r-l+1)); + return(Max (ma[l][k],ma[r-(1<<K) +1][k])-min ((Mi[l][k]), mi[r-(1<<K) +1][k])); A } at intMain () - { - intL,r; -scanf"%d%d",&n,&m); - for(intI=1; i<=n;i++) scanf ("%d",&a[i]); - pre (); in for(intI=1; i<=m;i++) - { toscanf"%d%d",&l,&R); +printf"%d\n", Query (l,r)); - } the return 0; *}
View Code
Above by DSB Lq_double
RMQ Nude Questions