Cod sursa(job #3199126)

Utilizator dariustgameTimar Darius dariustgame Data 31 ianuarie 2024 20:21:57
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream fin("arbint.in");
ofstream fout("arbint.out");

int n, m;
int v[100005];
int ai[200005];

void build(int nod, int st, int dr)
{
	if (st == dr)
	{
		ai[nod] = v[st];
		return;
	}
	int mij = (st + dr) / 2;
	build(2 * nod, st, mij);
	build(2 * nod + 1, mij + 1, dr);
	ai[nod] = max(ai[2 * nod], ai[2 * nod + 1]);
}

void update(int nod, int st, int dr, int p, int x)
{
	if (st == dr)
	{
		ai[nod] = x;
		return;
	}
	int mij = (st + dr) / 2;
	if (p <= mij)
	{
		update(2 * nod, st, mij, p, x);
	}
	else
	{
		update(2 * nod + 1, mij + 1, dr, p, x);
	}
	ai[nod] = max(ai[2 * nod], ai[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, ai[nod]);
		return;
	}
	int mij = (st + dr) / 2;
	if (a <= mij)
	{
		query(2 * nod, st, mij, a, b);
	}
	if (b >= mij + 1)
	{
		query(2 * nod + 1, mij + 1, dr, a, b);
	}
}

int main()
{
	fin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		fin >> v[i];
	}
	build(1, 1, n);
	int x, y, z;
	for (int i = 1; i <= m; i++)
	{
		fin >> x >> y >> z;
		if (x == 0)
		{
			sol = -1;
			query(1, 1, n, y, z);
			fout << sol << '\n';
		}
		else
		{
			update(1, 1, n, y, z);
		}
	}
}