HDOJ question 1754 I Hate It (Single Point update of Line Segment tree, finding the maximum interval)
I Hate ItTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 44713 Accepted Submission (s): 17548
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 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.
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
Author linle
Source 2007 provincial training team exercise session (6) _ linle session
Recommend lcy | We have carefully selected several similar problems for you: 1698 1542 2795 1540 1255 it is faster to call a Mao function than to define a macro. if you write a macro definition, it will time out. If you write a function, it will pass ac code
#include
#include
//#define max(a,b) (a>b?a:b)int node[200020<<2];int max(int a,int b){if(a>b)return a;return b;}void pushup(int tr){node[tr]=max(node[tr<<1],node[tr<<1|1]);}void build_tr(int l,int r,int tr){if(l==r){scanf("%d",&node[tr]);return;}int mid=(l+r)>>1;build_tr(l,mid,tr<<1);build_tr(mid+1,r,tr<<1|1);pushup(tr);}void update(int num,int add,int l,int r,int tr){if(l==r){node[tr]=add;return;}int mid=(l+r)>>1;if(num>mid)update(num,add,mid+1,r,tr<<1|1);elseupdate(num,add,l,mid,tr<<1);pushup(tr);}int query(int x,int y,int l,int r,int tr){int ans=0;if(x<=l&&y>=r)return node[tr];int mid=(l+r)>>1;if(x<=mid)ans=max(ans,query(x,y,l,mid,tr<<1));if(y>mid)ans=max(ans,query(x,y,mid+1,r,tr<<1|1));return ans;}int main(){int n,m;while(scanf("%d%d",&n,&m)!=EOF){build_tr(1,n,1);char s[2];int a,b;while(m--){scanf("%s%d%d",s,&a,&b);if(s[0]=='U'){update(a,b,1,n,1);}elseprintf("%d\n",query(a,b,1,n,1));}}}