HDU 1754 I Hate It 线段树单点更新求最大值

题目链接

线段树入门题,线段树单点更新求最大值问题。

 

#include <bits/stdc++.h>
using namespace std;
#define m ((l+r)>>1)
#define lson root<<1,l,m
#define rson root<<1|1,m+1,r
#define N 30005
struct Tree
{
    int l,r,ans;
}tree[N<<2];
void build(int root,int l,int r)
{
    tree[root].l=l;
    tree[root].r=r;
    if(l==r){
        scanf("%d",&tree[root].ans);
        return ;
    }
    build(lson);
    build(rson);
    tree[root].ans=max(tree[root<<1].ans,tree[root<<1|1].ans);
}
void update(int root,int l,int r,int pos,int val)
{
    if(l==r){
        tree[root].ans=val;
        return ;
    }
    if(pos<=m) update(lson,pos,val);
    if(pos>m) update(rson,pos,val);
    tree[root].ans=max(tree[root<<1].ans,tree[root<<1|1].ans);
}
int query(int root,int l,int r,int ll,int rr)
{
    //查询区间包含当前区间
    if(ll<=l&&rr>=r){
        return tree[root].ans;
    }
    int cnt=-1;
    if(ll<=m)cnt=max(cnt,query(lson,ll,rr));
    if(rr>m) cnt=max(cnt,query(rson,ll,rr));
    return cnt;
}
int main()
{
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF){
        build(1,1,n);
        int a,b;char c[5];
        while(k--){
            scanf("%s%d%d",c,&a,&b);
            if(c[0]=='U') update(1,1,n,a,b);
            else{
                if(a>b) swap(a,b);
                printf("%d\n",query(1,1,n,a,b));
            }
        }
    }
    return 0;
}

 

posted @ 2016-12-24 11:05  Ritchie丶  阅读(313)  评论(0编辑  收藏  举报