bzoj1636:balanced Lineup

Source: Internet
Author: User

1636: [Usaco2007 jan]balanced lineup time limit:5 Sec Memory limit:64 MB
submit:599 solved:423
[Submit] [Status] [Discuss] 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.

Every day, farmer John's N (1 <= n <= 50,000) Cows are always queued in the same sequence. One day, John.
Decided to let some cows play a Frisbee game. He was going to find a group of cows that were set up in the column to play.
But in order to avoid the level of disparity, the height of the cow should not be too large.
John prepared the Q (1 <= q <= 180,000) a possible selection of cattle and the height of all the cows (1 <=
Height <= 1,000,000). He wanted to know the height difference between the highest and lowest cows in each group.
Note: On the largest data, the input and output will occupy most of the elapsed time.

Input

* Line 1:two space-separated integers, N and Q. * Lines 2..n+1:line i+1 contains a single integer this 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 Inc Lusive.

OUTPUT6 31734251 2 Sample Input* Lines 1..q:each line contains a single integer so is a response
To a reply and indicates the difference in height between the
Tallest and shortest cow in the range.
Sample Output6
3
0
HINT Source

Silver

The bare segment tree. However, from the afternoon to the evening are writing a monotonous queue and then Leng AC not together. Then abandon the treatment ... Then suddenly wrote a question 1a ....

----------------------------------------------------------------------------------------------------------

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <deque>
using namespace Std;
int nmin[200000],nmax[200000];
int ql,qr,v,cur;
void Insert (int l,int r,int k) {
if (l==r) {
Nmin[k]=v;
Nmax[k]=v;
Return
}
int m= (L+R)/2;
if (cur<=m)
Insert (L,M,2*K);
Else
Insert (m+1,r,2*k+1);
Nmin[k]=min (nmin[2*k],nmin[2*k+1]);
Nmax[k]=max (nmax[2*k],nmax[2*k+1]);
}
int Findl (int l,int r,int k) {
if (ql<=l&&qr>=r)
return nmax[k];
int m= (L+R)/2;
int ans=-0x7fffffff;
if (qr>m)
Ans=max (Ans,findl (m+1,r,2*k+1));
if (ql<=m)
Ans=max (Ans,findl (l,m,2*k));
return ans;
}
int Findr (int l,int r,int k) {
if (ql<=l&&qr>=r)
return nmin[k];
int m= (L+R)/2;
int ans=0x7fffffff;
if (qr>m)
Ans=min (Ans,findr (m+1,r,2*k+1));
if (ql<=m)
Ans=min (Ans,findr (l,m,2*k));
return ans;
}
int main () {

int n,m;
scanf ("%d%d", &n,&m);
for (int i=1;i<=n;i++) {
int tmp;
scanf ("%d", &tmp);
v=tmp;
Cur=i;
Insert (1,n,1);
}
for (int i=1;i<=m;i++) {
scanf ("%d%d", &QL,&QR);
printf ("%d\n", Findl (1,n,1)-findr (1,n,1));
}
return 0;
}

---------------------------------------------------------------------------------------------------

bzoj1636: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.