hdu 1754(种一棵线段树)

感谢小鹏同学的耐心debug。。。。。。发现是少写一个return。。。导致调试好长时间。。。囧。。


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 200005
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
struct node{
    int L,R,val;
}edge[N<<2];
int buit(int L,int R,int i){
    edge[i].L=L,edge[i].R=R;
    if(L==R){
        scanf("%d",&edge[i].val);
        return edge[i].val;
    }
    int mid=(edge[i].L+edge[i].R)/2;
    return edge[i].val=max(buit(mid+1,R,RR(i)),buit(L,mid,LL(i)));
}
void updata(int id,int x,int i){
    if(edge[i].L==edge[i].R){
        edge[i].val=x;return;
    }
    int mid=(edge[i].L+edge[i].R)/2;
    if(id<=mid) updata(id,x,LL(i));
    else updata(id,x,RR(i));
    edge[i].val=max(edge[RR(i)].val,edge[LL(i)].val);
}
int query(int L,int R,int i){
    if(L==edge[i].L && R==edge[i].R) return edge[i].val;
    int mid=(edge[i].L+edge[i].R)/2;
    if(R<=mid) return query(L,R,LL(i));
    else if(L>mid) return query(L,R,RR(i));
    else return max(query(mid+1,R,RR(i)),query(L,mid,LL(i)));
}
int main(){
    int n,m,x,y;
    char s[5];
    while(scanf("%d%d",&n,&m)!=-1){
        buit(1,n,1);
        while(m--){
            scanf("%s%d%d",s,&x,&y);
            if(s[0]=='Q') printf("%d\n",query(x,y,1));
            else updata(x,y,1);
        }
    }
}


你可能感兴趣的:(hdu 1754(种一棵线段树))