HDU-1754-I Hate It [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): 28044 Accepted Submission (s): 11126

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 
 

 

Code:

 

Write the meaning of the node int variable as the maximum value.

 

# Include <stdio. h> # define M 200005 struct node {int l, r, max;} tree [M * 4]; int a [M]; int max (int a, int B) {return a> B? A: B;} void build (int p, int l, int r) {tree [p]. l = l; tree [p]. r = r; if (l = r) {tree [p]. max = a [l]; return;} int mid = (l + r)> 1; int next = p <1; build (next, l, mid ); build (next + 1, mid + 1, r); tree [p]. max = max (tree [next]. max, tree [next + 1]. max);} void update (int p, int side, int val) {if (tree [p]. l = side & tree [p]. r = side) {tree [p]. max = val; return;} int mid = (tree [p]. l + tree [p]. r)> 1; int next = p <1; If (side> mid) update (next + 1, side, val); else update (next, side, val); tree [p]. max = max (tree [next]. max, tree [next + 1]. max);} int query (int p, int l, int r) {if (tree [p]. l = l & tree [p]. r = r) return tree [p]. max; int mid = (tree [p]. l + tree [p]. r)> 1; int next = p <1; if (r <= mid) return query (next, l, r); else if (l> mid) return query (next + 1, l, r); else {return max (query (next, l, mid), query (next + 1, mid + 1, r )); // This example If max is implemented with a macro definition, it will time out ..} // It may be because there are too many calls .. use the function} int main () {int n, m, l, r, I; while (scanf ("% d", & n, & m )! = EOF) {for (I = 1; I <= n; I ++) scanf ("% d", & a [I]); build (1, 1, n ); for (I = 1; I <= m; I ++) {char c [2]; // if you write a character here, add getchar () scanf ("% s", c); if (c [0] = 'q') {scanf ("% d", & l, & r ); printf ("% d \ n", query (1, l, r);} if (c [0] = 'U ') {scanf ("% d", & l, & r); update (1, l, r) ;}} 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.