1. 程式人生 > 實用技巧 >hdu 1754

hdu 1754

線段樹入門題,找下手感

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;

const int maxn= 2e5+5;
const int maxm= 5e3+5;
const int maxl= maxn<<2;

int tree[maxl], ar[maxn];
int a, b;

inline LChild(int x)
{
	return x<<1;
}
inline RChild(int x)
{
	return x<<1|1;
}
void Build(int rt, int l, int r)
{
	if (l== r){
		tree[rt]= ar[l];
		return;
	}
	int lc= LChild(rt), rc= RChild(rt);
	int mid= (l+r)>>1;
	Build(lc, l, mid);
	Build(rc, mid+1, r);

	tree[rt]= tree[lc] > tree[rc] ? tree[lc] : tree[rc];
}
void Update(int rt, int l, int r)
{
	if (l== r){
		tree[rt]= ar[l];
		return;
	}

	int lc= LChild(rt), rc= RChild(rt);
	int md= (l+r)>>1;
	if (l<= a && a<= md){
		Update(lc, l, md);
	}
	if (md+1<= a && a<= r){
		Update(rc, md+1, r);
	}

	tree[rt]= tree[lc] > tree[rc] ? tree[lc] : tree[rc];
}
int Query(int rt, int l, int r)
{
	if (a<= l && r<= b){
		return tree[rt];
	}

	int md= (l+r)>>1, ans= -1, t= -1;
	if (a<= md){
		t= Query(LChild(rt), l, md);
		if (t> ans){
			ans= t;
		}
	}
	if (md+1<= b){
		t= Query(RChild(rt), md+1, r);
		if (t> ans){
			ans= t;
		}
	}

	return ans;
}

int main(int argc, char const *argv[])
{
	int n, m;
	char op;

	while (EOF!= scanf("%d %d", &n, &m)){
		for(int i= 1; i<= n; ++i){
			scanf("%d", ar+i);
		}
		memset(tree, -1, sizeof(tree));
		Build(1, 1, n);

		while (m--){
			scanf(" %c", &op);
			if ('U'== op){
				scanf("%d %d", &a, &b);
				ar[a]= b;
				Update(1, 1, n);
			}
			else{
				scanf("%d %d", &a, &b);
				cout<<Query(1, 1, n)<<endl;
			}
		}
	}

	return 0;
}