Cod sursa(job #545058)

Utilizator dornescuvladVlad Eugen Dornescu dornescuvlad Data 2 martie 2011 17:23:33
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <iostream>
#include <fstream>

using namespace std;

const char iname[] = "arbint.in";
const char oname[] = "arbint.out";

ifstream fin(iname);
ofstream fout(oname);

int n, m, i, val, poz, x, y, midd, op, mx, arb[400000];

void update(int nod, int lf, int rg)
{
	if(lf >= rg)
	{
		arb[nod] = val;
		return;
	}

	midd = (lf + rg) >> 1;
	if(poz <= midd)
		update(2 * nod, lf, midd);
	else
		update(2 * nod + 1, midd + 1, rg);
	arb[nod] = max(arb[nod * 2], arb[nod * 2 + 1]);
}

void query(int nod, int lf, int rg)
{
	if(x <= lf && rg <= y)
	{
		if(arb[nod] > mx)
			mx = arb[nod];
		return;
	}
	midd = (lf + rg) >> 1;
	if(x <= midd)
		query(nod * 2, lf, midd);
	if(midd < y)
		query(nod * 2 + 1, midd + 1, rg);
}

int main()
{	
	fin >> n >> m;
	for(i = 1; i <= n; i ++)
	{
		fin >> val;
		poz = i;
		update(1, 1, n);
	}
	
	for(i = 1; i <= m; i ++)
	{	
	
		fin >> op;
		if(op == 0)
		{	
			mx = -1;
			fin >> x >> y;
			query(1, 1, n);
			fout << mx << "\n";
		}
		else
		{	
			mx = -1;
			fin >> poz >> val;
			update(1, 1, n);
		}
	}
	return 0;
}