HDU_1754_I Hate It

#include<iostream>
#include<sstream>    
#include<string>    
#include<vector>    
#include<list>    
#include<set>    
#include<map>    
#include<stack>    
#include<queue>    
#include<algorithm>    
#include<cmath>    
#pragma warning(disable:4996)    
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
template<typename ElemType>
class Interval
{
public:
	ElemType first, second, weight;
	Interval()
	{
		first = second = weight = 0;
	}
	Interval(const ElemType &f, const ElemType &s)
	{
		first = f;
		second = s;
		weight = 0;
	}
	ElemType mid()
	{
		return first + (length() - 1) / 2;
	}
	ElemType length()
	{
		return second - first + 1;
	}
};
template<typename ElemType>
class SegTree
{
private:
	vector<Interval<ElemType> >tree;
public:
	SegTree() {}
	SegTree(const ElemType &n)
	{
		tree.resize(4 * n);
	}
	void resize(const ElemType &n)
	{
		tree.resize(n);
	}
	void update_parent(const ElemType &parent)//父区间值为子区间的最大值
	{
		tree[parent].weight = std::max(tree[parent * 2].weight, tree[parent * 2 + 1].weight);
	}
	void update_child(const ElemType &parent)//把父区间的延时标记下放给两个给两个子区间,然后父区间的延时标记清零
	{
		/*
		if (2 * parent < tree.size())
		{
			tree[2 * parent].weight = std::max(tree[2 * parent].weight, tree[parent].weight);
		}
		if (2 * parent + 1 < tree.size())
		{
			tree[2 * parent + 1].weight = std::max(tree[2 * parent + 1].weight, tree[parent].weight);
		}
		*/
	}
	//以数组形式建立线段树,当数据离散化之后,调用build(min,max,1)
	void build(Interval<ElemType>interval, ElemType parent, ElemType &size)
	{
		tree[parent] = interval;
		if (interval.first == interval.second)
		{
			scanf("%d", &tree[parent].weight);
			size = std::max(size, parent);
			return;//当为元线段时停止递归
		}
		ElemType mid = (interval.first + interval.second) / 2;
		build({ interval.first,mid }, 2 * parent, size);
		build({ mid + 1,interval.second }, 2 * parent + 1, size);
		update_parent(parent);
	}
	void insert(Interval<ElemType>interval, ElemType parent, const ElemType &weight)
	{
		if (tree[parent].first == interval.first&&interval.second == tree[parent].second)
		{
			tree[parent].weight = weight;
			return;
		}
		update_child(parent);
		ElemType mid = tree[parent].mid();
		if (interval.second <= mid)
		{
			insert(interval, parent * 2, weight);
		}
		else if (interval.first > mid)
		{
			insert(interval, parent * 2 + 1, weight);
		}
		else
		{
			insert({ interval.first,mid }, parent * 2, weight);
			insert({ mid + 1,interval.second }, parent * 2 + 1, weight);
		}
		update_parent(parent);
	}
	//查询区间之和(假设要求的区间包含于最大区间)
	ElemType getRank(pair<ElemType, ElemType>interval, ElemType parent)
	{
		//如果要查询的区间==当前区间
		if (tree[parent].first == interval.first&&interval.second == tree[parent].second)
		{
			//这时候应该从根据该区间的祖先区间的延时标记更新当前区间
			return tree[parent].weight;
		}
		update_child(parent);
		ElemType mid = tree[parent].mid();
		if (interval.second <= mid)
		{
			return getRank(interval, parent * 2);
		}
		else if (interval.first > mid)
		{
			return getRank(interval, parent * 2 + 1);
		}
		return std::max(getRank({ interval.first,mid }, parent * 2) ,getRank({ mid + 1,interval.second }, parent * 2 + 1));
	}
};
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt","w",stdout); 
	int n, m;
	while (scanf("%d%d", &n, &m) != EOF)
	{
		SegTree<int> segtree(n);
		int size = 0;
		segtree.build({1,n}, 1,size);
		segtree.resize(size + 1);
		while (m--)
		{
			char order; cin >> order;
			switch (order)
			{
			case 'Q':
			{
				int left, right;
				cin >> left >> right;
				cout << segtree.getRank({ left,right },1)<<endl;
			}break;
			case 'U': 
			{
				int i, rank;
				cin >> i >> rank;
				segtree.insert({ i,i }, 1, rank);
			}break;
			default:break;
			}
		}
	}
	return 0;
}

你可能感兴趣的:(tree,HDU,segment)