Poj 3264 balanced lineup

Source: Internet
Author: User

Balanced lineup
Time limit:5000 Ms   Memory limit:65536 K
Total submissions:33094   Accepted:15552
Case time limit:2000 ms

Description

For the daily milking, Farmer John'sNCows (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 will take a contiguous range of cows from the milking lineup to play the game. however, for all the cows to have fun they shocould not differ too much in height.

Farmer John has made a listQ(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 group.

Input

Line 1: two space-separated integers, NAnd Q.
Lines 2 .. N+ 1: Line I+ 1 contains a single integer that 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 BIntrusive.

Output

Lines 1 .. Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

6 31734251 54 62 2

Sample output

630

We plan to use two methods. By the way, the differences between rmq and line tree are different. They all said that rmq is better than a line segment tree, and I found that the time is not much different, even though it is not optimized.


The AC code is as follows:


Line Segment tree!

// 2250 Ms 2404 K # include <iostream> # include <cstring> # include <cstdio> # include <algorithm> # define M 50010 # define INF 100000000 using namespace std; struct H {int L, R, Maxx, Minn;} trees [4 * m]; int n, m; int num [m]; void build_trees (int jd, int l, int R) {trees [JD]. L = L; trees [JD]. R = r; If (L = r) {trees [JD]. maxx = num [l]; trees [JD]. minn = num [l]; return;} int mid = (L + r)/2; build_trees (JD * 2, L, mid); build_trees (JD * 2 + 1, Mid + 1, R); trees [JD]. maxx = max (trees [JD * 2]. maxx, trees [JD * 2 + 1]. maxx); trees [JD]. minn = min (trees [JD * 2]. minn, trees [JD * 2 + 1]. minn);} int query_max (int jd, int L, int R) {int ans = 0; If (L <= trees [JD]. L & R> = trees [JD]. r) return trees [JD]. maxx; int mid = (trees [JD]. L + trees [JD]. r)/2; If (L <= mid) ans = max (ANS, query_max (JD * 2, L, R); If (r> mid) ans = max (ANS, query_max (JD * 2 + 1, L, R); Return ans;} int query_min (int jd, int l, Int R) {int ans = inf; If (L <= trees [JD]. L & R> = trees [JD]. r) return trees [JD]. minn; int mid = (trees [JD]. L + trees [JD]. r)/2; If (L <= mid) ans = min (ANS, query_min (JD * 2, L, R); If (r> mid) ans = min (ANS, query_min (JD * 2 + 1, L, R); Return ans;} int main () {int I, j; int A, B; while (~ Scanf ("% d", & N, & M) {memset (Num, 0, sizeof num); for (I = 1; I <= N; I ++) scanf ("% d", & num [I]); build_trees (1, 1, n); for (I = 1; I <= m; I ++) {scanf ("% d", & A, & B); printf ("% d \ n", query_max (1, a, B)-query_min (1, a, B) ;}} return 0 ;}


Rmq !!!


///RMQ  1813MS  12100K#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#define  M 50010#define inf 100000000using namespace std;int n,m;int num[M];int dp1[M][30],dp2[M][30];void RMQ_min(){    int i,j;    memset(dp1,0,sizeof dp1);    for(i=1;i<=n;i++)        dp1[i][0]=num[i];    for(j=1;1<<j<=n;j++)        for(i=1;i+(1<<j)-1<=n;i++)            dp1[i][j]=min(dp1[i][j-1],dp1[i+(1<<(j-1))][j-1]);}void RMQ_max(){    int i,j;    memset(dp2,0,sizeof dp2);    for(i=1;i<=n;i++)        dp2[i][0]=num[i];    for(j=1;1<<j<=n;j++)        for(i=1;i+(1<<j)-1<=n;i++)            dp2[i][j]=max(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]);}int rmq_min(int l,int r){    int i,j;    int k=0;    while(1<<(k+1)<=r-l+1)        k++;    return min(dp1[l][k],dp1[r-(1<<k)+1][k]);}int rmq_max(int l,int r){    int i,j;    int k=0;    while(1<<(k+1)<=r-l+1)        k++;    return max(dp2[l][k],dp2[r-(1<<k)+1][k]);}int main(){    int i,j;    int a,b;    while(~scanf("%d%d",&n,&m))    {        for(i=1;i<=n;i++)            scanf("%d",&num[i]);            RMQ_min();            RMQ_max();            for(i=1;i<=m;i++)            {                scanf("%d%d",&a,&b);                printf("%d\n",rmq_max(a,b)-rmq_min(a,b));            }    }    return 0;}




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.