HUD1754 (完全二叉树)

#include<cstdio>
const int size=200010;
int a[size];
struct node{
	int s,l,r;
};
struct node tree[size*10];
int max(int x,int y){
	return x>y?x:y;
}
int create_tree(int h,int p,int q){
	tree[h].l=p;
	tree[h].r=q;
	int x1,x2,mid;
	mid=(p+q)/2;
	if(p==q){
		tree[h].s=a[p];
		return tree[h].s;
	}
	x1=create_tree(2*h,p,mid);
	x2=create_tree(2*h+1,mid+1,q);
	tree[h].s=max(x1,x2);
	return tree[h].s;
}
int dfs(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 x1=dfs(2*h,x,y);
	int x2=dfs(2*h+1,x,y);	
	return max(x1,x2);	
}
int update(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 x1,x2;
	x1=update(2*h,x);
	x2=update(2*h+1,x);
	tree[h].s=max(x1,x2);
	return tree[h].s;
}
int main(){
	int i,j,k,m,n;
	int x,y;
	char c;
	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++){
			getchar();
			scanf("%c %d %d",&c,&x,&y);
			if(c=='Q'){
				printf("%d\n",dfs(1,x,y));
			}else{ 
				a[x]=y;
				update(1,x);
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值