I Hate ItTime
limit:9000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 48513 Accepted Submission (s): 18986
Problem description Many schools are popular for a comparative habit. Teachers like to ask, from XXX to XXX, the highest score is how much.
This makes many students very disgusted.
Whether you like it or not, now you need to do is to follow the teacher's request, write a program, mock teacher's inquiry. Of course, teachers sometimes need to update a student's grades.
Input This topic contains multiple sets of tests, please handle to the end of the file.
On the first line of each test, there are two positive integers N and M (0<n<=200000,0<m<5000), which represent the number of students and the number of operations respectively.
Student ID numbers are numbered from 1 to N, respectively.
The second line contains n integers representing the initial scores of the N students, of which the number of I represents the student's score for ID i.
Then there's M-line. Each line has a character C (only ' Q ' or ' U '), and two positive integers, A/b.
When C is ' Q ', it indicates that this is a query operation, which asks for the highest number of students whose IDs are from a to B (including A, a).
When C is ' U ', it indicates that this is an update operation that requires the student with ID A to change the grade to B.
Output outputs the highest score in a row for each query operation.
Sample Input
5 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5
Sample Output
5659HintHuge input,the C function scanf () would work better than CIN
Authorlinle
Source2007 Provincial Training Team Practice (6) _linle session
Relatively simple line-segment tree.
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> #include < cstring>using namespace std; #define Lson l,m,rt<<1#define Rson m+1,r,rt<<1|1int max[2222222<<2]; void pushup (int rt) {Max[rt]=max (max[rt<<1],max[rt<<1|1]);} void build (int l,int R,int RT) {if (r==l) {scanf ("%d", &max[rt]); Return } int m= (R+L) >>1; Build (Lson); Build (Rson); Pushup (RT);} void update (int a,int b,int l,int r,int RT) {if (l==r) {max[rt]=b; Return } int m= (L+R) >>1; if (a<=m) update (A,b,lson); else update (A,b,rson); Pushup (RT);} int query (int l,int r,int l,int r,int RT) {if (l<=l &&r<=r) {return MAX[RT]; } int res=0; int m= (R+L) >>1; if (l<=m) Res=max (Res,query (L,r,lson)); if (r>m) Res=max (Res,query (L,r,rson)); return res;} int main () {int n,m; int A, B; while (~sCANF ("%d%d", &n,&m)) {build (1,n,1); Char s[2]; while (m--) {scanf ("%s", s); scanf ("%d%d", &a,&b); if (s[0]== ' Q ') printf ("%d\n", Query (a,b,1,n,1)); else update (a,b,1,n,1); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1754 (maximum segment tree interval)