I hate it
Time Limit: 9000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 36007 accepted submission (s): 14178
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 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
The line segment tree is much more complicated than the tree array, but the AC template is easy to solve, and 1200ms is helpless. I hope it will be optimized in the future.
#include<iostream>#include<cstring>#include<cstdio>#define M 500010using namespace std;int n,m;struct H{ int mi,ma,sum,l,r;}trees[M];int num[M];void build_trees(int jd,int l,int r){ trees[jd].l=l;trees[jd].r=r; if(l==r) {trees[jd].ma=num[l];return ;} int mid=(l+r)/2; build_trees(jd*2,l,mid); build_trees(jd*2+1,mid+1,r); trees[jd].ma=max(trees[jd*2].ma,trees[jd*2+1].ma);}int query(int jd,int l,int r){ int ans=0; if(l<=trees[jd].l&&r>=trees[jd].r) return trees[jd].ma; int mid=(trees[jd].l+trees[jd].r)/2; if(l<=mid) ans=max(ans,query(jd*2,l,r)); if(r>mid) ans=max(ans,query(jd*2+1,l,r)); return ans;}void update(int jd,int l,int a){ if(trees[jd].l==trees[jd].r) {trees[jd].ma=a;return;} int mid = (trees[jd].l+trees[jd].r)/2; if(l<=mid) update(jd*2,l,a); else update(jd*2+1,l,a); trees[jd].ma=max(trees[jd*2].ma,trees[jd*2+1].ma);}int main(){ int i,j; int a,b; char c[1]; while(~scanf("%d%d",&n,&m)) { memset(num,0,sizeof num); for(i=1;i<=n;i++) scanf("%d",&num[i]); build_trees(1,1,n); for(i=1;i<=m;i++) { scanf("%s%d%d",c,&a,&b); if(c[0]=='Q') { printf("%d\n",query(1,a,b)); } if(c[0]=='U') { update(1,a,b); } } } return 0;}