Cod sursa(job #2189767)

Utilizator trifangrobertRobert Trifan trifangrobert Data 28 martie 2018 22:25:26
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.7 kb
#include <algorithm>
#include <fstream>

using namespace std;

const int DIM = 100010;
int n, q;
int v[DIM], aint[4 * DIM];
int op, x, y;
int ans = -1;

inline int LeftSon(const int &x)
{
	return 2 * x;
}

inline int RightSon(const int& x)
{
	return 2 * x + 1;
}

void Build(int node, int left, int right)
{
	if (left == right)
	{
		aint[node] = v[left];
		return;
	}
	int mid = (left + right) / 2;
	Build(LeftSon(node), left, mid);
	Build(RightSon(node), mid + 1, right);
	aint[node] = max(aint[LeftSon(node)], aint[RightSon(node)]);
}

void Update(int node, int left, int right, const int& pos, const int& val)
{
	if (left == right)
	{
		aint[node] = val;
		v[pos] = val;
		return;
	}
	int mid = (left + right) / 2;
	if (pos <= mid)
		Update(LeftSon(node), left, mid, pos, val);
	else
		Update(RightSon(node), mid + 1, right, pos, val);
	aint[node] = max(aint[LeftSon(node)], aint[RightSon(node)]);
}

void Query(int node, int left, int right, const int& LeftQuery, const int& RightQuery)
{
	if (LeftQuery <= left && right <= RightQuery)
	{
		ans = max(ans, aint[node]);
		return;
	}
	int mid = (left + right) / 2;
	if (LeftQuery <= mid)
		Query(LeftSon(node), left, mid, LeftQuery, RightQuery);
	if (RightQuery >= mid + 1)
		Query(RightSon(node), mid + 1, right, LeftQuery, RightQuery);
}

int main()
{
	ifstream fin("arbint.in");
	ofstream fout("arbint.out");
	fin >> n >> q;
	for (int i = 1;i <= n;++i)
		fin >> v[i];
	Build(1, 1, n);
	for (int i = 1;i <= n;++i)
	{
		fin >> op >> x >> y;
		if (op == 0)
		{
			ans = -1;
			Query(1, 1, n, x, y);
			fout << ans << "\n";
		}
		else
			Update(1, 1, n, x, y);
	}
	fin.close();
	fout.close();
	return 0;
}