Cod sursa(job #2190300)

Utilizator ibogdan25Ilie Ionut ibogdan25 Data 30 martie 2018 14:43:41
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.94 kb
// Arbori de intervale.cpp : Defines the entry point for the console application.
//

#include "iostream"
#include "algorithm"
#include "fstream"
#define NMAX 100002
using namespace std;

int aInt[NMAX * 2], V[NMAX], nV, m;
ifstream f("arbint.in");
ofstream g("arbint.out");
void read() {
	f >> nV >> m;
	for (int i = 1; i <= nV; i++) {
		f >> V[i];
	}
}
void build(int left, int right, int pos) {
	if (left == right) {
		aInt[pos] = V[left];
		return;
	}
	int mij = (left + right) / 2;
	build(left, mij, 2 * pos);
	build(mij + 1, right, 2 * pos + 1);
	aInt[pos] = max(aInt[2 * pos], aInt[2 * pos + 1]);

}

int returnMax(int left, int right, int leftArea, int rightArea, int pos) {
	if (left >= leftArea && right <= rightArea) {
		return aInt[pos];
	}
	int valMax = -1, mij = (left + right) / 2;
	if ((leftArea >= left && leftArea <= mij) || (rightArea >= left && rightArea <= mij) || (left >= leftArea && mij <= rightArea)) {
		valMax = returnMax(left, mij, leftArea, right, pos * 2);
	}
	if ((leftArea >= mij + 1 && leftArea <= right) || (rightArea >= mij + 1 && rightArea <= right) || (mij + 1 >= leftArea && right <= rightArea)) {
		valMax = max ( valMax, returnMax(mij + 1, right, leftArea, right, pos * 2 + 1));
	}
	return valMax;
}
void update(int left, int right, int pos, int pozModif, int valModif) {
	if (left == right) {
		aInt[pos] = valModif;
		return;
	}
	int mij = (left + right) / 2;
	if (pozModif >= left && pozModif <= mij) {
		update(left, mij, 2 * pos, pozModif, valModif);
	}
	if (pozModif >= mij + 1 && pozModif <= right) {
		update(mij + 1, right , 2 * pos + 1, pozModif, valModif);
	}
	aInt[pos] = max(aInt[2 * pos], aInt[2 * pos + 1]);
}
int main()
{
	read();
	build(1, nV, 1);
	while (m--) {
		int optiune = 0, a = 0, b = 0;
		f >> optiune >> a >> b;
		if (optiune == 1) {
			update(1, nV, 1, a, b);
		}
		else {
			g << returnMax(1, nV, a, b, 1) << "\n";
		}
	}
    return 0;
}