POJ 3264 Balanced Lineup (segment tree single point update interval query)

Source: Internet
Author: User


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

title Link: http://poj.org/problem?id=3264

The main idea: to give a set of ordered series, the value of the maximum value minus the minimum value in the interval

Title Analysis: Bare-wire Segment tree single-point update, the time of delivery C + + speed than g++ one times faster

#include <cstdio> #include <cstring> #include <algorithm> #define Lson L, Mid, Rt << 1#define Rson m ID + 1, r, RT << 1 |    1using namespace Std;int Const INF = 0x3fffffff;int Const MAX = 50005;int A[max], MA, mi;struct node{int L, R;    int mi, MA;    int mid () {return (L + r)/2;    }}t[3 * max];void push_up (int rt) {t[rt].ma = MAX (t[rt << 1].ma, t[rt << 1 | 1].ma); T[RT].MI = min (t[rt << 1].mi, t[rt << 1 | 1].mi);}    void Build (int l, int r, int rt) {T[RT].L = l;    T[RT].R = R;        if (L = = r) {t[rt].ma = T[RT].MI = A[l];    Return        } else {int mid = T[rt].mid ();        Build (Lson);        Build (Rson);    PUSH_UP (RT);        }}void Query (int l, int r, int rt) {if (T[RT].L = = L && T[RT].R = = r) {ma = max (t[rt].ma, MA);        mi = min (T[RT].MI, MI);    Return        } else {int mid = T[rt].mid (); if (R <= mid) Query (L, R, RT << 1);        else if (L > Mid) Query (L, R, RT << 1 | 1);            else {Query (Lson);        Query (Rson);    }}}int Main () {int n, m;    scanf ("%d%d", &n, &m);    for (int i = 1; I <= n; i++) scanf ("%d", &a[i]);    Build (1, N, 1);        for (int i = 1; I <= m; i++) {int L, R;        scanf ("%d%d", &l, &r);        Ma =-inf;        Mi = INF;        Query (L, R, 1);    printf ("%d\n", Ma-mi); }}




POJ 3264 Balanced Lineup (segment tree single point update interval query)

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.