HDU(1754),线段树,单点替换,区间最值

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754

线段树模板题,update功能是单点替换,query是访问区间最大值。

#include <stdio.h>
#include <algorithm>

using namespace std;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 200010;
int MAX[maxn<<2];

void PushUP(int rt)
{
    MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);
}

void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%d",&MAX[rt]);
        return ;
    }
    int m = (l+r)>>1;
    build(lson);
    build(rson);
    PushUP(rt);
}

void update(int p,int sc,int l,int r,int rt)
{
    if(l==r)
    {
        MAX[rt] = sc;
        return ;
    }
    int m = (l+r)>>1;
    if(p<=m) update(p,sc,lson);
    else update(p,sc,rson);
    PushUP(rt);
}

int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
        return MAX[rt];
    int m = (l+r)>>1;
    int ret = 0;
    if(L<=m) ret = max(ret,query(L,R,lson));
    if(R>m) ret = max(ret,query(L,R,rson));
    return ret;
}

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,n,1);
        while(m--)
        {
            char ch[2];
            int a,b;
            scanf("%s%d%d",ch,&a,&b);
            if(ch[0]=='Q')
            {
                printf("%d
",query(a,b,1,n,1));
            }
            else
                update(a,b,1,n,1);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/TreeDream/p/5724604.html