• HDU 1754 I Hate It(单点更新,区间求最大值)


    题意:n个点m次操作,每次操作给出c,a,b;若c为‘Q’,则查询【a,b】区间最大值;若c为‘U’,将第a个点更新为b;

    思路:线段树单点更新,区间求极值;

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int n,m;
    int tree[5000010];
    int a,b,c;
    void pushup(int pos)
    {
        tree[pos]=max(tree[2*pos],tree[2*pos+1]);
    }
    void build(int l,int r,int pos)
    {
        int mid=(l+r)/2;
        if(l==r)
        {
            scanf("%d",&tree[pos]);
            return;
        }
        build(l,mid,2*pos);
        build(mid+1,r,2*pos+1);
        pushup(pos);
    }
    void update(int p,int val,int l,int r,int pos)
    {
        int mid=(l+r)/2;
        if(l==r)
        {
            tree[pos]=val;
            return;
        }
        if(p<=mid) update(p,val,l,mid,2*pos);
        else update(p,val,mid+1,r,2*pos+1);
        pushup(pos);
    }/**/
    int query(int L,int R,int l,int r,int pos)
    {
        int mid=(l+r)/2;
        if(L<=l&&r<=R)
        {
            return tree[pos];
        }
        int mm=0;
        if(L<=mid)
            mm=max(mm,query(L,R,l,mid,2*pos));
        if(mid<R)
            mm=max(mm,query(L,R,mid+1,r,2*pos+1));
        return mm;
    }
    int main()
    {
        int i,j,k;
        char ch;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            build(1,n,1);
            for(i=0;i<m;i++)
            {
                getchar();
                scanf("%c%d%d",&ch,&b,&c);
                if(ch=='Q')
                    printf("%d
    ",query(b,c,1,n,1));
                else
                {
                    update(b,c,1,n,1);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    Git 上传本地项目
    virtual和override
    ASP .NET依赖注入理解
    dotnet不是内部或外部的命令,也不是可运行的程序或批处理文件
    C++ 简单选择排序
    C++ 排序
    iOS UIDynamic
    iOS Modal
    C++ 折半查找
    C++ 二叉链表
  • 原文地址:https://www.cnblogs.com/dashuzhilin/p/4535482.html
Copyright © 2020-2023  润新知