Cod sursa(job #2691647)

Utilizator cristia_razvanCristia Razvan cristia_razvan Data 29 decembrie 2020 15:21:02
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>
#define NMAX 100000
using namespace std;

ifstream fin("arbint.in");
ofstream fout("arbint.out");
int n, m,lung;
int aint[4 * NMAX + 10];

void update(int poz, int x)
{
	poz = poz + lung - 1;
	aint[poz] = x;
	while (poz >= 1)
	{
		if (poz & 1)poz--;
		aint[poz / 2] = max(aint[poz], aint[poz + 1]);
		poz /= 2;
	}
}

int query(int nod, int st, int dr, int x, int y)
{
	if (st == x && dr == y)
		return aint[nod];
	else {
		int mij = (st + dr) / 2;
		if (y <= mij)
			query(2 * nod, st, mij, x, y);
		else if (x >= mij + 1)
			query(2 * nod + 1, mij + 1, dr, x, y);
		else return max(query(2 * nod, st, mij, x, mij), query(2 * nod + 1, mij + 1, dr, mij + 1, y));
	}

}

int main()
{	
	int x, y, task;
	fin >> n >> m;
	lung = 1;
	while (lung < n)lung *= 2;
	for (int i = lung; i < lung + n; i++)
		fin >> aint[i];
	for (int i = lung - 1; i >= 1; i--)
		aint[i] = max(aint[2 * i], aint[2 * i + 1]);
	while (m--)
	{
		fin >> task >> x >> y;
		if (task == 1)
			update(x, y);
		else fout << query(1, 1, lung, x, y) << "\n";
	}
	return 0;
}