HDU 1754 I Hate It (line segment tree), hdu1754
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
The question of the line tree, set achievements, update (Single Point), query for one
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace std; const int maxn = 200005; int n, m; int tree [maxn <2]; void pushup (int rs) // update upwards, you can also enter {tree [rs] = max (tree [rs <1], tree [rs <1 | 1]);} void build (int rs, int l, int r) {if (l = r) {scanf ("% d", & tree [rs]); return ;} int mid = (l + r)> 1; build (rs <1, l, mid); build (rs <1 | 1, mid + 1, r ); pushup (rs);} void update (int rs, int l, int r, int X, int y) {if (l = r) {tree [rs] = y; return;} int mid = (l + r)> 1; if (x <= mid) update (rs <1, l, mid, x, y); // note the <=, and the value of mid corresponds to else update (rs <1 | 1, mid + 1, r, x, y); pushup (rs);} int query (int rs, int l, int r, int x, int y) {if (l = x & r = y) return tree [rs]; int mid = (l + r)> 1; int re = 0; if (y <= mid) return query (rs <1, l, mid, x, y); // note that the <= here corresponds to the value of mid in else if (x> mid) return query (rs <1 | 1, mid + 1, r, x, y); else {re = max (re, query (Rs <1, l, mid, x, mid); re = max (re, query (rs <1 | 1, mid + 1, r, mid + 1, y); return re;} return 0;} int main () {char c [2]; int x, y; while (~ Scanf ("% d", & n, & m) {getchar (); build (1, 1, n); for (int I = 0; I <m; I ++) {scanf ("% s % d", c, & x, & y); if (c [0] = 'q ') cout <query (, n, x, y) <endl; else update (, n, x, y) ;}} return 0 ;}
Do not pay attention to mid, debug =-=