HDU1754线段树

#include<iostream>
#include<cstdio>
using namespace std;
struct node{  
    int s;  
    int l,r;  
};  
struct node tree[2000100];  
int a[200010];  
int create_tree(int h,int x,int y){//建树 
    int mid=(x+y)/2;//中间 
    tree[h].l=x;tree[h].r=y; 
    if(x==y){//如果左右范围相等 
	    tree[h].s=a[x];
	    return tree[h].s;
	}
	int a=create_tree(2*h,x,mid);//向两边分开递归 
	int b=create_tree(2*h+1,mid+1,y);
	tree[h].s=a>b?a:b;
    return tree[h].s;
}
int Q(int h,int x,int y){//询问 
    if(y<tree[h].l || x>tree[h].r)//如果需要查询的范围不在原来的范围内  
        return 0;  
    if(x<=tree[h].l && y>=tree[h].r)//如果需要查询的范围刚好匹配  
        return tree[h].s;  
    int a=Q(2*h,x,y);//向两侧分别递归  
    int b=Q(2*h+1,x,y);    
    return a>b?a:b;;
}
int U(int h,int x){//更新      
    if(x<tree[h].l || x>tree[h].r)  
        return tree[h].s;  
    if(tree[h].l==tree[h].r){  
        tree[h].s=a[tree[h].l];  
        return tree[h].s;  
    }  
    int a=U(2*h,x);  
    int b=U(2*h+1,x);  
    tree[h].s=a>b?a:b;  
    return tree[h].s;     
}
int main(){
	int i,j,k,m,n,x,y;
	while(scanf("%d%d",&n,&m)!=EOF){
	    for(i=1;i<=n;i++)scanf("%d",&a[i]);
	    create_tree(1,1,n);
	    for(i=1;i<=m;i++){  
	        char c;
            getchar();  
            scanf("%c%d%d",&c,&x,&y); 
            if(c=='Q'){  
                cout<<Q(1,x,y)<<endl;  
            }else{   
                a[x]=y;  
                U(1,x);  
            }  
        }  
	}
	return 0;
}

你可能感兴趣的:(线段树,C语言,图论,HDU)