Cod sursa(job #2392327)

Utilizator DMMDDicu Mihai DMMD Data 29 martie 2019 21:35:14
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include<fstream>
#include<algorithm>
#define NMAX 100000
using namespace std;
int n, m, sol;
int A[4 * NMAX + 5];
int x[NMAX + 5];
ifstream in("arbint.in");
ofstream out("arbint.out");

void buildArb(int st, int dr, int nod)
{
	if (st == dr)
	{
		A[nod] = x[st];
		return;
	}
	int mij = st + (dr - st) / 2;

	buildArb(st, mij, 2 * nod);
	buildArb(mij + 1, dr, 2 * nod + 1);
	A[nod] = max(A[2 * nod], A[2 * nod + 1]);

}

void updateArb(int st, int dr, int nod, int poz, int val)
{
	if (dr == st)
	{
		A[nod] = val;
		return;
	}
	int mij = st + (dr - st) / 2;

	if(poz <= mij)
		updateArb(st, mij, 2 * nod, poz, val);
	else
		updateArb(mij + 1, dr, 2 * nod + 1, poz, val);
	
	A[nod] = max(A[2 * nod], A[2 * nod + 1]);

}

void queryArb(int st, int dr, int nod, int a, int b)
{
	if (a <= st && dr <= b)
	{
		sol = max(sol, A[nod]);
		return;
	}
	int mij = st + (dr - st) / 2;

	if (a <= mij)
		queryArb(st, mij, 2 * nod, a, b);
	if(b > mij)
		queryArb(mij + 1, dr, 2 * nod + 1, a, b);
}

int a, b, op;
int main()
{
	in >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		in >> x[i];
	}

	buildArb(1, n, 1);

	for (int i = 1; i <= m; i++)
	{
		in >> op >> a >> b;
		if (op == 0)
		{
			sol = 0;
			queryArb(1, n, 1, a, b);
			out << sol << "\n";
		}
		else
		{
			updateArb(1, n, 1, a, b);
		}
	}

	return 0;
}