Description
Many schools are popular for a comparative habit. Teachers like to ask, from XXX to XXX, the highest score is how much.
This makes many students very disgusted.
whether you like it or not, now you need to do is to follow the teacher's request, write a program, mock teacher's inquiry. Of course, teachers sometimes need to update a student's grades.
Input
This topic contains multiple sets of tests, please handle to the end of the file.
On the first line 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 numbered from 1 to N, respectively.
The second line contains n integers representing the initial scores of the N students, of which the number of I represents the student's score for ID i.
Then there's M-line. Each line has a character C ( only ' Q ' or ' U ') , and two positive integers, A/b.
When C is ' Q ', it indicates that this is a query operation, which asks for the highest number of students whose IDs are from a to B (including A, a).
when C is ' U ', it indicates that this is an update operation that requires the student with ID A to change the grade to B.
Output
for each query operation, output the highest score in one line.
Sample Input
5 6
1 2 3) 4 5
Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5
Sample Output
5659
AC Code:
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>Forgot the Max function's header fileUsingNamespace Std;Constint MAXN=200000;The data here is wrong at first.struct Node{int L;int R;int m;} Tree[MAXN*3];This must be 3 times times more.int num[MAXN],ans;voidBuild(int I,int L,int R){Tree[I].l=l; Tree[I].r=r;If(l==r){Tree[I].m=num[l];Return;}int mid= (l+r)/2;Build(I*2, l, mid);Build(I*2+1, mid+1R); Tree[I].m=Max(Tree[I*2].m, tree[I*2+1].m);Back to the maximum value of each interval}voidUpdate(int I,int POS,int Val){int mid= (tree[I].l+tree[I].r)/2;If(Tree[I].l==pos&&tree[I].r==pos){Tree[I].m=val;Return;}ElseIf(POS<=mid){Update(I*2Pos, Val);}Else{Update(I*2+1Pos, Val);} Tree[I].m=Max(Tree[I*2].m, tree[I*2+1].m);}voidQuery(int I,int a,int b){If(Tree[I].l==a&&tree[I].r==b){ans=Max(ans, tree[I].m);Return;}int mid= (tree[I].l+tree[I].r)/2;If(A>mid)Query(I*2+1AB);/* Here is wrong, left and right endpoint confused, error occurred, run error can only input three sets of data will automatically end */ElseIf(b<=mid)Query(I*2AB);Else{Query(I*2A, mid);Query(I*2+1, mid+1B);}}IntMain(){int nKXY, I;Char Str[11];While(scanf("%d%d", &n, &k)!=eof){For(I=1; I<=n; I++)scanf("%d", &num[I]);Build(1,1N);While(k--){scanf('%s 'Str);It's better to use strings here.If(str[0]==' Q '){scanf("%d%d", &x, &y); Ans=0;Query(1XY);Printf ( "%d\n" Span class= "Sh-symbol" >,ans); else { Scanf ( "%d%d" ,&x ,&y); update (1,x< Span class= "Sh-symbol" >,y); } } return 0;}
Analysis: Line tree problems can also be done with a tree-like array
Experience: See so many mistakes above to know what my experience is.
E-i Hate It