hdu_1754

當op爲'Q'的時候,表示這是一條詢問操作,它詢問ID從A到B(包括A,B)的學生當中,成績最高的是多少。 

當op爲'U'的時候,表示這是一條更新操作,要求把ID爲A的學生的成績更改爲B。


#include<cstdio>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 200000 + 10;
int Max[maxn<<2];
int num[maxn];

int max(int a,int b){
	return a > b ? a : b;
}

inline void setMax(int rt){
	Max[rt] = max(Max[rt<<1],Max[rt<<1|1]);
}

void buildTree(int l,int r,int rt){
	if(l == r){
		Max[rt] = num[l];
		return;
	}
	int m = (l+r)>>1;
	buildTree(lson);
	buildTree(rson);
	setMax(rt);
}

void update(int a,int sco,int l,int r,int rt){
	if(l == r){
		Max[rt] = sco;
		return ;
	}
	int m = (l+r)>>1;
	if(a<=m)
		update(a,sco,lson);
	else
		update(a,sco,rson);
	setMax(rt);
}

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

int main(){
	int N,M;
	while(scanf("%d %d",&N,&M)!= EOF){
		for(int i = 1;i<=N;i++){
			scanf("%d",&num[i]);
		}
		buildTree(1,N,1);
		char op[2];
		int a,b;
		while(M--){
			scanf("%s %d %d",op,&a,&b);
				if(op[0] == 'Q')
					printf("%d\n",query(a,b,1,N,1));
				else
					update(a,b,1,N,1);
		}
	}
	return 0;
}

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章