Cod sursa(job #545011)

Utilizator dornescuvladVlad Eugen Dornescu dornescuvlad Data 2 martie 2011 16:39:32
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 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]);
}

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

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)
		{
			fin >> x >> y;
			fout << query(1, 1, n) << "\n";
		}
		else
		{	
			mx = -1;
			fin >> poz >> val;
			update(1, 1, n);
		}
	}
	return 0;
}