Title Description:
The teacher wants to know from the so-and-so classmate, the highest score is how many, now asks you to program the simulation teacher's inquiry. Of course, teachers sometimes need to update a student's grades.
Input Description:
The input includes multiple sets of test data.
The first line of each set of inputs is two positive integers N and m (0 < N <= 30000,0 < M < 5000), representing the number of students and the number of operations respectively.
Student ID number from 1 to N.
The second line contains n integers representing the initial scores of the N students, where the number of I represents the student's score for ID i.
Next M-line, each line has a character C (only ' Q ' or ' U '), and two positive integers, a, B, when C is ' Q ', indicating that this is a query operation, he asked the ID from a to B (including A, a) among students, the highest performance is how much
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 Description:
For each query operation, output the highest score in one line.
Input Example:
5 7
1 2 3) 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 4 5
U 2 9
Q 1 5
Output Example:
5
6
5
9
Thinking Analysis:
- Pay attention to multiple sets of test data in the topic, need to loop to determine the input
- One of the Q query, a and B to determine the size of a, and b corresponding to the array index will be reduced by one
- The ontology takes the array and stores the first n numbers, traverses m times, operates, takes the most direct way, the time complexity is O (n), the improved method considers the data structure of the segment tree, modifies O (logn), Queries O (Logn), but to Preprocess O (Nlogn), or a block list (in fact, a block array is OK).
Quite simply, now we're going to split the entire n-size array into sqrt (n) (square root N) decimal groups with sqrt (n) elements per decimal group
For example 1 2 3 4 5 6 7 8 9
Now split into
1 2 3
4 5 6
7 8 9
Then for each small piece, we have to change the value of the corresponding position, but also to record the maximum value of the entire small block.
If I update, the maximum value of that small piece increases, it is simple, the maximum value also increases.
What if the maximum is changed to a smaller number? In order to be correct, we can only sweep the whole small piece and re-calculate the maximum value.
Therefore, the complexity of the modification is O (sqrt (n))
Sample code (Java)
Import Java.util.Scanner; Public classMain { Public Static void Main(string[] args) {intm=0, n=0;intIintA =0, B =0; Scannerinch=NewScanner (System.inch); while(inch. Hasnext ()) {N =inch. Nextint (); M =inch. Nextint ();//System.out.println (N + "" + M); int[] score =New int[N]; for(i=0;inch. Hasnext () && i<n; i++) {Score[i] =inch. Nextint ();//System.out.println (Score[i] + "");} String C =NULL; for(i=0;inch. Hasnext () && i< M; i++) {c =inch. Next (); A =inch. Nextint (); B =inch. Nextint (); Deal (C,a,b,score); } } }Private Static void deal(String C,intAintBint[] score) {intBegin,end;if(C.equals ("Q") {end = Math.max (A, b); Begin = Math.min (A, B)-1;intmax = Score[begin]; for(inti=begin;i<end;i++) {if(Max<score[i]) {max = score[i]; }} System. out. println (max); }Else if(C.equals ("U")) {score[a-1] = b; } }}
Reference:
http://www.nowcoder.com/questionTerminal/3897c2bcc87943ed98d8e0b9e18c4666
My QR code is as follows, welcome to exchange discussion
You are welcome to pay attention to the "It question summary" subscription number. Every day to push the classic face test and interview tips, are dry! The QR code of the subscription number is as follows:
Huawei Machine Question "1"