Cod sursa(job #906162)

Utilizator Impaler_009Mihai Nitu Impaler_009 Data 6 martie 2013 15:59:58
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <fstream>
using namespace std;
ifstream fin("arbint.in");
ofstream fout ("arbint.out");

int t[1<<18];

void update (int nod, int st, int dr, int i, int x)
{
	if (st<dr)
	{
		int m=(st+dr)/2;
		if (i<=m)
		update (nod<<1,st,m,i,x);
		else
		update ((nod<<1)+1,m+1,dr,i,x);
	}
	else  {t[nod]=x; return;}
	if (t[nod<<1]>t[(nod<<1)+1]) t[nod]=t[nod<<1];
	else t[nod]=t[(nod<<1)+1];
}

int query (int nod, int st, int dr, int a, int b)
{
	if (st<a||b<dr)
	{
		int m=(st+dr)/2;
		int x=0,y=0;
		if (a<=m) x=query(nod<<1,st,m,a,b);
		if (b>m) y=query ((nod<<1)+1,m+1,dr,a,b);
		if (x<y) x=y;
		return x;	
	}
	else  return t[nod];
}

int main()
{
	int n,m,i,a,b,x; bool op;
	fin>>n>>m;
	for (i=1;i<=n;i++) {fin>>x; update (1,1,n,i,x);}
	for (i=1;i<=m;i++)
	{
		fin>>op>>a>>b;
		if (op==0) 
		{x=query(1,1,n,a,b); 
		fout<<x<<"\n";}
		else update (1,1,n,a,b);
	}
    return 0;
}