Want to see more problem-solving reports:
Http://blog.csdn.net/wangjian8006/article/details/7870410
Reprinted please indicate the source:Http://blog.csdn.net/wangjian8006
Give a string of numbers, then give a range of a B, and output the maximum number from A to B and the Minimum Number Difference
Solution: Line Segment tree
use the line segment tree to obtain the minimum and maximum values from A to B, directly subtract the output
# Include <iostream> using namespace STD; # define maxv 50100 # define maxt 1 <18 # define max (A, B) A> B? A: B # define min (A, B) A> B? B: aint a [maxv], Tl [maxt], TR [maxt], Tmax [maxt], tmin [maxt]; // TL and TR represent the numbers on the left and right of the node v range, which facilitates the query // Tmax and tmin, representing the maximum and minimum values of node v void createtree (int v, int F, int t) {TL [v] = f; tr [v] = T; If (F = T) {Tmax [v] = A [f]; tmin [v] = A [f]; return;} int mid = (F + T)> 1; createtree (v <1, F, mid ); createtree (v <1) | 1, Mid + 1, t); Tmax [v] = max (Tmax [v <1], tmax [(v <1) | 1]); tmin [v] = min (tmin [v <1], tmin [(v <1) | 1]);} int querytree (int v, int L, int R, int flag) {If (L = tl [V] & R = tr [v]) {return (flag? Tmin [v]: Tmax [v]);} int mid = (TL [v] + Tr [v])> 1; if (r <= mid) return querytree (v <1, L, R, flag); else if (L> mid) return querytree (v <1) | 1, L, R, flag); else {If (FLAG) return min (querytree (v <1, L, mid, flag), querytree (v <1) | 1, mid + 1, R, flag); else return max (querytree (v <1, L, mid, flag), querytree (v <1) | 1, mid + 1, R, flag) ;}} int main () {int N, Q, L, R, I; while (scanf ("% d ", & N, & Q )! = EOF) {for (I = 1; I <= N; I ++) scanf ("% d", & A [I]); createtree (1, 1, n ); while (Q --) {scanf ("% d", & L, & R); printf ("% d \ n", querytree (1, L, R, 0)-querytree (1, L, R, 1) ;}} return 0 ;}