zoukankan      html  css  js  c++  java
  • hdu 1754 I Hate It(一个AC了,却有着奇葩问题的题目)

     这道题目,近来为了熟悉线段树,有做了一遍。

     本来不难,却发现一个奇葩问题(见代码72行):

    #include "algorithm"
    #include "iostream"
    #include "cstring"
    #include "cstdio"
    #include "string"
    #include "stack"
    #include "cmath"
    #include "queue"
    #include "set"
    #include "map"
    
    #define lson l , m , rt << 1
    #define rson m + 1 , r , rt << 1 | 1
    
    typedef long long ll;
    using namespace std;
    
    const int inf=0x3f3f3f3f;
    const int maxn=200000+5;
    
    int n,m;
    
    int tree[maxn<<2];
    
    void pushUp(int rt)
    {
        tree[rt] = max( tree[rt<<1] , tree[rt<<1|1] );
    }
    void build(int l,int r,int rt)
    {
        if(l==r)
        {
            //tree[rt]=a[l];
            scanf("%d",&tree[rt]);
            return;
        }
        int m = (l+r)>>1;
        build(lson);
        build(rson);
        pushUp(rt);
    }
    void update(int index,int value,int l,int r,int rt)
    {
        if(l==r)
        {
            tree[rt]=value;
            return;
        }
        int m = (l+r)>>1;
        if(index<=m)update(index,value,lson);
        else update(index,value,rson);
        pushUp(rt);
    }
    int query(int L,int R,int l,int r,int rt)
    {
        if( L<=l && r<=R )
        {
            return tree[rt];
        }
        int m = (l+r)>>1;
        int ret = -inf;
        if( L<=m )ret= max(ret, query( L, R,lson) );
        if( m<R) ret=max(ret, query( L, R,rson) );
        return ret;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            build(1,n,1);
            char ch[5];//如果定义为"char ch;"且下面的%s改为%c,会超内存。为什么?!
            int x,y;
            for(int i=0;i<m;++i)
            {
                scanf("%s %d %d",ch,&x,&y);
                if(ch[0]=='Q')
                {
                    printf("%d
    ",query(x,y,1,n,1));
                }
                else if(ch[0]=='U')
                {
                    update(x,y,1,n,1);
                }
            }
        }
    
        return 0;
    }
    
  • 相关阅读:
    DHCP DHCPv6
    DHCPv6协议
    IPv6邻居发现协议
    CentOS下禁止防火墙
    centOS下更新yum源
    centOS下yum报错
    Flink+Kafka整合的实例
    Flink基本概念
    Ubuntu16.04下配置ssh免密登录
    Zookeeper+Kafka的单节点配置
  • 原文地址:https://www.cnblogs.com/bruce27/p/5425467.html
Copyright © 2011-2022 走看看