Cod sursa(job #303794)

Utilizator whiskeyOzzy Osbourne whiskey Data 10 aprilie 2009 13:30:42
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <cstdio>
#define NX 400000

int n, val, pos, start, end, maxim;
int arb[NX];

void update(int, int, int);
void query(int, int, int);
inline int max(int, int);

int main()
{
	int m, op, x, y;
	FILE *fin = fopen("arbint.in", "r");
	FILE *fout = fopen("arbint.out", "w");
	
	fscanf(fin, "%d%d", &n, &m);
	for (pos = 1; pos <= n; ++pos)
	{
		fscanf(fin, "%d", &val);
		update(1, 1, n);
	}
	
	for (; m; --m)
	{
		fscanf(fin, "%d%d%d", &op, &x, &y);
		if (op)
		{
			pos = x;
			val = y;
			update(1, 1, n);
		}
		else
		{
			start = x;
			end = y;
			maxim = 0;
			query(1, 1, n);
			fprintf(fout, "%d\n", maxim);
		}
	}
	
	fclose(fin);
	fclose(fout);
	return 0;
}

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

void query(int nod, int st, int dr)
{
	if (start <= st && dr <= end)
	{
		maxim = max(maxim, arb[nod]);
		return;
	}
	int mid = (st + dr) / 2;
	if (start <= mid)
	{
		query(2 * nod, st, mid);
	}
	if (mid < end)
	{
		query(2 * nod + 1, mid + 1, dr);
	}
}

inline int max(int a, int b)
{
	return (a > b ? a : b);
}