[HDU] 1754 I hate it -- maximum value of the single-point update interval of the Line Segment tree

Source: Internet
Author: User
I hate it

Time Limit: 9000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 37448 accepted submission (s): 14816


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 output

565 9 HintHuge input, the C function scanf () will work better than CIN

 

Authorlinle

 

Question: Obviously, this is the most important question for the single-point update interval of a line segment tree. It is very easy to update a single point. You can use the UPDATE function to update it to a single point, the key is the problem of finding the maximum value of the interval. If we calculate the maximum value at the end of the query, it may take more time than the direct brute force, therefore, the correct solution to this problem is to update the values of all passing nodes during the update process.

The AC code is as follows:

1 # include <cstdio> 2 # include <cstring> 3 # include <algorithm> 4 using namespace STD; 5 6 const int Len = 200020; 7 8 struct line 9 {10 int left; 11 int right; 12 INT Ma; 13} line [Len * 5]; 14 15 void buildt (int l, int R, int step) // create a tree, and the maximum value of the initialization interval is 16 {17 line [STEP]. left = L; 18 line [STEP]. right = r; 19 line [STEP]. MA = 0; 20 if (L = R) 21 return; 22 int mid = (L + r)/2; 23 buildt (L, mid, step * 2 ); 24 buildt (Mid + 1, R, step * 2 + 1); 25} 26 27 int Update (int l, int R, int value, int step) // update the function, returns the integer 28 {29 If (line [STEP]. left = line [STEP]. right) {// If the node reaches the deepest point, it indicates that the single point to be updated is found. The value is 30 line [STEP]. MA = value; 31 return max (line [STEP/2*2]. ma, line [STEP/2*2 + 1]. ma); // returns the 32} 33 int mid = (line [STEP] of the node and Its sibling node. left + line [STEP]. right)/2; // search for 34 if (r <= mid) 35 line [STEP]. MA = Update (L, R, value, step * 2); 36 else if (L> mid) 37 line [STEP]. MA = Update (L, R, value, step * 2 + 1); 38 else {// if the target line segment is on either side of the midpoint of the current line segment, then, search for the two subnodes of the node at the same time and compare them with 39 line [STEP]. ma = max (Update (L, mid, value, step * 2), update (Mid + 1, R, value, step * 2 + 1 )); 40} 41 return max (line [STEP/2*2]. ma, line [STEP/2*2 + 1]. ma); // returns the 42} 43 44 int DFS (int l, int R, int step) of the node and Its sibling node) // DFS answers 45 {46 If (L = line [STEP]. left & R = line [STEP]. right) // if the target interval is found, the return value is 47 re. Turn line [STEP]. ma; 48 int mid = (line [STEP]. left + line [STEP]. right)/2; 49 If (r <= mid) 50 return DFS (L, R, step * 2); 51 else if (L> mid) 52 return DFS (L, r, step * 2 + 1); 53 else {54 return max (DFS (L, mid, step * 2), DFS (Mid + 1, R, step * 2 + 1); // return the number of larger than 55} 56} 57 58 59 int main () 60 {61 int n, m; 62 // freopen ("in.txt", "r", stdin); 63 while (scanf ("% d", & N, & M )! = EOF) {64 buildt (1, n, 1); 65 for (INT I = 1; I <= N; I ++) {66 int T; 67 scanf ("% d", & T); 68 Update (I, I, T, 1); 69} 70 for (INT I = 0; I <m; I ++) {71 char Query [2]; 72 int A, B; 73 scanf ("% S % d", query, & A, & B ); 74 if (Query [0] = 'U') 75 Update (a, a, B, 1); 76 else if (Query [0] = 'q ') {77 printf ("% d \ n", DFS (A, B, 1); 78} 79} 80} 81 return 0; 82}

 

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.