Poj_3264 Balanced Lineup

Source: Internet
Author: User

Balanced Lineup
Time Limit: 5000 MS
Memory Limit: 65536 K
Total Submissions: 23380
Accepted: 10882
Case Time Limit: 2000 MS
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 agame of Ultimate Frisbee with some of the cows. to keep things simple, he willtake 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 inheight.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potentialgroups of cows and their heights (1 ≤ height ≤ 1,000,000 ). for eachgroup, he wants your help to determine the difference in height between theshortest and the tallest cow in the group.
Input
Line 1: Two space-separated integers, N and Q.
Lines 2. N + 1: Line I + 1 contains a single integer that is theheight 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 Binclusive.
Output
Lines 1. Q: Each line contains a single integerthat is a response to a reply and indicates the difference in height betweenthe tallest 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
Source
USACO 2007 JanuarySilver
Solution:
This question can be done using the line segment tree. There are only two operations: the first to create a tree and the second to query the line segment interval.
Code:
 
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Define N 50005
# Derefined Q 200005
Using namespace std;
Struct segment
{
Int left;
Int right;
Int max;
Int min;
Int value;
};
 
Int n, q;
Int high;
Segment seg [N * 4];
Int max (int a, int B)
{
If (a> B)
Return;
Return B;
}
 
Int min (int a, int B)
{
If (a <B)
Return;
Return B;
}
Void update (int rt)
{
Seg [rt]. max = max (seg [rt * 2]. max, seg [rt * 2 + 1]. max );
Seg [rt]. min = min (seg [rt * 2]. min, seg [rt * 2 + 1]. min );
 
}
// Build
Void buildTree (int l, int r, int rt)
{
Seg [rt]. left = l;
Seg [rt]. right = r;
If (l = r)
{
Scanf ("% d", & high );
Seg [rt]. max = seg [rt]. min = high;
Return;
}
Int mid = (l + r)/2;
BuildTree (l, mid, rt * 2 );
BuildTree (mid + 1, r, rt * 2 + 1 );
Update (rt );
}
 
// Query the maximum difference between a and B.
Int queryMax (int a, int B, int rt)
{
If (a = seg [rt]. left & B = seg [rt]. right)
{
Return seg [rt]. max;
}
Int res = 0;
Int mid = (seg [rt]. left + seg [rt]. right)/2;
If (B <= mid)
{
Res = max (res, queryMax (a, B, rt * 2 ));
}
Else if (a> mid)
{
Res = max (res, queryMax (a, B, rt * 2 + 1 ));
}
Else
{
Res = max (res, queryMax (a, mid, rt * 2 ));
Res = max (res, queryMax (mid + 1, B, rt * 2 + 1 ));
}
Return res;
}
 
Int queryMin (int a, int B, int rt)
{
If (a = seg [rt]. left & B = seg [rt]. right)
{
Return seg [rt]. min;
}
Int mid = (seg [rt]. left + seg [rt]. right)/2;
Int res = 999999999;
If (B <= mid)
{
Res = min (res, queryMin (a, B, rt * 2 ));
}
Else if (a> mid)
{
Res = min (res, queryMin (a, B, rt * 2 + 1 ));
}
Else
{
Res = min (res, queryMin (a, mid, rt * 2 ));
Res = min (res, queryMin (mid + 1, B, rt * 2 + 1 ));
}
Return res;

}
 
Int main ()
{
 
Int a, B;
 
Scanf ("% d", & n, & q );
BuildTree (1, n, 1 );
While (q --)
{
Scanf ("% d", & a, & B );
Int max = queryMax (a, B, 1 );
Int min = queryMin (a, B, 1 );
Int val = max-min;
Printf ("% d \ n", val );
}
Return 0;
}

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.