Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1754
Time Limit: 9000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 35640 accepted submission (s): 14034
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 row of each test, there are two positive integers n and M (0 <n <= 200000,0 <m <5000), representing the number of students and the number of operations respectively.
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 input5 61 2 3 4 5q 1 5u 3 6q 3 4Q 4 5u 2 9Q 1 5
Sample output5659
HintHuge input, the C function scanf () will work better than Cin-=- =-= classic line Segment tree template question, however, it will take a long time to use it flexibly.
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>#include <iostream>#define Maxx 200010int str[Maxx],root[Maxx*4];int n,m;int sum=0;using namespace std;void make_tree(int l,int r,int rt){ if(l==r) { root[rt]=str[l]; return ; } int mid=(l+r)/2; make_tree(l,mid,rt*2); make_tree(mid+1,r,rt*2+1); root[rt]=max(root[rt*2],root[rt*2+1]);}void update(int l,int r,int rt,int a,int b){ if(l == r&&l ==a) { root[rt]=b; return ; } int mid=(l+r)/2; if(a<=mid) { update(l,mid,rt*2,a,b); } else { update(mid+1,r,rt*2+1,a,b); } root[rt]=max(root[rt*2],root[rt*2+1]);}void query(int l,int r,int rt,int left,int right){ if(l==left && r==right) { sum=max(sum,root[rt]); return ; } int mid=(l+r)/2; if(left>mid) query(mid+1,r,rt*2+1,left,right); else if(right<=mid) query(l,mid,rt*2,left,right); else { query(l,mid,rt*2,left,mid); query(mid+1,r,rt*2+1,mid+1,right); }}int main(){ int i,j; char str1[2]; int x,y; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1;i<=n;i++) { scanf("%d",&str[i]); } make_tree(1,n,1); for(j=1;j<=m;j++) { scanf("%s%d%d",str1,&x,&y); if(str1[0]==‘Q‘) { sum=0; query(1,n,1,x,y); printf("%d\n",sum); } else { update(1,n,1,x,y); } } } return 0;}
View code