HDU 1754 I Hate It (initial application of Line Segment tree)

Source: Internet
Author: User

HDU 1754 I Hate It (initial application of Line Segment tree)
HDU 1754 I Hate It


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 line of each test, there are two positive integers N and M (0 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 61 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5 

Sample Output

5659 



I will not explain the question in Chinese.

Solution: Use the line tree to store students' scores. Note: During the creation process, the parent node value is the maximum value in the child node. During the modification process, the entire line segment tree should be updated. During the search process, pay attention to the boundary.



# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; # define NUM 200000int N, M; int S [4 * NUM], V [4 * NUM], R [4 * NUM], L [4 * NUM]; void build (int n, int l, int r) {// create a line segment tree if (l = r) {R [n] = l; L [n] = l; S [n] = V [l-1]; return;} else {int temp = (l + r)/2; R [2 * n + 1] = temp; R [2 * n + 2] = r; L [2 * n + 1] = l; L [2 * n + 2] = temp + 1; build (2 * n + 1, L [2 * n + 1], R [2 * n + 1]); build (2 * n + 2, L [2 * n + 2], R [2 * n + 2]); S [n] = max (S [2 * n + 1], S [2 * n + 2]);} return;} void modify (int n, int x, int v) {// change X to Vif (L [n] = x & R [n] = x) {S [n] = v; return ;} int mid = (L [n] + R [n])/2; if (x <= mid) {modify (n * 2 + 1, x, v );} else {modify (n * 2 + 2, x, v);} S [n] = max (S [n * 2 + 1], S [n * 2 + 2]);} int cnt; int find (int n, int l, int r) {// find the maximum value from range l to r if (l <= L [n] & r> = R [n]) {return S [n];} int mid = (L [n] + R [n])/2; int min = -1; if (l <= mid) {min = max (min, find (n * 2 + 1, l, r);} if (r> mid) {min = max (min, find (n * 2 + 2, l, r) ;}return min ;}int main () {while (scanf ("% d \ n", & N, & M )! = EOF) {memset (S, 0, sizeof (S); memset (V, 0, sizeof (V); memset (R, 0, sizeof (R )); memset (L, 0, sizeof (L); int cnt = 0; for (int I = 0; I <N; I ++) {scanf ("% d ", & V [cnt ++]);} L [0] = 1; R [0] = cnt; build (0, L [0], R [0]); char ch; for (int I = 0; I <M; I ++) {scanf ("% c", & ch); if (ch = 'q ') {int a, B; scanf ("% d \ n", & a, & B); printf ("% d \ n", find (0,, b);} else if (ch = 'U') {int a, B; scanf ("% d \ n", & a, & B ); modify (0, 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.