Basic Line Segment tree

Source: Internet
Author: User

 

Balanced lineup

Time Limit: 5000 Ms memory limit: 65536 K
Total submit: 35
Accepted: 22
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 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 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
Shortest 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 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 random Sive.

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
 
Code writing is very fast. It seems that the winter training is still a little effective, but there are fatal mistakes, such as wrong answers or right ones, so no matter what the small data you create is
Run it directly, so you have to re-check the code, that is, it is useless to write it quickly.
 
Post the error code submitted for the first time
# Include <iostream> using namespace STD; int N, Q; const int n = 200000; inline int max (int A, int B) {return A> B? A: B;} inline int min (int A, int B) {return a <B? A: B;} inline int mid (int A, int B) {return (a + B)> 1;} struct seg_tree {int L, R; int Mi, ma ;}; seg_tree Dia [4 * n]; int f [n + 5]; void maketree (int l, int R, int index) {Dia [Index]. L = L; Dia [Index]. R = r; If (L = r) {Dia [Index]. MA = Dia [Index]. mi = f [l]; return;} int MIDD = mid (L, R); maketree (L, MIDD, index <1); maketree (MIDD + 1, R, (index <1) + 1); Dia [Index]. ma = max (DIA [index <1]. ma, DIA [(index <1) + 1]. ma); Dia [Index]. mi = min (d IA [index <1]. mi, DIA [(index <1) + 1]. mi);} int finds_max (int l, int R, int c) {If (DIA [C]. L = Dia [C]. r) return Dia [C]. ma; If (DIA [C]. L = L & Dia [C]. R = r) return Dia [C]. ma; int MIDD = mid (DIA [C]. l, DIA [C]. r); If (r <= MIDD) return finds_max (L, R, C * 2); else if (L> MIDD) return finds_max (L, R, C * 2 + 1); else return max (finds_max (L, MIDD, C * 2), finds_max (L, R, C * 2 + 1 )); // This is the tragedy. I wrote an error in the right half. It should be finds_max (MIDD + 1, R, C * 2 + 1)
} Int finds_min (int l, int R, int c) {If (DIA [C]. L = Dia [C]. r) return Dia [C]. mi; If (DIA [C]. L = L & Dia [C]. R = r) return Dia [C]. mi; int MIDD = mid (DIA [C]. l, DIA [C]. r); If (r <= MIDD) return finds_min (L, R, C * 2); else if (L> MIDD) return finds_min (L, R, C * 2 + 1); else return min (finds_min (L, MIDD, C * 2), finds_min (L, R, C * 2 + 1 )); // This is the tragedy .} Int main () {int I, j; while (scanf ("% d", & N, & Q )! = EOF) {for (I = 1; I <= N; I ++) scanf ("% d", & F [I]); maketree (1, n, 1); While (Q --) {scanf ("% d", & I, & J); printf ("% d/N", finds_max (I, j, 1)-finds_min (I, j, 1) ;}} return 0 ;}
Later, I found that the largest and the smallest can be found and changed together. In fact, the error was found during the change. # Include <iostream> using namespace STD; int N, Q; const int n = 50000; const int INF = 999999999; inline int max (int A, int B) {return A> B? A: B;} inline int min (int A, int B) {return a <B? A: B;} inline int mid (int A, int B) {return (a + B)> 1;} struct seg_tree {int L, R; int Mi, ma ;}; seg_tree Dia [4 * n]; int _ max, _ min; int f [n + 5]; void maketree (int l, int R, int index) {Dia [Index]. L = L; Dia [Index]. R = r; If (L = r) {Dia [Index]. MA = Dia [Index]. mi = f [l]; return;} int MIDD = mid (L, R); maketree (L, MIDD, index <1); maketree (MIDD + 1, R, (index <1) + 1); Dia [Index]. ma = max (DIA [index <1]. ma, DIA [(index <1) + 1]. ma); Dia [Index]. mi = min (Dia [Index <1]. mi, DIA [(index <1) + 1]. mi);} void finds (int l, int R, int c) {If (DIA [C]. L = Dia [C]. r) {_ max = max (DIA [C]. ma, _ max); _ min = min (DIA [C]. mi, _ min); return;} If (DIA [C]. L = L & Dia [C]. R = r) {_ max = max (DIA [C]. ma, _ max); _ min = min (DIA [C]. mi, _ min); return;} int MIDD = mid (DIA [C]. l, DIA [C]. r); If (r <= MIDD) finds (L, R, C * 2); else if (L> MIDD) finds (L, R, C * 2 + 1); else {finds (L, MIDD, C * 2); finds (MIDD + 1, R, C * 2 + 1 );}} int main () {// freopen ("1. I N "," r ", stdin); // freopen (" 1.out", "W", stdout); int I, j; while (scanf ("% d ", & N, & Q )! = EOF) {for (I = 1; I <= N; I ++) scanf ("% d", & F [I]); maketree (1, n, 1); While (Q --) {scanf ("% d", & I, & J); _ max = 0; _ min = inf; finds (I, j, 1); printf ("% d/N", _ max-_ min) ;}} return 0 ;}this is the most convenient question to do with rmq, it is also the most suitable for rmq! I have the opportunity to use it for further reading.

 

 

 

 

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.