Hdu 1754 I Hate It
I Hate ItTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission (s): 37627 Accepted Submission (s): 14893
Problem DescriptionMany 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.
InputThis topic contains multiple groups of tests. Please process it until the end of the file.
In the first line of each test, there are two positive integers N and M (0 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.
OutputOutput 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 (maximum range value ):
#include
#include
#include
#includeusing namespace std;int max(int a,int b){ return (a>b?a:b);}int a[200000];struct Node{ int left,right; int t;}node[4*200000];void MakeTree(int l,int r,int i){ node[i].left=l; node[i].right=r; if(l==r) { node[i].t=a[l]; return ; } int mid=(l+r)/2; MakeTree(l,mid,2*i); MakeTree(mid+1,r,2*i+1); node[i].t=max(node[2*i].t,node[2*i+1].t);}void UpdateTree(int i,int x,int y){ int l=node[i].left; int r=node[i].right; int mid=(l+r)/2; if(x==l&&x==r) { node[i].t=y; return ; } if(x>mid) UpdateTree(2*i+1,x,y); else UpdateTree(2*i,x,y); node[i].t=max(node[2*i].t,node[2*i+1].t);}int QueryTree(int x,int y,int i){ if(node[i].left==x && node[i].right==y) return node[i].t; //if(node[i].left==node[i].right) //return 0; int m=(node[i].left+node[i].right)/2; if(x>m) return QueryTree(x,y,2*i+1); else if(y<=m) return QueryTree(x,y,2*i); else return max(QueryTree(x,m,2*i),QueryTree(m+1,y,2*i+1));}int main (){ int T,n,m; int i,j,x,y; char c[10]; while(~scanf("%d%d",&n,&m)) { for(i=1;i<=n;i++) scanf("%d",&a[i]); MakeTree(1,n,1); getchar(); for(j=1;j<=m;j++) { scanf("%c%d%d",&c[0],&x,&y); getchar(); if(c[0]=='U') UpdateTree(1,x,y); else cout<