Hdu-1754 I hate it【线段树(单点更新)】

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

解题思路:

依然线段树水题。。。。。

代码如下:

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

#define lson l, m, root << 1
#define rson m + 1, r, root << 1 | 1
const int N = 200010;
int segtree[N << 1 + 10]; //开2倍

void PushUp(int root) //节点保存区间最大值
{
	segtree[root] = max(segtree[root << 1], segtree[root << 1 | 1]);
}

void Build_Tree(int l, int r, int root)
{
	if(l == r)
	{
		scanf("%d", &segtree[root]);
		return ;
	}
	int m = (l + r) >> 1;
	Build_Tree(lson);
	Build_Tree(rson);
	PushUp(root);
}

void Update(int pos, int data, int l, int r, int root)
{
	if(l == r)
	{
		segtree[root] = data;
		return ;
	}
	int m = (l + r) >> 1;
	if(pos <= m)
		Update(pos, data, lson);
	else
		Update(pos, data, rson);
	PushUp(root);
}

int Query(int L, int R, int l, int r, int root)
{
	int sum = -9999999;
	if(L <= l && r <= R)
		return segtree[root];
	int m = (l + r) >> 1;
	if(L <= m)
		sum = max(sum, Query(L, R, lson));
	if(R > m)
		sum = max(sum, Query(L, R, rson));
	return sum;
}

int main()
{
	int num, que;
	char ope;
	int a, b;
	while(scanf("%d%d", &num, &que) != EOF)
	{
		Build_Tree(1, num, 1);
		for(int i = 0; i < que; ++i)
		{
			scanf("%*c%c %d %d", &ope, &a, &b);	
			if(ope == 'U')
				Update(a, b, 1, num, 1);
			else
				printf("%d\n", Query(a, b, 1, num, 1));
		}
	}
	return 0;
}

你可能感兴趣的:(Hdu-1754 I hate it【线段树(单点更新)】)