HDU1754 I Hate It

参考资料: http://www.cppblog.com/MiYu/archive/2010/08/29/125199.html
此题的simple建立的树为:
HDU1754 I Hate It

AC代码:
#include <iostream>
using namespace std;

const int _N = 200005;

int a[_N << 2];

int max(int b, int c)
{
	return b > c ? b : c;
}
//跟新父节点的值
void pushUp(int rt)
{
	a[rt] = max(a[rt << 1], a[rt << 1 | 1]);
}
//建立树
void bulid(int l, int r, int rt)
{
	if(l == r)
	{
		cin>>a[rt];
		return;
	}
	int m = (l + r) >> 1;
	bulid(l, m, rt << 1);
	bulid(m + 1, r, rt << 1 | 1);
	pushUp(rt);
}
//跟新值
void update(int p, int v, int l, int r, int rt)
{
	if(l == r)
	{
		a[rt] = v;
		return;
	}
	int m = (l + r) >> 1;
	if(p <= m)
		update(p, v, l, m, rt << 1);
	else
		update(p, v, m + 1, r, rt << 1 | 1);
	pushUp(rt);
}
//查询最大值
int query(int L, int R, int l, int r, int rt)
{
	int v = 0;
	if(L <= l && r <= R)
		return a[rt];
	int m = (l + r) >> 1;
	if(L <= m)
		v = max(v, query(L, R, l, m, rt << 1));
	if(R > m) 
		v = max(v, query(L, R, m + 1, r, rt << 1 | 1));
	return v;
}
int main()
{
	int n, m;
	while(cin>>n>>m)
	{
		bulid(1, n, 1);
		for(int i = 0; i < m; i++)
		{
			char q;
			int b, c;
			cin>>q>>b>>c;
			if(q == 'Q')
				cout<<query(b, c, 1, n, 1)<<endl;
			else
				update(b, c, 1, n, 1);
		}
	}
	return 0;
}


你可能感兴趣的:(HDU)