HDU 1754 I Hate It [Line segment tree] [Interval maximum value]

I Hate It 

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

Portal: Click to open the link
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


This question is to change the line segment tree. Each node stores not the sum of the interval, but the maximum value of the interval. Then each root node saves the larger of the root<<1 node and the root<<1|1 node

code show as below:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=200005;
int a[MAXN];
struct Node
{
    int l;
    int r;
    int Max;
} tree[MAXN<<2];
void built(int tr,int l,int r)
{
    tree[tr].l=l;
    tree[tr].r=r;
    tree[tr].Max=0;
    if(tree[tr].l==tree[tr].r)
    {
        tree[tr].Max=a[l];
        return ;
    }
    int mid=(tree[tr].l+tree[tr].r)/2;
    built(tr<<1,l,mid);
    built(tr<<1|1,mid+1,r);
    tree[tr].Max=max(tree[tr<<1].Max,tree[tr<<1|1].Max);///Retain the larger of the child nodes
    return ;
}
void update(int tr,int l,int r,int val)
{
    if(tree[tr].l>=l&&tree[tr].r<=r)
    {
        tree[tr].Max=max(tree[tr].Max,val);///Update the maximum value of the node
        return ;
    }
    int mid=(tree[tr].l+tree[tr].r)/2;
    if(l<=mid)
        update(tr<<1,l,r,val);
    if(mid<r)
        update(tr<<1|1,l,r,val);
    tree[tr].Max=max(tree[tr<<1].Max,tree[tr<<1|1].Max);
    return ;
}
int quary (int tr, int l, int r)
{
    int Max=0;
    if(tree[tr].l>=l&&tree[tr].r<=r)
    {
        return tree[tr].Max;///Return the maximum value each time
    }
    int mid=(tree[tr].l+tree[tr].r)/2;
    if(l<=mid)
        Max=max(Max,quary(tr<<1,l,r));///Return the larger value
    if(mid<r)
        Max=max(Max,quary(tr<<1|1,l,r));
    return Max;
}
intmain()
{
    int n,m;
    while(~scanf("%d %d",&n,&m))
    {
        memset(a,0,sizeof(a));
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        built(1,1,n);
        char ch;
        getchar();
        while(m--)
        {
            scanf("%c",&ch);
            int i;
            int j;
            scanf("%d %d",&i,&j);
            if(ch=='Q')
                printf("%d\n",quary(1,i,j));
            if(ch=='U')
                update(1,i,i,j);
            getchar();
        }
    }
    return 0;
}

Guess you like

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