Cod sursa(job #759427)

Utilizator Stefex09Stefan Teodorescu Stefex09 Data 18 iunie 2012 00:06:23
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include<cstdio>

using namespace std;

int AINT[1 << 18];

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

void update(int nod, int start, int stop, int poz, int val)
{
	int med;
	
	if(start == stop){
		AINT[nod] = val;
		return;
	}
	
	med = (start + stop) >> 1;
	
	if(poz <= med)
		update(nod << 1, start, med, poz, val);
	else
		update((nod << 1) + 1, med + 1, stop, poz, val);
	
	AINT[nod] = maxim(AINT[nod << 1], AINT[(nod << 1) + 1]);
}

int query(int nod, int start, int stop, int st, int dr)
{
	int med, sol1 = 0, sol2 = 0;
	
	if(st <= start && stop <= dr)
		return AINT[nod];
	
	med = (start + stop) >> 1;
	
	if(st <= med)
		sol1 = query(nod << 1, start, med, st, dr);
	if(med < dr)
		sol2 = query((nod << 1) + 1, med + 1, stop, st, dr);
	
	return maxim(sol1, sol2);
}

int main()
{
	freopen("arbint.in", "r", stdin);
	freopen("arbint.out", "w", stdout);
	
	int N, M, i, x, y, tip;
	
	//in >> N >> M;
	scanf("%d %d", &N, &M);
	
	for(i = 1; i <= N; ++i){
		//in >> x;
		scanf("%d", &x);
		update(1, 1, N, i, x);
	}
	
	while(M--){
		//in >> tip >> x >> y;
		scanf("%d %d %d", &tip, &x, &y);
		
		if(tip)
			update(1, 1, N, x, y);
		else
			//out << query(1, 1, N, x, y) << "\n";
			printf("%d\n", query(1, 1, N, x, y));
	}
	
	return 0;
}