Hdu 1754 I Hate It (Single Point update + maximum range of Line Segment tree), hdu1754
I Hate ItTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
Line Segment tree entry question. In the recent multi-School Joint competition, I found that each competition involves a line segment tree, but I have never learned a line segment tree before, so I decided to learn it.
# Include <cstdio> # include <algorithm> using namespace std; # define lson l, mid, root <1 # define rson mid + 1, r, root <1 | 1 const int N = 2000000 + 50; struct node {int l, r, mmax;} a [4 * N]; void build_tree (int l, int r, int root) {a [root]. l = l; a [root]. r = r; if (l = r) {scanf ("% d", & a [root]. mmax); return;} int mid = (l + r)> 1; build_tree (lson); build_tree (rson); a [root]. mmax = max (a [root <1]. mmax, a [roo T <1 | 1]. mmax);} void update (int l, int r, int root, int k) {if (l = a [root]. l & r = a [root]. r) {a [root]. mmax = k; return;} int mid = (a [root]. l + a [root]. r)> 1; if (r <= mid) update (l, r, root <1, k); else if (l> mid) update (l, r, root <1 | 1, k); else {update (lson, k); update (rson, k);} a [root]. mmax = max (a [root <1]. mmax, a [root <1 | 1]. mmax); // update the entire tree after updating a single vertex} int Query (int l, int r, int root ){ If (l = a [root]. l & r = a [root]. r) return a [root]. mmax; int mid = (a [root]. l + a [root]. r)> 1; if (r <= mid) return Query (l, r, root <1); else if (l> mid) return Query (l, r, root <1 | 1); else return max (Query (lson), Query (rson);} int main () {int n, m, a, B; char ch [5]; while (~ Scanf ("% d", & n, & m) {build_tree (1, n, 1); while (m --) {getchar (); scanf ("% s % d", ch, & a, & B); if (ch [0] = 'q ') printf ("% d \ n", Query (a, B, 1); else update (a, a, 1, B) ;}} return 0 ;}