Cod sursa(job #388845)

Utilizator alexandru92alexandru alexandru92 Data 31 ianuarie 2010 09:25:03
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2 kb
//      bit_indexed_tree.cpp
//      
//      Copyright 2010 SpeeDemon <virtualdemon@ubuntu>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.
#include <fstream>
#define inf 0x3f3f3f3f

/*
 * 
 */
using namespace std;
int N;
int *v, *tree;
inline int max( int x, int y )
{
	if( v[x] > v[y] )
		return x;
	return y;
}
int Query1( int x, int y )
{
	int maxim=0;
	for( ; x <= y; x+=( x & -x ) )
		maxim=max( maxim, x );
	return maxim;
}
int Query2( int x, int y )
{
	int maxim=0, left=x, right=y, middle, max1, max2;
	while( left < right )
	{
		middle=left+(right-left)/2;
		max1=Query1( left, middle );
		max2=Query1( middle+1, right );
		maxim=max( maxim, max( max1, max2 ) );
		if( v[max1] <= v[max2] )
			left=middle+1;
		else right=middle-1;
	}
	return  maxim;
}
void Update( int x )
{
	for( int z=x; z <= N; z+=( z & -z ) )
		if( x == tree[z] )
		{
			tree[z]=max( x, Query2( z-( z & -z )+1, z-1 ) );
		}
		else tree[z]=max( tree[z], x );
}
int main()
{
	int M, i, x, y, z;
	ifstream in("arbint.in");
	in>>N>>M;
	v=new int[N+1];
	tree=new int[N+1];
	v[0]=-inf;
	for( i=1; i <= N; ++i )
	{
		in>>v[i];
		Update( i );
	}
	ofstream out("arbint.out");
	for( i=1; i <= M; ++i )
	{
		in>>x>>y>>z;
		if( 0 == x )
		   out<<v[Query2( y, z )]<<'\n';
		else v[y]=z, Update( y );
	}
	return 0;
}