POJ 3264 Balanced Lineup (simple line segment tree or RMQ)

Source: Internet
Author: User
Tags cmath

Language:DefaultBalanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 36833 Accepted: 17252
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,NandQ.
Lines 2..N+1:lineI+1 contains a single integer which is the height of cowI
LinesN+2..N+Q+1:two integersAandB(1≤ABN), representing the range of cows fromAToBInclusive.

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

To find the maximum interval minimization


Line segment Tree Code:


#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <set> #include <map > #define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (x, y) ((x+y) >>1) #define EPS 1e-8typedef __ Int64 ll;using namespace std; #define N 50005int n,m,a[n];struct stud{int le,ri; int mi,ma;} f[n*4];void Build (int pos,int le,int ri) {f[pos].le=le;f[pos].ri=ri;if (Le==ri) {F[pos].mi=f[pos].ma=a[le];return;} int Mid=mid (LE,RI), Build (L (POS), Le,mid), Build (R (POS), Mid+1,ri), F[pos].ma=max (F[l (POS)].ma,f[r (POS)].ma); F[pos]. Mi=min (F[l (POS)].mi,f[r (POS)].MI);} int querymin (int pos,int le,int ri) {if (F[pos].le>=le&&f[pos].ri<=ri) return f[pos].mi; int Mid=mid (F[pos ].le,f[pos].ri); if (Mid>=ri) return Querymin (L (POS), Le,ri); if (Mid<le) return Querymin (R (POS), Le,ri); return min (Querymin (L (POS), Le,mid), Querymin (R (POS), Mid+1,ri));} int Querymax (int pos,int le,inT ri) {if (F[pos].le>=le&&f[pos].ri<=ri) return f[pos].ma; int Mid=mid (F[POS].LE,F[POS].RI); if (mid>= RI) return Querymax (L (POS), Le,ri); if (Mid<le) return Querymax (R (POS), Le,ri); Return Max (Querymax (L (POS), Le,mid), Querymax (R (POS), Mid+1,ri));}    int main () {#ifndef Online_judge freopen ("In.txt", "R", stdin); #endif//Online_judgeint I,j;while (~scanf ("%d%d", &n,&m)) {for (i=1;i<=n;i++) scanf ("%d", &a[i]); build        (1,1,n); int Le,ri;while (m--) {scanf ("%d%d", &le,&ri);p rintf ("%d\n", Querymax (1,le,ri)-querymin (1,le,ri));}} return 0;}


RMQ Code:


#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <set> #include <map > #define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (x, y) ((x+y) >>1) #define EPS 1e-8typedef __ Int64 ll;using namespace std; #define N 50005int dpmin[n][20];int dpmax[n][20];int n,m,a[n];void inint () {int i,j;for (i=1;  i<=n;i++) dpmin[i][0]=dpmax[i][0]=a[i];for (j=1; (1<<j) <=n+1;j++) for (i=1;i+ (1<<j) -1<=n;i++) {  Dpmin[i][j]=min (dpmin[i][j-1],dpmin[i+ (1<< (j-1))][j-1]); Dpmax[i][j]=max (dpmax[i][j-1],dpmax[i+ (1<< (j-1))][j-1]);}} inline int getmax (int le,int ri) {int k= (int) (log (ri-le+1+0.0)/log (2.0)); return Max (dpmax[le][k],dpmax[ri-(1<<k ) +1][k]);} inline int getmin (int le,int ri) {int k= (int) (log (ri-le+1+0.0)/log (2.0)), Return min (dpmin[le][k],dpmin[ri-(1<<k ) +1][k]);} int main () {//#ifndef online_judge//freopen ("In.txt", "R", sTdin);//#endif//Online_judge int i,j;while (~scanf ("%d%d", &n,&m)) {for (i=1;i<=n;i++) scanf ("%d", &a[i ]); Inint (); int Le,ri;while (m--) {scanf ("%d%d", &le,&ri);p rintf ("%d\n", Getmax (Le,ri)-getmin (Le,ri));}} return 0;}







POJ 3264 Balanced Lineup (simple line segment tree or RMQ)

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.