Cod sursa(job #700320)

Utilizator DSzprogDombi Szabolcs DSzprog Data 1 martie 2012 09:28:26
Problema Arbori de intervale Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>

#define max(a, b) (a > b ? a : b)

int n, m;
int start, end;
int heap[240000];

int locate(int x) {
	int pos = 1;
	while (x > pos) {
		pos <<= 1;
	}
	return(pos);
}

void swap(int pos, int val) {
	heap[pos] = val;
	while (pos >>= 1) {
		heap[pos] = max(heap[pos * 2], heap[pos * 2 + 1]);
	}
}

int findmax(int l, int r) {
	if (l == r) {
		return(heap[l]);
	}
	if (l % 2) {
		return(max(heap[l], findmax(l + 1, r)));
	}
	if (r % 2) {
		return(findmax(l / 2, r / 2));
	}
	return(max(findmax(l, r - 1), heap[r]));
}

int main() {
	FILE * in = fopen("arbint.in", "rt");
	FILE * out = fopen("arbint.out", "wt");

	fscanf(in, "%d%d", &n, &m);
	start = locate(n);
	end = start + n;
	for (int i = start; i < end; ++i) {
		fscanf(in, "%d", &heap[i]);
	}

	int level = start;
	while (level >>= 1) {
		for (int i = level; i < level * 2; ++i) {
			heap[i] = max(heap[2 * i], heap[2 * i + 1]);
		}
	}

	for (int i = 0; i < m; ++i) {
		int a, b, c;
		fscanf(in, "%d%d%d", &a, &b, &c);
		if (a) {
			swap(start + b - 1, c);
		} else {
			fprintf(out, "%d\n", findmax(start + b - 1, start + c - 1));
		}
	}

	fclose(in);
	fclose(out);
}