Cod sursa(job #396152)

Utilizator andrei.sfrentSfrent Andrei andrei.sfrent Data 14 februarie 2010 17:03:47
Problema Arbori de intervale Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 1.03 kb
#include <stdio.h>

#define DIMENSIUNE_ARBORE 262144

#define maxim(x, y) (x > y ? x : y)

int A[DIMENSIUNE_ARBORE], n, m;

void update(int nod, int li, int ls, int poz, int val)
{
	if(li == ls) A[nod] = val;
	else
	{
		int m = (li + ls) / 2;
		if(poz <= m) update(2 * nod, li, m, poz, val);
		else update(2 * nod + 1, m + 1, ls, poz, val);
		A[nod] = maxim(A[2 * nod], A[2 * nod +  1]);
	}
}

int querry(int nod, int li, int ls, int a, int b)
{
	if(a <= li && ls <= b) return A[nod];
	else
	{
		int q1 = -1, q2 = -1, m;
		m = (li + ls) / 2;
		if(a <= m) q1 = querry(2 * nod, li, m, a, b);
		if(b > m) q2 = querry(2 * nod + 1, m + 1, ls, a, b);
		return maxim(q1, q2);
	}
}

int main()
{
	freopen("arbint.in", "r", stdin);
	freopen("arbint.out", "w", stdout);

	int i, op, x, y;

	scanf("%d%d", &n, &m);
	
	for(i = 1; i <= n; ++i)
	{
		scanf("%d", &x);
		update(1, 1, n, i, x);
	}

	while(m--) //pentru fiecare operatie
	{
		scanf("%d%d%d", &op, &x, &y);
		if(op) update(1, 1, n, x, y);
		else printf("%d\n", querry(1, 1, n, x, y));
	}
	return 0;
}