*Problem description*

Many schools have a popular habit. Teachers like to ask, from xx to xx, what is the highest score.

This made many students very disgusted.

Whether you like it or not, what you need to do now is to write a program to simulate the instructor's inquiry according to the instructor's requirements. Of course, teachers sometimes need to update their scores.

*Input*

This topic contains multiple groups of tests. Please process it until the end of the file.

In the first row of each test, there are two positive integers n and M (0 <n <= 200000,0 <m <5000), representing the number of students and the number of operations respectively.

Student ID numbers are separated from 1 to n.

The second row contains N integers, indicating the initial score of the N students. The number of I represents the score of the students whose ID is I.

Next there are m rows. Each line has a character C (only 'q' or 'U'), and two positive integers A and B.

When C is 'Q', it indicates that this is a query operation. It asks the students whose ID ranges from A to B (including a and B) about the highest score.

When C is 'U', it indicates that this is an update operation. You must change the score of students whose ID is A to B.

*Output*

Output the highest score in one row for each query operation.

*Sample Input*

5 6

1 2 3 4 5

Q 1 5

U 3 6

Q 3 4

Q 4 5

U 2 9

Q 1 5

*Sample output*

5

6

5

9

Questions ...... Such as the red part ....

Solutions .... Application of the Line Segment tree .. Call the line segment tree template. Each root node stores the maximum values of their subnodes. For each traversal, you only need to find their corresponding locations and return the values stored on their root nodes.

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1754

1 # include <stdio. h> 2 3 # define max (x1, Y1) (X1)> (Y1 )? (X1): (Y1) 4 5 struct node 6 7 {8 9 int left, right, Max; 10 11} t [600010]; 12 13 int A [200005]; 14 15 void create (int u, int L, int R) // The leftmost end of the u node is l, and the rightmost end is R. 16 17 {18 19 T [u]. left = L; 20 21 t [u]. right = r; 22 23 int mid = (L + r)/2; 24 25 if (L = r) 26 27 {T [u]. max = A [R]; return;} 28 29 create (U + u, L, mid); 30 31 create (U + u + 1, Mid + 1, R ); 32 33 t [u]. max = max (T [U + u]. max, t [U + u + 1]. max); // The root node stores the maximum score of their subnodes 34 35} 36 37 int search_max (int l, int R, int I) // query interval [L, r]. In the value function, I should write 1 38 39 {40 41 if (L <= T [I]. left & T [I]. right <= r) 42 43 return T [I]. Max; 44 45 else 46 47 {48 49 int ans1 =-9999999, ans2 =-9999999; 50 51 if (L <= T [I + I]. right) ans1 = search_max (L, R, I + I); 52 53 If (r> = T [I + I + 1]. left) ans2 = search_max (L, R, I + I + 1); 54 55 return max (ans1, ans2 ); 56 57} 58 59} 60 61 void change_tree (INT order, int num, int I) // change the value of order in the input data to num, update the tree 62 63 {64 65 if (T [I]. left = T [I]. right) 66 67 {68 69 T [I]. max = num; 70 71 return; 72 73} 74 75 else 76 77 {78 79 if (Order <= T [I + I]. right) change_tree (Order, num, I + I); 80 81 else change_tree (Order, num, I + I + 1); 82 83 t [I]. max = max (T [I + I]. max, t [I + I + 1]. max); 84 85} 86 87} 88 89 int main () 90 91 {92 93 int n, m, a, B; 94 95 char C; 96 97 int I; 98 99 while (scanf ("% d", & N, & M )! = EOF) 100 101 {102 103 for (I = 1; I <= N; I ++) 104 105 scanf ("% d", & A [I]); 106 107 create (108, n); 109 110 for (I = 1; I <= m; I ++) 111 112 {113 getchar (); 114 115 scanf ("% C % d", & C, & A, & B); 116 117 If (C = 'q ') 118 119 printf ("% d \ n", search_max (A, B, 1); 120 121 else122 123 {124 125 A [a] = B; 126 127 change_tree (A, B, 1); 128 129} 130 131 132} 133 134 return 0; 135 136}

Code