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;
}
原文地址:https://www.cnblogs.com/bruce27/p/5425467.html