hdu1754 I Hate It


用线段树维护区间最大值,支持修改单点的值。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define LL(x) (x<<1)
#define RR(x) (x<<1|1)

const int maxn = 200005;
struct node 
{
    int l, r;
    int Max;
}tree[maxn*4];
int num[maxn];
void build(int rt, int l, int r) 
{
    tree[rt].l = l;
    tree[rt].r = r;
    if(tree[rt].l == tree[rt].r) {
        tree[rt].Max = num[l];
        return ;
    }
    int mid = (tree[rt].l + tree[rt].r)>>1;
    build(LL(rt), l, mid);
    build(RR(rt), mid+1, r);
    tree[rt].Max = max(tree[LL(rt)].Max, tree[RR(rt)].Max);
}
void update(int rt, int pos, int val) {
    if(tree[rt].l == tree[rt].r) {
        tree[rt].Max = val;
        return ;
    }
    int mid = (tree[rt].l + tree[rt].r)>>1;
    if(pos <=mid) update(LL(rt), pos, val);
    else update(RR(rt), pos, val);
    tree[rt].Max = max(tree[LL(rt)].Max, tree[RR(rt)].Max);
}
int query(int rt, int l, int r) {
    if(l <= tree[rt].l && r >= tree[rt].r) {
        return tree[rt].Max;
    }
    int mid = (tree[rt].l + tree[rt].r)>>1;
    int ret = 0;
    if(l <= mid) ret =max(ret, query(LL(rt), l, r) );
    if(r > mid ) ret =max(ret, query(RR(rt), l, r) );
    return ret;
}
int main()
{
    int n, m, i, a, b, val;
    while(scanf("%d%d",&n,&m)!=EOF) 
    {
        for(i=1; i<=n; ++i) scanf("%d",&num[i]);
        build(1,1,n);
        char op[10];
        while(m--)
        {
            scanf("%s",op);
            if(op[0]=='Q') {
                scanf("%d%d",&a,&b);
                printf("%d\n", query(1,a,b));
            }
            else if(op[0]=='U') {
                scanf("%d%d",&a,&val);
                update(1,a,val);
            }
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值