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/>}