Cod sursa(job #989641)

Utilizator Cosmin1490Balan Radu Cosmin Cosmin1490 Data 26 august 2013 02:09:46
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 2.75 kb
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;
  
const string file = "arbint";
  
const string infile = file + ".in";
const string outfile = file + ".out";



template <class T>
class SegmentTree
{
public:
	SegmentTree(int low, int hi);
	void Insert(int low, int hi, const T& data);
	T Query(int low, int hi);
protected:
private:

	void Insert(int nod, int st, int dr, int low, int hi, const T& data);
	T Query(int nod, int st, int dr, int low, int hi);

	int _hi;
	int _low;
	vector<T> _data;
};

template<class T>
SegmentTree<T>::SegmentTree(int low, int hi)
{
	_hi = hi;
	_low = low;
	int count = hi - low + 1;
	int c = 1;
	while(c < count) c <<= 1;
	c <<= 1;
	_data.resize(c + 1);
}

template<class T>
void SegmentTree<T>::Insert(int low, int hi, const T& data)
{
	Insert(1, _low, _hi, low, hi, data);
}

template<class T>
T SegmentTree<T>::Query(int low, int hi)
{
	return Query(1, _low, _hi, low, hi);
}

template<class T>
void SegmentTree<T>::Insert(int nod, int st, int dr, int low, int hi, const T& data)
{
	if(low <= st && dr <= hi)
	{
		_data[nod] = data;
	}
	else
	{
		int mid = (dr + st) >> 1;
		if(low <= mid)
			Insert(nod << 1 , st, mid, low, hi, data);
		if(mid < hi)
			Insert((nod << 1) + 1, mid + 1, dr, low, hi, data);
		_data[nod] = max(_data[nod << 1], _data[(nod << 1) + 1]);
	}
}

template<class T>
T SegmentTree<T>::Query(int nod, int st, int dr, int low, int hi)
{
	if(low <= st && dr <= hi)
	{
		return _data[nod];
	}
	else
	{
		int mid = (dr + st) >> 1;
		int result = 0;
		if(low <= mid)
			result = max(result, Query(nod << 1, st, mid, low, hi));
		if(mid < hi)
			result = max(result, Query((nod << 1) + 1, mid + 1, dr, low, hi));
		return result;
	}
}

char buffer[40000000];

int N, M;
int main()
{
	fstream fin(infile.c_str(), ios::in);
	fstream fout(outfile.c_str(), ios::out);

	fin.rdbuf()->pubsetbuf(buffer, sizeof(buffer));

	fin >> N >> M;

	SegmentTree<int> tree(1, N);

	for(int i = 0; i < N; i++)
	{
		int x;
		fin >> x;
		tree.Insert(i+1, i+1, x);
	}
	
	for(int i = 0; i < M; i++)
	{
		int op, a, b;
		fin >> op >> a >> b;
		if(op == 0)
		{
			fout << tree.Query(a, b) << "\n";
		}
		else if(op == 1)
		{
			tree.Insert(a, a, b);
		}
	}

	fout.close();
	fin.close();


}