HDU 1754 I Hate It(单点更新)

转载请注明出处忆梦http://blog.csdn.net/yimeng2013/article/details/17189055


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754

线段树功能: update:单点替换 query:区间求最值 

跟着NotOnlySuccess大牛学线段树


#include<cstdio>
#include<algorithm>
using namespace std;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define maxn 222222
int Max[maxn<<2];

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);
}

void update(int pos, int p, int l, int r, int rt)
{
	if(l == r)
	{
		Max[rt] = p;
		return ;
	}

	int m = (l + r) >> 1;
	if(pos <= m)
		update(pos, p, lson);
	else
		update(pos, p, 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 = -1;
	if(L <= m)
		ret = max(query(L,R,lson), ret);
	if(R > m)
		ret = max(query(L,R,rson), ret);
	return ret;
}
int main ()
{
	int n, m;
	while(scanf("%d %d", &n, &m) !=EOF)
	{
		build(1, n, 1);

		char ch[2];
		int a, b;
		for(int i = 1; i <= m; i++)
		{
			scanf("%s", ch);
			scanf("%d %d", &a, &b);
			if(ch[0] == 'Q')
			{
				printf("%d\n", query(a, b, 1, n, 1));
			}
			if(ch[0] == 'U')
			{
				update(a, b, 1, n, 1);
			}
		}
	}
	return 0;
}


你可能感兴趣的:(HDU 1754 I Hate It(单点更新))