HDU_1754 I Hate It

//绝对原创,欢迎交流!!
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAX 200010
struct node
{
    int left;
    int right;
    int value;
};
node tree[MAX*4];

void Build_tree(int p,int l,int r)
{
    tree[p].left=l;
    tree[p].right=r;
    if(l==r)
    {
        scanf("%d",&tree[p].value);
        return ;
    }
    int mid=(l+r)/2;
    Build_tree(p*2,l,mid);
    Build_tree(p*2+1,mid+1,r);
    tree[p].value=max(tree[p*2].value,tree[p*2+1].value);
}

void Update(int p,int s,int v)
{
    if(s>tree[p].right||s<tree[p].left)
        return ;
    if(s>=tree[p].left&&s<=tree[p].right)
    {
        tree[p].value=max(tree[p].value,v);
        if(tree[p].left==tree[p].right)
            return ;
    }
    Update(p*2,s,v);
    Update(p*2+1,s,v);
}

int Query(int p,int l,int r)
{
    if(l<=tree[p].left&&tree[p].right<=r)
        return tree[p].value;
    int mid=(tree[p].left+tree[p].right)/2;
    if(l>mid)
        return Query(p*2+1,l,r);
    else if(r<=mid)
        return Query(p*2,l,r);
    else
        return max(Query(p*2,l,mid),Query(p*2+1,mid+1,r));
}
int main()
{
    char str[20];
    int n,m,i,j,l,r,p,v;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        Build_tree(1,1,n);
        for(i=0; i<m; i++)
        {
            scanf("%s",&str);
            if(str[0]=='U')
            {
                scanf("%d%d",&p,&v);
                Update(1,p,v);
            }
            else
            {
                scanf("%d%d",&l,&r);
                printf("%d\n",Query(1,l,r));
            }
        }
    }
    return 0;
}

你可能感兴趣的:(tree,HDU,datebase,segment)