HDU 1754 I Hate It (线段树)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxN 200005
int A[maxN];
struct SegTreeNode{
    int val;
}s[maxN<<2];    //开四倍空间
void pushUp(int rt){    //更新根节点
    s[rt].val=max(s[rt<<1].val,s[rt<<1|1].val);
}
void build(int l,int r,int rt){ //构造线段树
    if(l==r){
        s[rt].val=A[l];
        return;
    }
    int m=(l+r)/2;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    pushUp(rt); //向上更新
}
void update(int L,int c,int l,int r,int rt){    //单点更新
    if(l==r){
        s[rt].val=c;
        return;
    }
    int m=(l+r)>>1;
    if(L<=m){
        update(L,c,l,m,rt<<1);
    }
    else{
        update(L,c,m+1,r,rt<<1|1);
    }
    pushUp(rt); //向上更新
}
int query(int L,int R,int l,int r,int rt){  //区间最值查询
    if(L<=l&&r<=R)return s[rt].val; //区间在[L,R]中间
    if(L>r||R<l)return 0;   //区间在[L,R]之外
    int m=(l+r)>>1;
    int ans=INT_MIN;
    if(L<=m){   //L在中间值左边
        ans=max(ans,query(L,R,l,m,rt<<1));
    }
    if(R>m){    //R在中间值右边
        ans=max(ans,query(L,R,m+1,r,rt<<1|1));
    }
    return ans;
}
int main(){
    ios::sync_with_stdio(false);    //流加速
    int n,m;
    while(cin>>n>>m){
        for(int i=1;i<=n;i++){
            cin>>A[i];
        }
        build(1,n,1);
        while(m--){
            int a,b;
            char c;
            cin>>c>>a>>b;
            if(c=='U'){
                update(a,b,1,n,1);
            }
            else cout<<query(a,b,1,n,1)<<endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值