Cod sursa(job #1348241)

Utilizator PikachuPikachu Pikachu Data 19 februarie 2015 16:28:27
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <cstdio>
#include <algorithm>

using namespace std;

int aint[400005], ans;

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

void query(int nod, int st, int dr, int x, int y) {
	int med;
	if(x <= st && y >= dr) {
		ans = max(ans, aint[nod]);
		return ;
	}
	med = (st + dr) / 2;
	if(x <= med)
		query(2 * nod, st, med, x, y);
	if(y > med)
		query(2 * nod + 1, med + 1, dr, x, y);
}

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

	int n, m, x, t, y;
	scanf("%d%d", &n, &m);

	for(int i = 1; i <= n; ++ i) {
		scanf("%d", &x);
		update(1, 1, n, i, x);
	}
	
	for(int i = 1; i <= m; ++ i) {
		scanf("%d%d%d", &t, &x, &y);
		if(t == 0) {
			ans = 0;
			query(1, 1, n, x, y);
			printf("%d\n", ans);
		} else {
			update(1, 1, n, x, y);
		}
	}

	return 0;
}