Cod sursa(job #3205425)

Utilizator AndreasBossGamerBaragau Andreas AndreasBossGamer Data 19 februarie 2024 16:18:52
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>

using namespace std;

ifstream cin("arbint.in");
ofstream cout("arbint.out");

int n, q;
int arbore[400000];

void build(int nod, int st, int dr)
{
	int mid = (st + dr) / 2;
	if (st == dr)
		cin >> arbore[st];
	else 
	{
		build(2 * nod, st, mid);
		build(2 * nod + 1, mid + 1, dr);
		arbore[nod] = max(arbore[2 * nod], arbore[2 * nod + 1]);
	}
}
void update(int nod, int st, int dr, int pos, int valoare)
{
	if (st == dr)
	{
		arbore[nod] = valoare;
	}
	else 
	{
		int mid = (st + dr) / 2;
		if (mid > pos)
		{
			update(2 * nod, st, mid, pos, valoare);
		}
		else
		{
			update(2 * nod + 1, mid + 1, dr, pos, valoare);	
		}
		arbore[nod] = max(arbore[2 * nod], arbore[2 * nod + 1]);
	}
}
int sol = -1;
void query(int nod, int st, int dr, int a, int b)
{
	if (a <= st && dr <= b)
	{
		sol = max(sol, arbore[nod]);
	}
	else 
	{
		int mid = (st + dr) / 2;
		if (st <= mid)
		{
			query(2 * nod, st, mid, a, b);
		}
		if (mid <= dr)
		{
			query(2 * nod + 1, mid + 1, dr, a, b);
		}
	}
}


int main()
{
	cin >> n >> q;
	build(1, 1, n);
	for (int i = 0; i < q; i++) {
		int caz, x, y;
		cin >> caz >> x >> y;
		if (caz == 0)
		{
			sol = 0;
			query(1, 1, n, x, y);
			cout << sol << '\n';
		}
		else
		{
			update(1, 1, n, x, y);
		}
	}
}