HDU - 1754 D - I Hate It

D - I Hate It

HDU - 1754

A more popular habit of many schools. The teachers really like to ask, so and so to so and so from among the highest score is. This allows many students are disgusted.

Whether you like it or not, now you need to do is, is in accordance with the requirements of the teacher, to write a program to simulate the teacher asked. Of course, teachers sometimes need to update certain students achievements.

Input

This topic contains multiple sets of test, to deal with 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), represents the number of operations and the number of students. Student ID numbers are compiled from 1 to N. The second line contains N integers, representing the N initial student grades, where the i-th student ID is representative of the results i. The next M rows. Each line has a character C (just take 'Q' or 'U'), and two positive integers A, B. When C is the 'Q' time, that this is a query operation, it queries the ID from A to B (including A, B) among the students, the highest score is. When C is the 'U' time, that this is an update operation, the ID required to change grades A student B.

Output

For each query operation, which outputs the highest score in a row.

Sample Input

6 5 
1 2 3 4 5
Do 1 5 the
U 3 6
Do 3 4
Do 4 5 the
U 2 9
Do 1 5

Sample Output

5
6
5
9

analysis:

It is seeking the maximum segment tree template title. (But do not know why the use of macros will max T).

Code:

#include <cstdio>
#include <algorithm>
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
using namespace std;
typedef long long ll;
const int maxn=222222;
int tree[maxn<<2];
int a[maxn+6];
void Pushup(int root)
{
    tree[root]=max(tree[root<<1],tree[root<<1|1]);
}
void build(int l,int r,int root)
{
    if(l==r)
    {
        tree[root]=a[l];
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    Pushup(root);
}
void update(int k,int dis,int l,int r,int root)
{
    if(l==r)
    {
        tree[root]=k;
        return;
    }
    int mid=(l+r)>>1;
    if(dis<=mid) update(k,dis,lson);
    else update(k,dis,rson);
    Pushup(root);
}
int query(int L,int R,int l,int r,int root)
{
    if(L<=l&&r<=R)
    {
        return tree[root];
    }
    if(l>R||r<L) return 0;
    int mid=(l+r)>>1;
    int res=0;
    if(L<=mid) res=max(res,query(L,R,lson));
    if(R>mid) res=max(res,query(L,R,rson));
    return res;
}
int main ()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
        build(1,n,1);
        while(m--)
        {
            char c;
            getchar();
            c=getchar();
            int L,R;
            scanf("%d%d",&L,&R);
            if(c=='Q') printf("%d\n",query(L,R,1,n,1));
            else update(R,L,1,n,1);
        }
    }
    return 0;
}

 

 

Guess you like

Origin www.cnblogs.com/studyshare777/p/12293606.html