线段树hdu1754 I Hate It

#include <stdio.h>
#define LL(a) ((a)<<1)
#define RR(a) ((a)<<1|1)

#define N 600001
#define MAX(a,b) (((a) > (b)) ? (a):(b))
//inline int MAX(int a, int b){return ((a > b) ? a:b);}
typedef struct NodeTag {
    int st, ed, value;
}Node;

Node tt[N]; // segment tree
int max, num[200001];

int build(int place, int st, int ed)
{
    int mid = (st+ed)/2, a, b;
    tt[place].st = st;
    tt[place].ed = ed;
    if(st == ed)
        return tt[place].value = num[st];
    a = build(LL(place), st, mid);
    b = build(RR(place), mid+1, ed);
    return tt[place].value = MAX(a,b);
}
void update(int place, int st, int ed, int value)
{
    if(tt[place].st == st && tt[place].ed == ed)
    {  
        tt[place].value = value;
//        place >>= 1;
//        while(place > 0)
//        {
//            int a = tt[LL(place)].value,
//            b = tt[RR(place)].value;
//            tt[place].value = MAX(a,b);
//            place >>= 1;
//        }
    }else{
        int mid = (tt[place].st + tt[place].ed) / 2;
        if(ed <= mid) update(LL(place), st, ed, value);
        else if(st >= mid+1) update(RR(place), st, ed, value);
        else{}
        
        tt[place].value = MAX(tt[LL(place)].value, tt[RR(place)].value);
    }
}

int query(int place, int st, int ed)
{
    int mid = (tt[place].st + tt[place].ed)/2;
    if(tt[place].st == st && tt[place].ed == ed)
        return tt[place].value;
    else {
        if(ed <= mid) return query(LL(place), st, ed);
        else if(st >= mid+1)return query(RR(place), st, ed);
        else {
            int a = query(LL(place), st, mid), 
                b = query(RR(place), mid+1, ed);
            return MAX(a,b);
        }
    }
}

int main()
{
    
    int n, m, i, a, b;
    char op[3];
    while(scanf("%d", &n) != EOF)
    {
        scanf("%d", &m);
        for(i = 1; i <= n; ++i)scanf("%d", num+i);
        build(1, 1, n);
        while(m--){
            scanf("%s%d%d", op, &a, &b);
            switch (op[0]) {
                case 'Q':
                    printf("%d\n", query(1,a,b));
                    break;
                 case 'U':
                    update(1, a, a, b);
                    break;
                default:
                    break;
            }
        }
    }
    
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值