zoukankan      html  css  js  c++  java
  • HDU

    bryce1010模板
    http://acm.hdu.edu.cn/showproblem.php?pid=1754
    区间求最大值+点更新
    (修改pushup的操作,维护节点保存儿子节点中的最大值)

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    
    const int MAXN=2e5+10;
    
    int sum[MAXN<<2];
    void pushup(int rt)
    {
        sum[rt]=max(sum[rt<<1],sum[rt<<1|1]);
    }
    void build(int l,int r,int rt=1)
    {
        if(l==r)
        {
            scanf("%d",&sum[rt]);
            return;
        }
        int m=(l+r)>>1;
        build(lson);
        build(rson);
        pushup(rt);
    }
    
    void update(int pos,int val,int l,int r,int rt=1)
    {
        if(l==r)
        {
            sum[rt]=val;
            return;
        }
        int m=(l+r)>>1;
        if(pos<=m)
            update(pos,val,lson);
        else
            update(pos,val,rson);
        pushup(rt);
    
    }
    
    int query(int L,int R,int l,int r,int rt=1)
    {
        //包含
        int ans=-1;
        if(L<=l&&r<=R)
        {
            return sum[rt];
        }
        int m=(l+r)>>1;
    
        if(L<=m)
        {
            ans=max(ans,query(L,R,lson));
        }
        if(m<R)
        {
            ans=max(ans,query(L,R,rson));
        }
        return ans;
    }
    
    
    
    
    int main()
    {
        int n,m;
    //    freopen("in.txt","r",stdin);
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            build(1,n,1);
            char ch;
            int l,r;
            for(int i=0;i<m;i++)
            {
                cin>>ch>>l>>r;
                if(ch=='Q')
                {
                    printf("%d
    ",query(l,r,1,n,1));
                }
                else
                {
                    update(l,r,1,n,1);
                }
            }
        }
    
    
        return 0;
    }
    
    
    
    
  • 相关阅读:
    排座椅
    关于math.h的问题
    客户调查
    排队打水
    删数游戏
    小数背包
    零件分组
    桐桐的组合
    桐桐的数学游戏
    桐桐的全排列
  • 原文地址:https://www.cnblogs.com/bryce1010/p/9386861.html
Copyright © 2011-2022 走看看