hdu 1754 I Hate It

Keywords: Java

I Hate It

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
A habit of comparison prevails in many schools. Teachers like to ask, from so-and-so to so-and-so, what is the highest score.
This makes many students very disgusted.

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

Input
This topic contains a number of groups of tests, please process to 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), representing the number of students and the number of operations, respectively.
Student ID numbers range from 1 to N, respectively.
The second line contains N integers representing the initial grades of the N students, and the number i represents the grades of the students whose ID is i.
Then there's line M. Each line has a character C (only'Q'or'U') and two positive integers A and B.
When C is'Q', it means that this is a query operation. It asks the students whose ID is from A to B (including A,B), what is the highest score.
When C is'U', it means that this is an update operation, requiring that the grade of the student whose ID is A be changed 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

# include <cstdio>
# include <cstring>
# include <cmath>
# include <algorithm>

# define MAXN 2000005

using namespace std;

int n, m;
int arr[MAXN * 2];

int max(int a, int b) //You can't use macro definitions to define max, which can lead to serious timeouts
{
    return a > b ? a : b;
}

void Build(int node, int nstart, int nend)
{
    if (nstart == nend)
    {
        scanf("%d", &arr[node]);
        return;
    }
    int mid = (nstart + nend) >> 1;
    Build(node << 1, nstart, mid);
    Build((node << 1) | 1, mid + 1, nend);
    arr[node] = max(arr[node << 1], arr[(node << 1) | 1]);
}

void Update(int node, int nstart, int nend, int pos, int updatenum)
{
    if (nstart == nend)
    {
        arr[node] = updatenum;
        return;
    }
    int mid = (nstart + nend) >> 1;
    if (pos > mid)
    {
        Update((node << 1) | 1, mid + 1, nend, pos, updatenum);
    }
    else
    {
        Update(node << 1, nstart, mid, pos, updatenum);
    }
    arr[node] = max(arr[node << 1], arr[(node << 1) | 1]);
}

int Query(int node, int nstart, int nend, int qstart, int qend)
{
    if (qstart <= nstart && qend >= nend)
    {
        return arr[node];
    }
    int mid = (nstart + nend) >> 1;
    int ret = 0;
    if (qstart <= mid)
    {
        ret = max(ret, Query(node << 1, nstart, mid, qstart, qend));
    }
    if (qend > mid)
    {
        ret = max(ret, Query((node << 1) | 1, mid + 1, nend, qstart, qend));
    }
    return ret;
}

int main(void)
{
    int i;
    char str;
    int a, b;
    while (~scanf("%d %d", &n, &m))
    {
        //memset(arr, 0, sizeof(arr));
        Build(1, 1, n);
        for (i = 0; i < m; i++)
        {
            scanf("%*c%c%d%d", &str, &a, &b);
            if ('U' == str)
            {
                Update(1, 1, n, a, b);
                //printf("%c\n", str[0]);
            }
            else
            {                
                printf("%d\n", Query(1, 1, n, a, b));
            }
        }
    }
    return 0;
}

Posted by leegreaves on Wed, 13 Feb 2019 17:18:19 -0800