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 question contains multiple groups of tests, please process 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 outputs the highest score in one row for each query operation. Sample input5 61 2 3 4 5q 1 5u 3 6q 3 4Q 4 5u 2 9Q 1 5 sample output5659
HintHuge input, the C function scanf () will work better than CIN
Line Segment tree:
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # define mMax 200003 5 6 using namespace STD; 7 int Q [4 * mMax]; 8 int N, F, Ql, Qr, P, V; 9 10 void Update (int r, int L, int R) 11 {12 int m = (L + r) /2; 13 if (L = r) 14 Q [R] = V; 15 else {16 if (P <= m) 17 Update (R * 2, l, m); 18 else19 Update (R * 2 + 1, m + 1, R); 20 Q [R] = max (Q [R * 2], Q [R * 2 + 1]); 21} 22} // update the line segment tree 23 24 int query (int r, int L, int R) 25 {26 int Ans =-1; 27 int M = (L + r)/2; 28 If (QL <= L & R <= QR) 29 return Q [R]; 30 if (QR <= m) 31 return query (R * 2, l, m); 32 else if (QL> m) 33 return query (R * 2 + 1, m + 1, R); 34 else35 ans = max (query (R * 2, l, m), query (R * 2 + 1, m + 1, R )); 36 return ans; 37} // query the interval 38 39 int main () 40 {41 while (~ Scanf ("% d", & N, & F) {42 // memset (Q, 0, sizeof (q); 43 for (INT I = 0; I <n; I ++) {44 scanf ("% d", & V); 45 p = I + 1; 46 update (1, 1, n ); 47} 48 for (INT I = 0; I <F; I ++) {49 char op [5]; 50 int A, B; 51 scanf ("% S % d", op, & A, & B); 52 If (OP [0] = 'q') {53 QL =, qr = B; 54 printf ("% d \ n", query (1, 1, n);} 55 else {56 p = A, V = B; 57 update (1, 1, n); 58} 59} 60} 61 Return 0; 62}