hdu1754 简单的线段树单点更新

很水的一道线段树,就不多说了

题意:

很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。


就是普通的模板线段树,很简单,只需要单点更新就行。

#include<cstdio>
#include<iostream>
using namespace std;

int a[200000<<2];


void build(int l,int r,int root){
	if(l==r){
		scanf("%d",&a[root]);
		return;
	}
	int mid=(l+r)>>1;
	build(l,mid,root<<1);
	build(mid+1,r,root<<1|1);
	a[root]=max(a[root<<1],a[root<<1|1]);
}

void update(int l,int r,int root,int p,int data){
	if(l==r){
		a[root]=data;
		return;
	}
	int mid=(l+r)>>1;
	if(p<=mid)update(l,mid,root<<1,p,data);
	else update(mid+1,r,root<<1|1,p,data);
	a[root]=max(a[root<<1],a[root<<1|1]);
}

int query(int l,int r,int root,int L,int R){
	if(L<=l&&r<=R){
		return a[root];
	}
	//printf("%d %d\n",l,r);
	int mid=(l+r)>>1;
	int res=0;
	if(L<=mid)
	res=max(res,query(l,mid,root<<1,L,R));
	if(R>mid)
	res=max(res,query(mid+1,r,root<<1|1,L,R));
	return res;
}


int main(){
	int n,m,l,r;
	char c[2];
	while(scanf("%d%d",&n,&m)!=EOF){
		build(1,n,1);
		while(m--){
			scanf("%s%d%d",c,&l,&r);
			if(c[0]=='Q'){
				printf("%d\n",query(1,n,1,l,r));
			}	
			else{
				update(1,n,1,l,r);
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值