HDU1754线段树单点更新

原题 http://acm.hdu.edu.cn/showproblem.php?pid=1754
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <vector>
#include <math.h>
using namespace std;
#define lson l,m,rt<<1//左儿子
#define rson m+1,r,rt<<1|1//右儿子
const int maxn = 222222;
int MAX[maxn<<2];

int max(int a,int b){
	if(a >= b)
		return a;
	else
		return b;
}

void pushup(int rt){
	MAX[rt] = max(MAX[rt<<1],MAX[rt<<1|1]);
}

void build(int l,int r,int rt){//建立线段树
	if(l == r){//输入
		scanf("%d",&MAX[rt]);
		return ;
	}
	int m = (l+r)>>1;
	build(lson);
	build(rson);
	pushup(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){//这步举个简单的例子,如果说我要找的区间是2到4,m等于3
		ret = max(ret,query(L,R,lson));
	}
	if(R > m){
		ret = max(ret,query(L,R,rson));
	}

	return ret;
}

void update(int a,int b,int l,int r,int rt){
	if(l == r){
		MAX[rt] = b;
		return ;
	}
	int m = (l+r)>>1;
	if(a <= m){//和要找的那个点进行比较。从而确定是更新左区间还是右区间
		update(a,b,lson);
	}
	else{
		update(a,b,rson);
	}
	pushup(rt);
}

int main(){
	int n,m;
	char op[2];
	int a,b;

	while(~scanf("%d%d",&n,&m)){
		build(1,n,1);
		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;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值