Cod sursa(job #494714)

Utilizator ZethpixZethpix Zethpix Data 22 octombrie 2010 18:07:17
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <stdio.h>

int T[250002],i,c,x,y,N,M;

void update(int nod,int L,int R,int pos,int v)
{
	int M;
	if(L==pos&&R==pos) T[nod]=v;
	else
	{
		M=(L+R)/2;
		if(pos<=M) update(2*nod,L,M,pos,v);
		else
		update(2*nod+1,M+1,R,pos,v);
		if(T[2*nod+1]>T[2*nod]) T[nod]=T[2*nod+1];
		else T[nod]=T[2*nod];
	}
}

int max(int nod,int L,int R,int a,int b)
{
	int M,sol=0,x=0;
	if(a<=L&&R<=b) sol=T[nod];
	else
	{
		M=(L+R)/2;
		if(a<=M) x=max(2*nod,L,M,a,b);
		if(x>sol) sol=x;
		if(M<b) x=max(2*nod+1,M+1,R,a,b);
		if(x>sol) sol=x;
	}
	return sol;
}

int main()
{
	freopen("arbint.in","r",stdin);
	freopen("arbint.out","w",stdout);

	scanf("%d%d",&N,&M);
	for(i=1;i<=N;i++)
	{
		scanf("%d",&x);
		update(1,1,N,i,x);
	}
	for(i=1;i<=M;i++)
	{
		scanf("%d%d%d",&c,&x,&y);
		if(c==0) printf("%d\n",max(1,1,N,x,y));
		else update(1,1,N,x,y);
	}

	return 0;
}