problem DescriptionMany 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.
InputThis 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 for each query operation, output the highest score in one line.
Sample Input5 61 2 3) 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5
Sample Output5659Note: This is an introductory question for a line segment tree, which mainly understands its basic operations.
1#include <cstdio>2#include <cmath>3#include <cstring>4#include <iostream>5#include <algorithm>6 using namespacestd;7 Const intMAXN =200005;8 structData9 {Ten intLeft , right, Max; One} tree[3*MAXN]; A intSCORE[MAXN]; - voidPushup (intnode) - { theTree[node]. max = max (tree[2*node]. Max, tree[2*node+1]. MAX); - } - voidBuildintLeftintRightintNode//Building a segment tree - { + //the interval represented by the current node -Tree[node].left =Left ; +Tree[node].right =Right ; A //The left and right intervals are the same, the node is a leaf, and Max should store the value of the corresponding student. at if(left = =Right ) { -Tree[node]. Max =Score[left]; - return; - } - //Recursive establishment of the left and right sub-trees - intMid = left + (right-left)/2; inBuild (left, Mid,2*node); -Build (mid+1, right,2*node+1); to //get the maximum value from the subtree + pushup (node); - } the intQueryintLeftintRightintNode//max value for query interval [left,right] * { $ //the range checked is not in scopePanax Notoginseng if(Tree[node].left > Right | | tree[node].right <Left ) - return 0; the //the interval to be checked contains the interval managed by the current node + if(left <= tree[node].left && tree[node].right <=Right ) A returnTree[node]. Max; the intMid = (Tree[node].left + tree[node].right)/2; + //The interval that is checked is partially intersected with the interval managed by the current node - if(Right <=mid) $ returnQuery (left, right,2*node); $ Else if(Left >mid) - returnQuery (left, right,2*node+1); - Else the returnMax (query (left, right,2*node), query (left, right,2*node+1)); - }Wuyi voidUpdata (intPosintValintNode//Update the value of a POS point the { - //Update the max value of the current node WuTree[node]. Max =Max (Val, Tree[node]. MAX); - //pos points are leaf nodes About if(Tree[node].left = = pos && Tree[node].right = =POS) { $Tree[node]. Max =Val; - return; - } - if(Pos <= tree[2*node].right) AUpdata (POS, Val,2*node); + Else theUpdata (POS, Val,2*node+1); - } $ intMain () the { the intN, M; the while(~SCANF ("%d%d", &n, &m)) { the for(intI=1; i<=n; i++) -scanf"%d", &score[i]); inBuild1N1); the CharC; the intA, B; About for(intI=1; i<=m; i++){ the GetChar (); thescanf"%c%d%d", &c, &a, &b); the if(c = ='U') +Updata (A, B,1); - Else theprintf"%d\n", query (A, B,1));Bayi } the } the return 0; -}
View Code
HDU 1754 I Hate It