POJ 3246 Balanced Lineup (segment tree)

Source: Internet
Author: User

Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 38942 Accepted: 18247
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≤He ight ≤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≤ ABN), 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

The main idea: given some number in a certain interval, it is required to find the difference between the maximum value and the minimum value in a certain interval.
The title of the segment tree. For this problem, since it is the difference between the maximum and minimum values, it is necessary to store the maximum and minimum values in the interval. At the same time, this problem is only a simple request for the difference within the interval, do not need to update.
#include <stdio.h> #include <string.h> #define MAX (A, b) A>b?a:b#define min (A, b) a<b?a:b#define INF 99999999#define N 50005struct tree{int l,r,maxi,mini;int Mid () {return l+r>>1;}} Tree[n<<2];int ma=-inf,mi=inf;void Build (int l,int r,int root) {tree[root].l=l;tree[root].r=r;tree[root].maxi=  -inf;tree[root].mini=inf; Initializes the maximum minimum if (l==r) {return;} int Mid=l+r>>1;build (l,mid,root<<1); build (mid+1,r,root<<1|1);} void update (int i,int z,int root) {if (TREE[ROOT].L==TREE[ROOT].R) {Tree[root].mini=tree[root].maxi=z;return;}      Tree[root].maxi=max (tree[root].maxi,z); Tree[root].mini=min (tree[root].mini,z);   Update Max and Min if (I<=tree[root].mid ()) update (i,z,root<<1) each time; The nodes below I are all updated here.     I have a relationship with mid. else update (i,z,root<<1|1);}   void Query (int l,int r,int root) {if (tree[root].mini>=mi&&tree[root].maxi<=ma) return;   if (L==TREE[ROOT].L&AMP;&AMP;R==TREE[ROOT].R) {mi=min (Mi,tree[root].mini);   Ma=max (Ma,tree[root].maxi);   Return  } int mid=tree[root].l+tree[root].r>>1;   if (r<=mid) {Query (l,r,root<<1);   } else if (L>mid) {Query (l,r,root<<1|1);   } else {Query (l,mid,root<<1);   Query (mid+1,r,root<<1|1); } return; int main () {int N,q,cow[200005],a,b;int i,j,k;while (scanf ("%d%d", &n,&q)!=eof) {build (1,n,1); for (i=1;i<=n   ; i++) {scanf ("%d", &cow[i]);     Update (i,cow[i],1); For the first digit I insert}while (q--) {scanf ("%d%d", &a,&b); ma=-inf;mi=inf; Query (a,b,1);p rintf ("%d\n", Ma-mi);}} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3246 Balanced Lineup (segment tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.