g-balanced Lineup POJ 3264 (segment tree + interval query no update)

Source: Internet
Author: User


g-balanced Lineup
Time limit:5000ms Memory limit:65536kb 64bit IO format:%i64d &%i64u
Submit Status Practice POJ 3264

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≤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 G Roup.

Input
Line 1:two space-separated integers, N and Q.
Lines 2.. N+1:line i+1 contains a single integer so is the height of cow I
Lines n+2. n+ q+1:two integers A and B (1≤a≤b≤n), representing the range of cows from A to B inclusive.

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 TA Llest and shortest cow in the range.

Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3

0


#include <bits/stdc++.h> #include <iostream> #include <cstring> #include <string> #include <cstdlib> #include <cstdio> #include <map> #include <algorithm>using namespace Std;const int maxn=50011;const int inf=999999999; #define Lson (rt<<1), L,m#define Rson (rt<<1|1), M+1,r#define M ((L+R)    &GT;&GT;1) #define for (i,n) for (int i=0;i< (n); i++) Template<class t>inline T Read (t&x) {char C;    while ((C=getchar ()) <=32);    BOOL Ok=false;    if (c== '-') Ok=true,c=getchar ();    for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 ';    if (OK) x=-x; return x;}    Template<class t> inline void Read_ (t&x,t&y) {read (x); Read (y);}    Template<class t> inline void read__ (t&x,t&y,t&z) {read (x);    Read (y); Read (z);}    Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x;    if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');}   Template<class t>inline void Writeln (T x) { Write (x); Putchar (' \ n ');} -------IO template------typedef long LONG ll;struct node{int maxv,minv;}    p[maxn<<3];void Build (int rt,int l,int R) {p[rt].maxv=-inf;    P[rt].minv=inf;        if (l==r) {int A;        Read (a);        P[rt].maxv=p[rt].minv=a;    return;    } build (Lson);    Build (Rson);    P[rt].maxv=max (P[RT&LT;&LT;1].MAXV,P[RT&LT;&LT;1|1].MAXV); P[rt].minv=min (P[RT&LT;&LT;1].MINV,P[RT&LT;&LT;1|1].MINV);}        int a,b;void query (int rt,int l,int r,int x,int y) {if (l==x&&y==r) {A=max (A,P[RT].MAXV);        B=min (B,P[RT].MINV);    return;    } if (y<=m) query (lson,x,y);    else if (x>m) query (rson,x,y);        else {query (lson,x,m);    Query (rson,m+1,y);     }}int Main () {int n,m;    Freopen ("In.txt", "R", stdin);        while (~SCANF ("%d%d", &n,&m)) {build (1,1,n);            while (m--) {int x, y;            Read_ (x, y);            A=-inf;b=inf; Query (1,1,n,x,y);        Writeln (A-B); }} return 0;}



g-balanced Lineup POJ 3264 (segment tree + interval query no update)

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.