PKU 3264 balanced lineup line segment tree

Source: Internet
Author: User

The first time I got a question done at PKU, I got killed. I 've been calling this question for a long time.

But I heard that the RMQ-ST algorithm is more efficient, and I use the line segment tree for 4047 Ms.

I was a little surprised that it could slow down like this ~~~~~~~~~

 

# Include <stdio. h> <br/> # define n 50005 </P> <p> struct node {<br/> int L; <br/> int R; <br/> int min; <br/> int Max; <br/>}; </P> <p> node tree [3 * n]; <br/> int arr [N]; </P> <p> void buid_tree (int K, int left, int right) <br/>{< br/> int mid; <br/> int L, R; <br/> tree [K]. L = left; tree [K]. R = right; <br/> If (right-left = 1) {<br/> tree [K]. min = tree [K]. max = arr [left]; <br/> return; <br/>}< br/> L = 2 * k; r = L + 1; <br /> Mid = (tree [K]. L + tree [K]. r)/2; <br/> buid_tree (L, left, mid); <br/> buid_tree (R, mid, right); <br/> tree [K]. min = tree [l]. min> tree [R]. min? Tree [R]. Min: Tree [l]. min; <br/> tree [K]. max = tree [l]. max> tree [R]. Max? Tree [l]. MAX: tree [R]. max; <br/> return; <br/>}</P> <p> int tree_search_min (int K, int left, int right) <br/>{< br/> int mid; <br/> int L, R; <br/> int LM = 1000005, Rm = 1000005; <br/> If (left <= tree [K]. L & right> = tree [K]. r) {<br/> return tree [K]. min; <br/>}< br/> L = 2 * k; r = L + 1; <br/> mid = (tree [K]. L + tree [K]. r)/2; <br/> If (left <mid) lm = tree_search_min (L, left, right); <br/> If (Right> mid) Rm = tree_se Arch_min (R, left, right); <br/> return lm <RM? LM: Rm; <br/>}</P> <p> int tree_search_max (int K, int left, int right) <br/>{< br/> int mid; <br/> int L, R; <br/> int LM = 0, Rm = 0; <br/> If (left <= tree [K]. L & right> = tree [K]. r) {<br/> return tree [K]. max; <br/>}< br/> L = 2 * k; r = L + 1; <br/> mid = (tree [K]. L + tree [K]. r)/2; <br/> If (left <mid) lm = tree_search_max (L, left, right); <br/> If (Right> mid) rm = tree_search_max (R, left, right); <br/> return lm> RM? LM: Rm; <br/>}</P> <p> int main () <br/>{< br/> int N, Q; <br/> int I; <br/> int A, B; <br/> while (scanf ("% d", & N, & Q) = 2) {<br/> for (I = 1; I <= N; I ++) <br/> scanf ("% d", arr + I ); <br/> buid_tree (1, 1, n + 1); <br/> for (I = 0; I <q; I ++) {<br/> scanf ("% d", & A, & B); <br/> printf ("% d/N", tree_search_max (1,, B + 1)-tree_search_min (1, A, B + 1); <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.