• HDU 1754 I Hate It 线段树单点更新求最大值


    题目链接

    线段树入门题,线段树单点更新求最大值问题。

    #include <bits/stdc++.h>
    using namespace std;
    #define m ((l+r)>>1)
    #define lson root<<1,l,m
    #define rson root<<1|1,m+1,r
    #define N 30005
    struct Tree
    {
        int l,r,ans;
    }tree[N<<2];
    void build(int root,int l,int r)
    {
        tree[root].l=l;
        tree[root].r=r;
        if(l==r){
            scanf("%d",&tree[root].ans);
            return ;
        }
        build(lson);
        build(rson);
        tree[root].ans=max(tree[root<<1].ans,tree[root<<1|1].ans);
    }
    void update(int root,int l,int r,int pos,int val)
    {
        if(l==r){
            tree[root].ans=val;
            return ;
        }
        if(pos<=m) update(lson,pos,val);
        if(pos>m) update(rson,pos,val);
        tree[root].ans=max(tree[root<<1].ans,tree[root<<1|1].ans);
    }
    int query(int root,int l,int r,int ll,int rr)
    {
        //查询区间包含当前区间
        if(ll<=l&&rr>=r){
            return tree[root].ans;
        }
        int cnt=-1;
        if(ll<=m)cnt=max(cnt,query(lson,ll,rr));
        if(rr>m) cnt=max(cnt,query(rson,ll,rr));
        return cnt;
    }
    int main()
    {
        int n,k;
        while(scanf("%d%d",&n,&k)!=EOF){
            build(1,1,n);
            int a,b;char c[5];
            while(k--){
                scanf("%s%d%d",c,&a,&b);
                if(c[0]=='U') update(1,1,n,a,b);
                else{
                    if(a>b) swap(a,b);
                    printf("%d
    ",query(1,1,n,a,b));
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    2020软件工程个人作业06————软件工程实践总结作业
    2020软件工程作业01
    班级博客V2.1版本更新日志
    博客园班级手机版
    班级帮助文档
    问题累计
    2020 软件工程作业06
    2020 软件工程作业04
    2020 软件工程作业03
    2020软件工程02
  • 原文地址:https://www.cnblogs.com/Ritchie/p/6216916.html
Copyright © 2020-2023  润新知