B-I hate it
Time limit:3000 Ms
Memory limit:32768kb
64bit Io format:% I64d & % i64usubmit status
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 topic contains multiple groups of tests. Please process it 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
Output 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
5659
Hint
Huge input,the C function scanf() will work better than cin
This question is similar to that of hdu1166, which is also an entry to the line tree. Learn how to create a line tree, change the line tree, and query the line tree.
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<string> 5 #include<stdlib.h> 6 #include<algorithm> 7 using namespace std; 8 struct node 9 {10 int l,r;11 int num;12 int mid()13 {14 return (l+r)/2;15 }16 }a[1000000];17 int maxn;18 19 void btree(int l,int r,int step)20 {21 a[step].l=l;22 a[step].r=r;23 if(l==r)24 {25 scanf("%d",&a[step].num);26 return ;27 }28 int mid=a[step].mid();29 btree(l,mid,step*2);30 btree(mid+1,r,step*2+1);31 a[step].num=max(a[step*2].num,a[step*2+1].num);32 }33 34 void ptree(int step,int vis,int val)35 {36 if(a[step].l==a[step].r&&a[step].l==vis)37 {38 a[step].num=val;39 return ;40 }41 int mid=a[step].mid();42 if(vis>mid) ptree(step*2+1,vis,val);43 else ptree(step*2,vis,val);44 a[step].num=max(a[step*2].num,a[step*2+1].num);45 }46 47 void maxtree(int l,int r,int step,int L,int R)48 {49 if(L<=l&&r<=R)50 {51 maxn=max(maxn,a[step].num);52 return ;53 }54 int mid=a[step].mid();55 if(L>mid)56 maxtree(mid+1,r,step*2+1,L,R);57 else if(R<=mid)58 maxtree(l,mid,step*2,L,R);59 else60 {61 maxtree(l,mid,step*2,L,R);62 maxtree(mid+1,r,step*2+1,L,R);63 }64 }65 66 int main()67 {68 int n,ope;69 while(scanf("%d %d",&n,&ope)!=EOF)70 {71 btree(1,n,1);72 while(ope--)73 {74 getchar();75 char ch;76 int x,y;77 scanf("%c %d %d",&ch,&x,&y);78 if(ch==‘Q‘)79 {80 maxn=-1;81 maxtree(1,n,1,x,y);82 printf("%d\n",maxn);83 }84 if(ch==‘U‘)85 ptree(1,x,y);86 }87 }88 89 return 0;90 }
View code