POJ 3264 Problem Solving report

Source: Internet
Author: User
Balanced Lineup
Time limit:5000 Ms   Memory limit:65536 K
Total submissions:16578   Accepted:7674
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

 

Question: Given a sequence, each element has a value. Now, enter several intervals and ask the maximum and minimum differences between these intervals .....

 

Ideas:

 

The first question of a new line segment tree .....

 

Create a line segment tree, and add Max and min to indicate the maximum and minimum values in the current interval...

 

 

# Include <iostream> <br/> using namespace std; </p> <p> struct T <br/>{< br/> int left, right, max, min; <br/>} tree [150000]; </p> <p> int m, s; </p> <p> void creat (int v, int left, int right) <br/>{< br/> int mid = (left + right)/2; <br/> if (left = right) <br/> {<br/> tree [v]. left = tree [v]. right = left; <br/> tree [v]. max =-1; <br/> tree [v]. min = 10000000; <br/> return; <br/>}< br/> else <br/> {<br/> tree [v]. left = left; tree [v]. right = right; <br/> tree [v]. max =-1; <br/> tree [v]. min = 10000000; <br/>}< br/> creat (v + 1) * 2-1, left, mid ); <br/> creat (v + 1) * 2, mid + 1, right); <br/>}</p> <p> void insert (int v, int left, int right, int value) <br/>{< br/> int mid = (tree [v]. left + tree [v]. right)/2; </p> <p> if (tree [v]. max <value) <br/> tree [v]. max = value; <br/> if (tree [v]. min> value) <br/> tree [v]. min = value; </p> <p> if (left = tree [v]. left & right = tree [v]. right) <br/> return; <br/> else if (mid> = right) <br/> insert (v + 1) * 2-1, left, right, value); <br/> else if (mid <left) <br/> insert (v + 1) * 2, left, right, value ); <br/> else <br/> {<br/> insert (v + 1) * 2-1, left, mid, value ); <br/> insert (v + 1) * 2, mid + 1, right, value ); <br/>}</p> <p> void getmax (int v, int left, int right) <br/> {<br/> int mid = (tree [v]. left + tree [v]. right)/2; <br/> if (left = tree [v]. left & right = tree [v]. right) <br/> {<br/> if (tree [v]. min <s) <br/> s = tree [v]. min; <br/> if (tree [v]. max> m) <br/> m = tree [v]. max; <br/> return; <br/>}< br/> else if (mid> = right) <br/> getmax (v + 1) * 2-1, left, right); <br/> else if (mid <left) <br/> getmax (v + 1) * 2, left, right ); <br/> else <br/> {<br/> getmax (v + 1) * 2-1, left, mid ); <br/> getmax (v + 1) * 2, mid + 1, right ); <br/>}</p> <p> int main () <br/>{< br/> int n, q, I, j, a, B; <br/> while (cin> n> q) <br/>{< br/> creat (0, 1, n ); <br/> for (I = 1; I <= n; I ++) <br/>{< br/> cin> j; <br/> insert (0, I, I, j); <br/>}< br/> for (I = 0; I <q; I ++) <br/>{< br/> scanf ("% d", & a, & B); <br/> m =-1; s = 10000000; <br/> getmax (0, a, B); <br/> printf ("% d/n", m-s ); <br/>}< br/> return 0; <br/>}

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.