Cod sursa(job #519471)

Utilizator andreitheo87Teodorescu Andrei-Marius andreitheo87 Data 5 ianuarie 2011 18:01:24
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

const int NMAX = 100000;
const int ARBMAX = 2*NMAX + 10;

ifstream fin("arbint.in");
ofstream fout("arbint.out");

int arb[4*NMAX + 10];

void insert(int nod, int left, int right, int pos, int newValue) {
	if (left != right) {
		int mid = (left + right)>>1;
		if (pos <= mid) insert(nod<<1, left, mid, pos, newValue);
		else insert((nod<<1) + 1, mid + 1, right, pos, newValue);
		arb[nod] = max(arb[(nod<<1)], arb[(nod<<1)+1]);
	} else arb[nod] = newValue;
}

int query(int nod, int left, int right, int a, int b) {
	if (a>right || b<left) return 0; // minvalue
	if (a<=left && b>=right) return
		arb[nod];
	else {
		int mid = (left + right)>>1;
		return max(query(nod<<1, left, mid, a, b),
					query((nod<<1) + 1, mid +1, right, a, b));
	}
	return 0;
}

int main() {
	int n, m;
	fin >> n;
	fin >> m;
	for (int i=0; i<n; i++) {
		int x;
		fin >> x;
		insert(1, 0, n-1, i, x);
	}
	for (int i=0; i<m; i++) {
		int op, a, b;
		fin >> op >> a >> b;
		switch (op) {
			case 0:
				fout << query(1, 0, n-1, a-1, b-1) << endl;
				break;
			case 1:
				insert(1, 0, n-1, a-1, b);
				break;
		}
	}
	return 0;
}