Cod sursa(job #2740073)

Utilizator KillHorizon23Orban Robert KillHorizon23 Data 11 aprilie 2021 10:46:32
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
typedef long long ll;
int aint[300005], N = 1;
void update(int a, int b)
{
	a = N + a - 1;
	aint[a] = b;
	while (a > 0)
	{
		int t = a / 2;
		aint[t] = max(aint[2 * t], aint[2 * t + 1]);
		a = t;
	}
}
int query(int nod, int a, int b, int x, int y)
{
	int mij;
	if (a == x && b == y)
		return aint[nod];
	else
	{
		mij = (x + y) / 2;
		if (b <= mij)
			return query(2 * nod, a, b, x, mij);
		else if (mij + 1 <= a)
			return query(2 * nod + 1, a, b, mij + 1, y);
		else
			return max(query(2 * nod, a, mij, x, mij), query(2 * nod + 1, mij + 1, b, mij + 1, y));
	}
}
int main()
{
	int n, m;
	fin >> n >> m;
	while (N < n)
		N *= 2;
	for (int i = N; i < N + n; ++i)
		fin >> aint[i];
	for (int i = N - 1; i >= 1; --i)
		aint[i] = max(aint[2 * i], aint[2 * i + 1]);
	while (m--)
	{
		int a, b, op;
		fin >> op >> a >> b;
		if (op)
			update(a, b);
		else fout << query(1, a, b, 1, N) << "\n";
	}
}