Description
For the daily milking, Farmer John's n cows (1≤ n ≤50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate frisbee with some of the cows. To keep things simple, he'll take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to has fun they should not differ too much in height.
Farmer John has made a list of Q (1≤ q ≤200,000) Potential groups of cows and their heights (1≤ H Eight ≤1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the G Roup.
The problem is to ask for RMQ, water problem.
The code is as follows:
//The ━━━━━━ of gods and Beasts ━━━━━━//┏┓┏┓//┏┛┻━━━━━━━┛┻┓//┃┃//┃━┃//████━████┃//┃┃//┃┻┃//┃┃//┗━┓┏━┛//┃┃//┃┃//┃┗━━━┓//┃┣┓//┃┏┛//┗┓┓┏━━━━━┳┓┏┛//┃┫┫┃┫┫//┗┻┛┗┻┛////━━━━━━ Feel the ━━━━━━ of Meng Meng//author:whywhy//Created time:2015 July 17 Friday 16:52 31 seconds//File name:3264.cpp#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<time.h>using namespacestd;Const intmaxn=50004;intdp1[maxn][ -],dp2[maxn][ -];intLOGN[MAXN];voidInitintNintnum[]) {logn[0]=-1; for(intI=1; i<=n;++i) {dp1[i][0]=Num[i]; dp2[i][0]=Num[i]; Logn[i]=logn[i-1]+ ((i& (i-1))==0); } for(intj=1; j<=logn[n];++j) for(intI=1; i+ (1<<J)-1<=n;++i) {dp1[i][j]=max (dp1[i][j-1],dp1[i+ (1<< (J-1))][j-1]); DP2[I][J]=min (dp2[i][j-1],dp2[i+ (1<< (J-1))][j-1]); }}intRMQ (intXinty) { intk=logn[y-x+1]; returnMax (dp1[x][k],dp1[y-(1<<K) +1][k])-min (dp2[x][k],dp2[y-(1<<K) +1][k]);}intNUM[MAXN];intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout); intn,q; intb; while(~SCANF ("%d%d",&n,&Q)) { for(intI=1; i<=n;++i) scanf ("%d",&Num[i]); Init (n,num); while(q--) {scanf ("%d%d",&a,&b); printf ("%d\n", RMQ (A, b)); } } return 0;}
View Code
Simple POJ 3264 Balanced lineup,rmq.