HDU1754 线段树 (完全二叉树)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int maxn=200000+10;
struct node{
	int s;
	int l,r;
};
struct node tree[maxn*10];
int a[maxn];
int max(int x,int y){
	return x>y?x:y;
}
int create_tree(int h,int x,int y){
	tree[h].l=x;tree[h].r=y;
	int mid=(x+y)/2;
	if(x==y){
		tree[h].s=a[x];
		return tree[h].s;
	}
	int x1=create_tree(h*2,x,mid);
	int x2=create_tree(h*2+1,mid+1,y);
	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=update(2*h,x);
	int 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;
}


  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值