Cod sursa(job #385556)

Utilizator Alexa_ioana_14Antoche Ioana Alexandra Alexa_ioana_14 Data 22 ianuarie 2010 22:43:52
Problema Arbori de intervale Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include<cstdio>
#define N 1001
#define maxx(a,b) ((a>b)? (a):(b))
#define M 1<<10
#define INF 1<<30
int v[N],a[M],q,x,y,n,m;
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[nod*2+1]);
}
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(2*nod, l,m,poz,val);
	else
	if (poz>m) update(2*nod+1,m+1,r,poz,val);
	a[nod]=maxx(a[nod*2],a[nod*2+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(2*nod,l,m,x,y);
	if (y>m)
	i2=maxim(2*nod+1,m+1,r,x,y);
	return (i1<i2)?(i2):(i1);
}
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;
}