HDU 1754 I Hate It, hdu1754
I Hate It
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 66742 Accepted Submission (s): 25969
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
Authorlinle
Source 2007 provincial training team exercise session (6) _ linle session
Recommendlcy: I will not repeat it in Chinese. Solution: the single point update of the Line Segment tree and the maximum value of the interval. It is also a template question, as long as the template is ready. For details, see the code. Attach the AC code: 1 # include <bits/stdc ++. h> 2 # define lrt rt <1 3 # define rrt rt <1 | 1 4 # define lson l, m, lrt 5 # define rson m + 1, r, rrt 6 using namespace std; 7 const int maxn = 200005; 8 int maxv [maxn <2]; 9 int n, q; 10 char op [5]; 11 12 void push_up (int rt) {13 maxv [rt] = max (maxv [lrt], maxv [rrt]); 14} 15 16 void build (int l, int r, int rt) {17 if (l = r) {18 int num; 19 scanf ("% d", & num); 20 maxv [rt] = num; 21 return; 22} 23 int m = (l + r)> 1; 24 build (lson); 25 build (rson); 26 push_up (rt ); 27} 28 29 void modify (int p, int val, int l, int r, int rt) {30 if (l = r) {31 maxv [rt] = val; 32 return; 33} 34 int m = (l + r)> 1; 35 if (p <= m) 36 modify (p, val, lson ); 37 else38 modify (p, val, rson); 39 push_up (rt); 40} 41 42 int query (int ql, int qr, int l, int r, int rt) {43 if (ql <= l & r <= qr) 44 return maxv [rt]; 45 int m = (L + r)> 1; 46 int maxr = 0; 47 if (ql <= m) 48 maxr = max (maxr, query (ql, qr, lson); 49 if (qr> m) 50 maxr = max (maxr, query (ql, qr, rson); 51 return maxr; 52} 53 54 int main () {55 while (~ Scanf ("% d", & n, & q) {56 build (1, n, 1); 57 int a, B; 58 while (q --) {59 scanf ("% s % d", op, & a, & B); 60 if (op [0] = 'q ') 61 printf ("% d \ n", query (a, B, 1, n, 1); 62 else63 modify (a, B, 1, n, 1 ); 64} 65} 66 return 0; 67}View Code