Cod sursa(job #825575)

Utilizator alinaelenaFMI Colceag Alina alinaelena Data 29 noiembrie 2012 11:15:55
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include<cstdio>
using namespace std;
# define nmax 100001
int arb[4*nmax+1],maxim,op,poz,a,b,n,m,val;

int maxim2 (int a,int b)
{
	if (a<b)
		return b;
	return a;
}



void update (int nod,int st,int dr)
{
	int pivot;
	if (st==dr)
		{arb[nod]=val; return ;}
	
		pivot=(st+dr)/2;
		if (poz<=pivot) update(2*nod,st,pivot);
		else
			update(2*nod+1,pivot+1,dr);
		arb[nod]=maxim2(arb[2*nod],arb[2*nod+1]);
	
}


void query (int nod,int st,int dr)
{
	if ((a<=st)&&(dr<=b))
	{  if(maxim<arb[nod]) maxim=arb[nod]; return;}
	
	int pivot=(st+dr)/2;
	 if (a<=pivot) query(2*nod,st,pivot);
	 if (pivot<b) query(2*nod+1,pivot+1,dr);
	
}


int main()
{
	freopen("arbint.in","r",stdin);
	freopen("arbint.out","w",stdout);
	scanf("%d%d",&n,&m);
	int x,i;
	for (i=1;i<=n;++i)
		{
			scanf("%d",&x);
			poz=i; val=x;
			update(1,1,n);
	}
	
	for (i=1;i<=m;++i)
	{
		scanf("%d %d %d",&op,&a,&b);
		
		if (op==0)
		{
			maxim=-1;
			query(1,1,n);
			printf("%d\n",maxim);
		}
		else
		{
			poz=a; val=b;
			update(1,1,n);
		}
			
	}
	
}