Poj3264_balanced lineup (line segment tree/Single Point update)

Source: Internet
Author: User

Solution report

Question:

Evaluate the difference between the maximum and minimum values in the interval.

Ideas:

Bare line segment tree, the first line segment tree. The maximum interval value.

#include <iostream>#include <cstring>#include <cstdio>#define  inf 99999999#define LL long longusing namespace std;LL minn[201000],maxx[201000];void update(LL root,LL l,LL r,LL p,LL v){    if(l==r)minn[root]=v,maxx[root]=v;    if(l<r)    {        LL mid=(l+r)/2;        if(p<=mid)update(root*2,l,mid,p,v);        else update(root*2+1,mid+1,r,p,v);        minn[root]=min(minn[root*2],minn[root*2+1]);        maxx[root]=max(maxx[root*2],maxx[root*2+1]);    }}LL q_minn(LL root,LL l,LL r,LL ql,LL qr){    LL mid=(l+r)/2,ans=inf;    if(ql<=l&&r<=qr)return minn[root];    if(ql<=mid)ans=min(ans,q_minn(root*2,l,mid,ql,qr));    if(qr>mid)ans=min(ans,q_minn(root*2+1,mid+1,r,ql,qr));    return ans;}LL q_maxx(int root ,int l,int r,int ql,int qr){    LL mid=(l+r)/2,ans=-inf;    if(ql<=l&&r<=qr)return maxx[root];    if(ql<=mid)ans=max(ans,q_maxx(root*2,l,mid,ql,qr));    if(mid<qr)ans=max(ans,q_maxx(root*2+1,mid+1,r,ql,qr));    return ans;}int main(){    LL a,n,i,j,q,ql,qr;    scanf("%lld%lld",&n,&q);    for(i=1; i<=n; i++)    {        scanf("%lld",&a);        update(1,1,n,i,a);    }    while(q--)    {        scanf("%lld%lld",&ql,&qr);        printf("%lld\n",q_maxx(1,1,n,ql,qr)-q_minn(1,1,n,ql,qr));    }    return 0;}

Balanced lineup
Time limit:5000 Ms   Memory limit:65536 K
Total submissions:34181   Accepted:16065
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

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.