Balanced Lineup
Time limit:5000ms Memory limit:65536k
Total submissions:36813 accepted:17237
Case Time Limit:2000ms
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≤height≤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.
Input
Line 1:two space-separated integers, N and Q.
Lines 2..n+1:line i+1 contains a single integer so is the height of cow I
Lines n+2..n+q+1:two integers A and B (1≤a≤b≤n), representing the range of cows from A to B inclusive.
Output
Lines 1..q:each line contains a single integer that's a response to a reply and indicates the difference in height Betwe En the tallest and shortest cow in the range.
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
Action: Implements the difference between the maximum and minimum values in the query [a, b].
St is supposed to be faster than the segment tree, but St ran down with more than 3,000 ms ...
Two query operations one implementation to find the maximum value, one implementation to find the minimum value, the last two minus the line.
#include <cstdio>#include <algorithm>using namespace STD;Const intMAXN =200010;intNintS[MAXN];intdpmin[maxn][ -];intdpmax[maxn][ -];intMinquery (intAintb) {intK =0; while(1<< (k +1) <= b-a+1) k++;returnMin (dpmin[a][k],dpmin[b-(1<<K) +1][k]);}intMaxquery (intAintb) {intK =0; while(1<< (k +1) <= b-a+1) k++;returnMax (dpmax[a][k],dpmax[b-(1<<K) +1][k]);}intMain () {intQscanf("%d%d", &n,&q); for(inti =1; I <= N; i + +)scanf("%d", &s[i]); for(inti =1; I <= N; i + +) dpmin[i][0] = S[i]; for(intj =1; (1<<J) <= N; J + +) { for(inti =1; i+ (2<< (J-1))-1<= N; i + +) {Dpmin[i][j] = min (dpmin[i][j-1],dpmin[i+ (1<< (J-1))][j-1]); } } for(inti =1; I <= N; i + +) dpmax[i][0] = S[i]; for(intj =1; (1<<J) <= N; J + +) { for(inti =1; i+ (2<< (J-1))-1<= N; i + +) {Dpmax[i][j] = max (dpmax[i][j-1],dpmax[i+ (1<< (J-1))][j-1]); } } while(q--) {intb;scanf("%d%d", &a,&b);printf("%d\n", Maxquery (A, B)-minquery (A, b)); }return 0;}
Line Segment Tree Code
#include <cstdio>#include <algorithm>Using namespace Std;constintMAXN =200010;intN,masegtree[maxn *+Ten],misegtree[maxn *+Ten];int s[Maxn];void BUILDMA (intNodeintLintR) {if(L = = r) Masegtree[node] =s[L];Else{BUILDMA (node * *, L, (l+r)/2); BUILDMA (node * *+1, (L+R)/2+1, R); Masegtree[node] = max (masegtree[node * *],masegtree[node * *+1]); }}void Buildmi (intNodeintLintR) {if(L = = r) Misegtree[node] =s[L];Else{BUILDMI (node * *, L, (l+r)/2); BUILDMI (node * *+1, (L+R)/2+1, R); Misegtree[node] = min (misegtree[node * *],misegtree[node * *+1]); }}intMaquery (intAintBintKintLintR) {if(L > B | | r < a)return 1<< to;if(A <= l && R <= B)returnMASEGTREE[K];returnMax (Maquery (a,b,k * *, L, (l+r)/2), Maquery (a,b,k * *+1, (L+R)/2+1, r));}intMiquery (intAintBintKintLintR) {if(L > B | | r < a)return 1<< to-1;if(A <= l && R <= B)returnMISEGTREE[K];returnMin (Miquery (a,b,k * *, L, (l+r)/2), Miquery (a,b,k * *+1, (L+R)/2+1, r));}intMain () {int Q; scanf"%d%d",&n,&Q); for(inti =1; I <= N; i + +) scanf ("%d",&s[i]); for(inti =1; I <4*MAXN+Ten; i + +) {Masegtree[i] =1<< to; Misegtree[i] =1<< to-1; } BUILDMA (1,1, n); BUILDMI (1,1, n); while(Q--) {intb; scanf"%d%d", &a,&b);printf("%d\ n", Maquery (A, B,1,1, N)-miquery (A, B,1,1, n)); }return 0;}
POJ3264 Balanced Lineup segment Tree RMQ ST algorithm Application