Cod sursa(job #385546)

Utilizator Alexa_ioana_14Antoche Ioana Alexandra Alexa_ioana_14 Data 22 ianuarie 2010 22:11:41
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include<stdio.h>
#define N 100001
#define M 1<<30
#define INF 1000000
int v[N],n,m,a[M],x,y,q;
#define maxx(a,b) ((a>b)? (a):(b))
void update(int nod, int l,  int r, int poz, int val)
{
	if (l==r)
	{
		a[nod]=val;
		return;
	}
	int m=(l+r)>>1;
	if (poz<=m)
		update(nod*2,l,m,poz,val);
	if (poz>m)
		update(nod*2+1,m+1,r,poz,val);
	a[nod]=maxx(a[nod*2],a[nod*2+1]);
}
void build(int nod,int l,int r)
{
	if (l==r)
	{
		a[nod]=v[l];
		return;
	}
	int m=(l+r)>>1;
	build(nod*2,l,m);
	build(nod*2+1,m+1,r);
	a[nod]=maxx(a[nod*2],a[2*nod+1]);
}
int maxim(int nod, int l,int r,int x, int y)
{
	if (x<=l&&r<=y)
		return a[nod];
	int m=(l+r)>>1,i1=-INF,i2=-INF;
	if (x<=m)
		i1=maxim(nod*2,l,m,x,m);
	if (y>m)
		i2=maxim(nod*2+1,m+1,r,m+1,y);
	return maxx(i1,i2);
}
void citire()
{
	freopen("arbint.in","r",stdin);
	freopen("arbint.out","w",stdout);
	scanf("%d%d",&n,&m);
	for (int i=1; i<=n; ++i)
		scanf("%d",&v[i]);
        build(1,1,n);
	while (m--)
	{
		scanf("%d%d%d",&q,&x,&y);
		if (q)
			update(1,1,n,x,y);
		else
			printf("%d\n",maxim(1,1,n,x,y));
	}

}
int main()
{
citire();
return 0;
}