Cod sursa(job #875128)

Utilizator SebiSebiPirtoaca George Sebastian SebiSebi Data 9 februarie 2013 18:52:18
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.39 kb
#include<stdio.h>
using namespace std;

#define NMAX 400001
#define DIM 10001

int v[NMAX],val,poz,a,b,cp;
char buff[DIM];

inline void citeste(int &numar)
{
	while(buff[cp]<'0' || buff[cp]>'9')
		if(++cp==DIM) {
			fread(buff,DIM,1,stdin);
			cp=0;
		}
	numar=0;
	while(buff[cp]>='0' && buff[cp]<='9') {
		numar=numar*10+buff[cp]-48;
			if(++cp==DIM) {
			fread(buff,DIM,1,stdin);
			cp=0;
		}
	}
}

inline int maxim(int a, int b)
{
	if(a>=b)
		return a;
	return b;
}

inline void update(int nod, int st, int dr)
{
	int mij;
	if(st==dr) {
		v[nod]=val;
		return ;
	}
	mij=(st+dr)/2;
	if(poz<=mij)
		update(nod*2,st,mij);
	else update(nod*2+1,mij+1,dr);
	v[nod]=maxim(v[2*nod],v[2*nod+1]);
}

inline void query(int nod, int st, int dr)
{
	int mij;
	if((a<=st)&&(dr<=b)) {
		if(v[nod]>val)
			val=v[nod];
		return ;
	}
	mij=(st+dr)/2;
	if(a<=mij)
		query(nod*2,st,mij);
	if(mij<b)
		query(nod*2+1,mij+1,dr);
}

int main ()
{
	int n,m,i,opt;
	freopen("arbint.in","r",stdin);
	freopen("arbint.out","w",stdout);
	citeste(n);
	citeste(m);
	for(i=1;i<=n;i++) {
		citeste(val);
		poz=i;
		update(1,1,n);
	}
	for(i=1;i<=m;i++) {
		citeste(opt);
		citeste(a);
		citeste(b);
		if(opt==0) {
			val=0;
			query(1,1,n);
			printf("%d\n",val);
		}
		else {
			poz=a;
			val=b;
			update(1,1,n);
		}
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}