Hdu I hate it (line segment tree)

Source: Internet
Author: User
I Hate ItTime Limit: 9000/3000 ms (Java/Other) Memory Limit: 32768/32768 K (Java/Other) Total Submission (s): 6 Accepted Submission (s ): 2 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 Input

5 61 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5
 


Sample Output

5659HintHuge input,the C function scanf() will work better than cin
 


Authorlinle


Source2007 provincial training team exercise session (6) _ linle

# Include <stdio. h> # include <string. h ># include <algorithm> using namespace std; int stu [200005]; struct Node {int l, r, maxn;} tree [1000000]; void Build (int t, int l, int r) {tree [t]. l = l; tree [t]. r = r; int mid = (l + r)/2; if (l = r) {tree [t]. maxn = stu [mid]; return;} Build (2 * t, l, mid); Build (2 * t + 1, mid + 1, r ); tree [t]. maxn = max (tree [2 * t]. maxn, tree [2 * t + 1]. maxn);} int Query (int t, int x, int y) {int l = tree [T]. l; int r = tree [t]. r; if (l = x & r = y) return tree [t]. maxn; int mid = (l + r)/2; if (x <= mid & y <= mid) return Query (2 * t, x, y ); // here, x <= is written as <by mistake. It has been changed for a long time. Do not mistakenly think that x = y is. It is returned as early as in the previous statement. Actually, it is not. This is a recursive function, actually it is returned in the next function, so you must add the = else if (x> mid) return Query (2 * t + 1, x, y ); else return max (Query (2 * t, x, mid), Query (2 * t + 1, mid + 1, y);} void Modify (int t, int x, int der) {int l, r, mid; l = tree [t]. l; r = tree [t]. r; mid = (l + R)/2; if (l = r) {tree [t]. maxn = der; return;} if (x <= mid) Modify (2 * t, x, der); else Modify (2 * t + 1, x, der ); tree [t]. maxn = max (Query (2 * t, l, mid), Query (2 * t + 1, mid + 1, r); // pay attention to the position of this sentence, different from the question of summation, the following values must be changed first and then updated, that is, update from bottom up} int main () {int m, n, a, B; int I, j; char c; while (scanf ("% d", & n, & m )! = EOF) {for (I = 1; I <= n; I ++) {scanf ("% d", & stu [I]);} Build (1, 1, n); for (I = 0; I <m; I ++) {getchar (); scanf ("% c % d", & c, &, & B); if (c = 'q') {if (a> B) {int tem = a; a = B; B = tem ;} printf ("% d \ n", Query (1, a, B);} else {Modify (1, a, B) ;}} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.