现在的位置: 首页 > 综合 > 正文

hdu 1754 (区间最大线段树基础题)

2019年03月07日 ⁄ 综合 ⁄ 共 830字 ⁄ 字号 评论关闭
#include<cstdio>

#define M 200005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

int Max[M<<2];

void Pushdata(int rt){

	Max[rt] = Max[rt<<1] >Max[rt<<1|1] ?Max[rt<<1]:Max[rt<<1|1];

}

void Bulid(int l,int r,int rt){

	if( l==r ){
		scanf("%d",&Max[rt]);
		return ;
	}

	int m= ( l+r ) >> 1;

	Bulid(lson);
	Bulid(rson);

	Pushdata(rt);

}

void Updata(int p,int c,int l,int r,int rt){
	
	if( l == r ){
		Max[rt] = c;
		return ;
	}
	int m = ( l +r )>>1 ;

	if(p<=m)
		Updata(p,c,lson);
	else
		Updata(p,c,rson);

	Pushdata(rt);
}

int Query(int L,int R,int l,int r,int rt){
	
	if( L <= l && r <= R)
		return Max[rt];
	int max=0,temp;
	int m = ( l + r) >>1;
	if( L<= m){
		temp = Query(L,R,lson);
		max=temp>max?temp:max;
	}
	if(R > m){
		temp = Query(L,R,rson);
		max=temp>max?temp:max;
	}
	return max;
	
}

int main(){

	int n,m,a,b;
	char op[10];

	while(~scanf("%d %d",&n,&m)){
	Bulid(1,n,1);
	while(m--){

		scanf("%s %d %d",op,&a,&b);

		if(op[0]=='U')
			Updata(a,b,1,n,1);
		else if(op[0] == 'Q')
			printf("%d\n",Query(a,b,1,n,1));
	}
	}
	return 0;

}

抱歉!评论已关闭.