Cod sursa(job #595142)

Utilizator veleanduAlex Velea veleandu Data 11 iunie 2011 12:12:30
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 kb
#include<fstream>
using namespace std;
long Arb[200050];
long n,m;
long maxim,i,j,lastN;
long a,b;
long val,op;
void fnd ( long st, long dr, long it , long  &nod)
{
	long m = (dr-st)/2+st;
	if ( ( st == it ) && ( dr==it ) )
		return ;
	nod*=2;
	if ( m>= it )
		fnd ( st , m , it, nod );
	else
	{
		nod++;
		fnd ( m+1 , dr , it, nod );
	}
}
void update ( long nod ) 
{
	if ( nod%2==0 )
	{
		if( Arb[nod/2]!= max ( Arb[nod],Arb[nod+1]) )
		{
			Arb[nod/2] = max ( Arb[nod],Arb[nod+1]);
			update( nod/2 );
		}
		return ;
	}
	if( Arb[nod/2]!= max ( Arb[nod],Arb[nod-1]) )
	{
		Arb[nod/2] = max ( Arb[nod],Arb[nod-1]);
		update( nod/2 );
	}
}
void maxint ( long st, long dr , long cst, long cdr, long nod)
{
	if ( ( st>=cst ) && ( dr<=cdr ) )
	{
		if ( maxim < Arb[nod] )
			maxim = Arb[nod];
		return ;
	}
	if ( !( ( ( st<=cst ) && ( cst<=dr ) ) || ( ( st<=cdr ) && ( cdr<=dr ) ) ) )
		return ;
	long m = (dr-st)/2+st;
	maxint ( st , m , cst , cdr , nod*2 );
	maxint ( m+1 , dr , cst , cdr , nod*2 +1 );	
}
int main()
{
	ifstream in("arbint.in");
	ofstream out("arbint.out");
	in>>n>>m;
	for ( i=1; i<=n; ++i )
	{
		in>>val;	
		lastN=1;
		fnd(1,n,i,lastN);
		Arb[lastN]=val;
		update(lastN);
	}	
	for ( i=1; i<=m; ++i )
	{
		in>>op>>a>>b;
		if ( op==0 ) 
		{
			maxim=-1;
			maxint(1,n,a,b,1);
			out<<maxim<<"\n";
		}
		else{
			lastN=1;
			fnd ( 1,n,a,lastN);
			Arb[lastN]=b;
			update ( lastN );
		}
	}
	in.close();
	out.close();
	return 0;
}