HDU - 1754 I Hate It

bryce1010模板
http://acm.hdu.edu.cn/showproblem.php?pid=1754
区间求最大值+点更新
(修改pushup的操作,维护节点保存儿子节点中的最大值)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const int MAXN=2e5+10;

int sum[MAXN<<2];
void pushup(int rt)
{
    sum[rt]=max(sum[rt<<1],sum[rt<<1|1]);
}
void build(int l,int r,int rt=1)
{
    if(l==r)
    {
        scanf("%d",&sum[rt]);
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}

void update(int pos,int val,int l,int r,int rt=1)
{
    if(l==r)
    {
        sum[rt]=val;
        return;
    }
    int m=(l+r)>>1;
    if(pos<=m)
        update(pos,val,lson);
    else
        update(pos,val,rson);
    pushup(rt);

}

int query(int L,int R,int l,int r,int rt=1)
{
    //包含
    int ans=-1;
    if(L<=l&&r<=R)
    {
        return sum[rt];
    }
    int m=(l+r)>>1;

    if(L<=m)
    {
        ans=max(ans,query(L,R,lson));
    }
    if(m<R)
    {
        ans=max(ans,query(L,R,rson));
    }
    return ans;
}




int main()
{
    int n,m;
//    freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,n,1);
        char ch;
        int l,r;
        for(int i=0;i<m;i++)
        {
            cin>>ch>>l>>r;
            if(ch=='Q')
            {
                printf("%d\n",query(l,r,1,n,1));
            }
            else
            {
                update(l,r,1,n,1);
            }
        }
    }


    return 0;
}



posted @ 2018-07-19 23:58  Bryce1010  阅读(79)  评论(0编辑  收藏  举报