POJ 3264 Balanced Lineup

Source: Internet
Author: User
Tags vmin

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 which 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 Tal Lest and shortest cow in the range.

Sample Input

6 31734251 54) 62 2

Sample Output

630
Simple line-segment tree, practice practiced hand, just started with CIN timeout, changed to scanf over. #include <iostream>
#include <cstdio>
usingnamespaceStd

structW
{
intL
intR
intVmax
intVmin
}k[800010];
intA=999999999, b=-999999999;
voidSet(intLintRintIndex
{
K[index]. L=l;
K[index]. R=r;
k[index].vmax=-999999999;
k[index].vmin=999999999;
if(L!=R)
{
Set(l, (l+r)/2, index*2);
Set((L+R)/2+1, r,index*2+1);
}
}

voidInsertintIndexintIintV
{
if(K[index]. L==k[index]. R
{
K[index].vmax=k[index].vmin=v;
return;
}
if(K[INDEX].VMAX&LT;V)
K[index].vmax=v;
if(K[INDEX].VMIN&GT;V)
K[index].vmin=v;
if(I<= (K[index). R+k[index]. L)/2)
Insert (index*2, i,v);
Else
Insert (index*2+1, i,v);
}

voidQuary (intIndexintSintE
{
//if (k[index].vmin>=a&&k[index].vmax<=b)
//return;
if(K[index]. L==s&&k[index]. R==E)
{
if(K[index].vmin<a)
A=k[index].vmin;
if(K[INDEX].VMAX&GT;B)
B=k[index].vmax;
return;
}
if(K[index]. L==k[index]. R
return;
if(E<= (K[index). L+k[index]. R)/2)
Quary (index*2, s,e);
Elseif(S> (K[index). L+k[index]. R)/2)
Quary (index*2+1, s,e);
Else
{
Quary (index*2, S, (K[index]. L+k[index]. R)/2);
Quary (index*2+1, (K[index]. L+k[index]. R)/2+1, e);
}
}
intMain ()
{
intN,m,num,l,r;
scanf"%d%d", &n,&m);
Set(1N1);
for(intI=1; i<=n;i++)
{
//cin>>num;
scanf"%d", &num);
Insert1, I,num);
}
for(intI=1; i<=m;i++)
{
//cin>>l>>r;
scanf"%d%d", &l,&r);
A=999999999, b=-999999999;
Quary (1, l,r);
//cout <<b-a<<endl;
printf"%d\n", b-a);
}
return0;
}

POJ 3264 Balanced Lineup

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.