Topic PortalBalanced Lineup
Time Limit: 5000MS |
|
Memory Limit: 65536K |
Total Submissions: 64655 |
|
Accepted: 30135 |
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≤ 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.
Input
Line 1:two space-separated integers,
Nand
Q.
Lines 2..
N+1:line
I+1 contains a single integer which is the height of cow
I
Lines
N+2..
N+
Q+1:two integers
Aand
B(1≤
A≤
B≤
N), representing the range of cows from
ATo
BInclusive.
Output
Lines 1..
Q: Each line contains a single integer so is a response to a reply and indicates the difference in height between the Tal Lest and shortest cow in the range.
Sample Input
6 31734251 54) 62 2
Sample Output
630
Source
Usaco January Silver
Test Instructions: is the difference between the maximum and minimum values of the interval [l,r] first of all, introduce the tree-like array, the complexity of the tree array is O (Logn), so fast, it is using the idea of block optimization, such as: 1-5 is divided into 1, 1-2, 3, 1-2-3-4, 5, and the most basic implementation of the tree array is to seek prefixes and interval single-point update; In this case, we can follow this, of course. The single-point update Extremum is the same as before, the focus is to find the extremum of the interval, we can not be like the prefix and the same sum[r]-sum[l], what should be done? in fact, we still use the idea of block optimization, but we can not use max[r], because this indicates that the interval may be beyond the range we require, but we could use some of the block contained in [L,R], thus reducing the complexity. Assuming we're violent, we're going from right to left, iterating through an array of a and then taking the maximum value, then in this case, if there are some blocks in [L,r], you can use them directly, and you don't have to traverse them, but when we encounter some blocks that go beyond [l,r], we can only compare a[r] directly.
Code:
#include <iostream>#include<string.h>using namespaceStd;typedefLong Longll;typedef unsignedLong Longull;#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineMAX 50005intMax[max],min[max];intA[max];intN,q;inlinevoidReadint&x) { Charch; BOOLflag=false; for(Ch=getchar ();! IsDigit (CH); Ch=getchar ())if(ch=='-') flag=true; for(x=0; isdigit (ch); x=x*Ten+ch-'0', ch=GetChar ()); X=flag?-x:x;} InlinevoidWriteintx) { Static Const intmaxlen= -; Static CharS[maxlen]; if(x<0) {Putchar ('-'); x=-x;} if(!x) {Putchar ('0');return; } intlen=0; for(; x;x/=Ten) S[len++]=x%Ten+'0'; for(inti=len-1; i>=0;--i) Putchar (S[i]);}intLowbit (intx) { returnx&-x;}voidUpdata (intIintval) { while(i<=N) {Min[i]=min (min[i],val); Max[i]=Max (max[i],val); I+=lowbit (i); }}intQueryintLintR) { intmaxn=a[l],minn=A[r]; while(1) {MAXN=max (Maxn,a[r]), minn=min (minn,a[r]); if(L==R) Break; for(r-=1; R-l>=lowbit (R); r-=Lowbit (R)) MAXN=max (MAX[R],MAXN), minn=min (minn,min[r]); } returnmaxn-Minn;}intMain () {memset (Min,inf,sizeof(Min)); Read (n); read (q); for(intI=1; i<=n;i++) {read (a[i]); Updata (I,a[i]); } while(q--) { intb; Read (a), read (b); Write (query (a, b)); Putchar ('\ n'); } return 0;}
The time complexity is approximately log2 (n).
Reference Blog: https://www.cnblogs.com/mypride/p/5002556.html
52734329
poj3264 Balanced Lineup (tree-like array)