POJ 3264 Balanced Lineup (rmq/segment tree)

Source: Internet
Author: User

Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 40312 Accepted: 18936
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≤ 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


Test instructions: Give a non-ordered sequence of N, then m query, input two number l,r, output closed interval [l,r] maximum value and minimum worth difference.

Ideas

1. A segment tree can be used to do, a variable in the segment tree records the maximum value of an interval, the other variable records the minimum value of an interval, and then input l,r, respectively, the query, the result of the final subtraction is the required value.

2. You can also use RMQ to do, after the input sequence, preprocessing, and then each input l,r directly after the maximum and minimum value subtraction.



Click to open link


Line segment Tree procedure:

#include <iostream> #include <algorithm> #include <stdio.h> #include <stdlib.h> #include <    string.h>using namespace std;struct node{int l;    int R;    int xx; int yy;}    Q[200010];int n,m;void Build (int l,int R,int rt) {q[rt].l = l;    Q[RT].R = R;        if (L = = r) {scanf ("%d", &q[rt].yy);        q[rt].xx = q[rt].yy;    return;    } int mid = (l+r) >>1;    Build (l,mid,rt<<1);    Build (mid+1,r,rt<<1|1);    q[rt].yy = min (q[rt<<1].yy,q[rt<<1|1].yy); q[rt].xx = max (q[rt<<1].xx,q[rt<<1|1].xx);}    int qurry1 (int ll,int rr,int l,int r,int RT) {if (ll<=l && rr>=r) {return q[rt].xx;    } if (Ll>r | | rr<l) {return 0;    } int mid = (l+r) >>1; Return Max (Qurry1 (ll,rr,l,mid,rt<<1), Qurry1 (ll,rr,mid+1,r,rt<<1|1));}    int qurry2 (int ll,int rr,int l,int r,int RT) {if (ll<=l && rr>=r) {return q[rt].yy;      } if (Ll>r | | rr<l) {  return 99999999;    } int mid = (l+r) >>1; return min (Qurry2 (ll,rr,l,mid,rt<<1), Qurry2 (ll,rr,mid+1,r,rt<<1|1));}        int main () {while (scanf ("%d%d", &n,&m)!=eof) {build (1,n,1);        int x, y;            for (int i=0;i<m;i++) {scanf ("%d%d", &x,&y);            int maxx = Qurry1 (x,y,1,n,1);            int minn = Qurry2 (x,y,1,n,1);        printf ("%d\n", Maxx-minn); }} return 0;}




RMQ Procedure:


#include <iostream> #include <algorithm> #include <stdio.h> #include <stdlib.h> #include < String.h> #include <math.h>using namespace std;const int N = 200010;int maxx[n][20];int minn[n][20];int n,m;void    Play () {int L = floor (log10 (double (n))/log10 (double (2))); for (int j=1;j<=l;j++) {for (int i=1;i<=n+1-(1&LT;&LT;J); i++) {Maxx[i][j] = max (maxx[i][j-1],maxx[i+ (1<< (j-1))]            [J-1]);        Minn[i][j] = min (minn[i][j-1],minn[i+ (1<< (j-1))][j-1]);        }}}int Main () {while (scanf ("%d%d", &n,&m)!=eof) {int x, y;            for (int i=1;i<=n;i++) {scanf ("%d", &x);            Maxx[i][0] = x;        Minn[i][0] = x;        } play ();            while (m--) {scanf ("%d%d", &x,&y);            int pp = floor (log10 (double (y-x+1))/log10 (double (2)));        printf ("%d\n", Max (maxx[x][pp],maxx[y-(1&LT;&LT;PP) +1][pp])-min (minn[x][pp],minn[y-(1&LT;&LT;PP) +1][pp]));   }    } return 0;} 



Copyright NOTICE: This article is the original blogger article, if you have special needs, please contact Bo Master qq:793977586.

POJ 3264 Balanced Lineup (rmq/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.