[HDU] 1754 - I Hate It - Block - Single Point Update - Interval Maximum

I Hate It

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 86684    Accepted Submission(s): 33234


Problem Description
A habit of comparison is popular in many schools. Teachers like to ask what is the highest score from so-and-so to so-and-so.
This disgusts many students.

Whether you like it or not, what you need to do now is to write a program according to the teacher's request to simulate the teacher's inquiry. Of course, teachers sometimes need to update a student's grades.
 

Input
This question contains multiple sets of tests, please process until the end of the file.
In the first line of each test, there are two positive integers N and M ( 0<N<=200000, 0<M<5000 ), which represent 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 grades of the N students, where the ith number represents the grades of the student whose ID is i.
Next there are M lines. Each line has a character C (only take 'Q' or 'U'), and two positive integers A, B.
When C is 'Q', it means that this is a query operation, which asks the students whose IDs range from A to B (including A and B), which is the highest grade.
When C is 'U', it means that this is an update operation, which requires changing the grade of the student whose ID is A 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 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
 

Sample Output
 
  
5 6 5 9
Hint
Huge input,the C function scanf() will work better than cin
 

Author
linle
 

Source
 

Recommend
lcy

Single point update RMQ

I suddenly wanted to write chunks, so I grabbed this question and wrote it

feel ok
#include <bits/stdc++.h>
using namespace std;

const int N = 200020;

int ma[N];
int n, m;
int block;
int num;
you are with [N];
int br[N];
int bo [N];
int maxx[N];

void build()
{
	memset(maxx, -1, sizeof(maxx));
	block = sqrt(n);
	num = n / block;
	if(n % block){
		num++;
	}
	for(int i = 1; i <= n; i ++){
		bo[i] = (i - 1) / block + 1;
	}
	for(int i = 1; i <= num; i ++){
		bl[i] = (i - 1) * block + 1;
		br[i] = i * block;
	}
	br[num] = min(n, br[num]);
	for(int i = 1; i <= num; i ++){
		for(int j = bl[i]; j <= br[i]; j ++){
			maxx[i] = max(maxx[i], ma[j]);
		}
	}
}

void update(int x, int v)
{
	int t = bo [x];

	ma [x] = v;
	for(int i = bl[t]; i <= br[t]; i ++){
		maxx[t] = max(maxx[t], ma[i]);
	}
}

int quary (int l, int r)
{
	int x = bo [l];
	int y = bo [r];
	int years = -1;

	for(int i = x + 1; i < y; i ++){
		yrs = max(yrs, maxx[i]);
	}
	if(x != y){
        for(int i = l; i <= br[x]; i ++){
            yrs = max(yrs, ma[i]);
        }
        for(int i = bl[y]; i <= r; i ++){
            yrs = max(yrs, ma[i]);
        }
	}
	else{
        for(int i = l; i <= r; i ++){
            yrs = max(yrs, ma[i]);
        }
	}

	return ans;
}

intmain()
{
	while(scanf("%d%d", &n, &m) == 2){
		for(int i = 1; i <= n; i ++){
			scanf("%d", &ma[i]);
		}
		build();
		for(int i = 0; i < m; i ++){
			char ops[100];
			int l, r;

			scanf("%s%d%d", ops, &l, &r);
			if(ops[0] == 'Q'){
				printf("%d\n", quary(l, r));
			}
			else{
				update(l, r);
			}
		}
	}

	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325559910&siteId=291194637