I Hate It 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 INPUT5 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5 Sample OUTPUT5 6 5 9 Report:
This question is also a typical line tree problem, need update, build, query Three, do not need pushup and pushdown, because it is not related to child node parent node. In the structure to set a maximum value of one interval of the maximum fraction, query QA==NODE[U].L&&QB==NODE[U].R to find, or separate interval to find the two intervals, and then take the maximum value is the answer; Node[u]. Maxs=max (Node[l (U)].maxs,node[r (u)].maxs);
The code is as follows:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <string>5 #defineL (U) (u<<1)6 #defineR (U) (u<<1|1)7 using namespacestd;8 intn,m,a[200005];9 Chars[2];Ten structpp{ One intMaxs,l,r; A }; -PP node[800005]; - intMaxintAintb) the { - returnA>b?a:b; - } - voidUpdateintUintOneintNow ) + { - if(node[u].l==NODE[U].R) + { Anode[u].maxs=Now ; at return ; - } - intMid= (NODE[U].L+NODE[U].R) >>1; - if(one<=mid) Update (L (U), one,now); - ElseUpdate (R (U), one,now); -node[u].maxs=Max (node[l (U)].maxs,node[r (u)].maxs); in } - intQueryintUintLeftintRight ) to { + if(node[u].l==left&&node[u].r==Right ) - returnNode[u].maxs; the intMid= (NODE[U].L+NODE[U].R) >>1; * if(Right<=mid)returnQuery (L (U), left,right);//return or not? $ Else if(Left>mid)returnQuery (R (U), left,right);Panax Notoginseng Else - returnMax (Query (L (U), left,mid), query (R (U), mid+1, right)); the } + voidBuildintUintLeftintRight ) A { theNode[u].l=left;node[u].r=Right ; + if(left==Right ) - { $node[u].maxs=A[left]; $ return ; - } - intMid= (left+right) >>1; the Build (L (U), left,mid); -Build (R (U), mid+1, right);Wuyinode[u].maxs=Max (node[l (U)].maxs,node[r (u)].maxs); the } - intMain () Wu { - //freopen ("hate.in", "R", stdin); About $ while(SCANF ("%d%d", &n,&m) = =2) - { - - for(intI=1; i<=n;i++) Ascanf"%d",&a[i]); +Build1,1, n); the for(intI=1; i<=m;i++) - { $scanf"%s", s); the if(s[0]=='Q') the { the intE,r; theCin>>e>>R; -Cout<<query (1, E,r) <<Endl; in } the Else the { About intE,r; theCin>>e>>R; theUpdate1, e,r); the } + } - } the return 0;Bayi}
Precautions:
1. In query, is return query () or not return? To see if the topic needs to return the value of the query. If you just record the value of a query, return it directly;
2, on the site to evaluate the data must see whether it is read into the multiple sets of data, or read only one set of data at a time.
Hdoj 1754 I Hate It segment Tree second question