Cod sursa(job #2190443)

Utilizator AlexnolifeAlexandru Ica Alexnolife Data 30 martie 2018 20:13:47
Problema Arbori de intervale Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <limits>
#include <array>

std::ifstream f("arbint.in");
std::ofstream g("arbint.out");

enum OpType { MAX = 0, CHANGEVAL = 1 };

constexpr unsigned int MAX_S = 100001;

int n, numOp;
std::array<unsigned int, MAX_S> v;
std::array<unsigned int, 4 * MAX_S> segTree;

void ConstructTree(unsigned int lo, unsigned int hi, unsigned int pos)
{
	if (lo == hi) {
		segTree[pos] = v[lo];
		return;
	}

	int mid = lo + (hi - lo) / 2;
	ConstructTree(lo, mid, 2 * pos + 1);
	ConstructTree(mid + 1, hi, 2 * pos + 2);

	segTree[pos] = std::max(segTree[2 * pos + 1], segTree[2 * pos + 2]);
}

unsigned int GetMaxFromInterval(unsigned int a, unsigned int b, unsigned int lo, unsigned int hi, unsigned int pos = 0)
{
	if (a <= lo && b >= hi) // total overlap
		return segTree[pos];
	if (a > hi || b < lo)   // no overlap
		return std::numeric_limits<unsigned int>::min();
	
	unsigned int mid = lo + (hi - lo) / 2;
	return std::max(GetMaxFromInterval(a, b, lo,      mid, 2 * pos + 1), 
					GetMaxFromInterval(a, b, mid + 1, hi,  2 * pos + 2));
}

void Read()
{
	f >> n >> numOp;

	unsigned int i, type, a, b;

	for (i = 0; i < n; ++i) {
		f >> v[i];
	}

	ConstructTree(0, n - 1, 0);

	for (i = 0; i < numOp; ++i) {
		f >> type >> a >> b;

		switch (type)
		{
		case OpType::MAX: {
			g << GetMaxFromInterval(a - 1, b - 1, 0, n - 1) << '\n';
			break;
		}
		case OpType::CHANGEVAL: {
			v[a - 1] = b;
			ConstructTree(0, n - 1, 0);
			break;
		}
		}
	}
}

int main(int, char *[])
{
	Read();

	return 0;
}