Cod sursa(job #145720)

Utilizator raduzerRadu Zernoveanu raduzer Data 29 februarie 2008 11:31:26
Problema Arbori de intervale Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <stdio.h>

int n,m,d[131072],c,x,y,p,vp,max;

void find(int p, int a, int b)
{
	int v=(a+b)/2;
	if (a>=x && b<=y)
	{
		if (max<d[p]) max=d[p];
		return;
	}
	if (x<=v) find(p*2,a,v);
	if (y>v) find(p*2+1,v+1,b);
}

void update(int p)
{
	int max;
	max=d[p*2];
	if (max<d[p*2+1]) max=d[p*2+1];
	d[p]=max;
	if (p>1) update(p/2);
}

int main()
{
	freopen("arbint.in","r",stdin);
	freopen("arbint.out","w",stdout);
	scanf("%d%d",&n,&m);
	int i,j;
	p=0;
	vp=1;
	while (vp<n) vp=1<<(++p);
	for (i=vp; i<vp+n; ++i) 
		scanf("%d",&d[i]);
	for (i=1; i<p; ++i)
		for (j=1<<i; j<1<<(i+1); ++j)
		{
			max=d[j*2];
			if (d[j*2+1]>max) max=d[j*2+1];
			d[j]=max;
		}
	for (i=1; i<=m; ++i)
	{
		scanf("%d",&c);
		if (c==0)
		{
			scanf("%d%d",&x,&y);
			max=0;
			find(1,1,vp);
			printf("%d\n",max);
		}
		if (c==1)
		{
			scanf("%d%d",&x,&y);
			d[vp+x-1]=y;
			update((vp+x-1)/2);
		}
	}
	return 0;
}