Cod sursa(job #3313025)

Utilizator DobraVictorDobra Victor Ioan DobraVictor Data 1 octombrie 2025 19:57:55
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.62 kb
#include <iostream>
#include <fstream>
#include <stdint.h>

const int32_t MAX_N = 100000;

int32_t treeVals[(MAX_N << 1) + 1];

int32_t max(int32_t x, int32_t y) {
	return (x > y) ? x : y;
}
void UpdateTree(int32_t ind, int32_t newVal, int32_t treeInd, int32_t treeLeft, int32_t treeRight) {
	if(treeLeft == treeRight) {
		treeVals[treeInd] = newVal;
		return;
	}

	int32_t treeMid = (treeLeft + treeRight) >> 1;
	if(ind <= treeMid) {
		UpdateTree(ind, newVal, treeInd << 1, treeLeft, treeMid);
	} else {
		UpdateTree(ind, newVal, (treeInd << 1) + 1, treeMid + 1, treeRight);
	}
	treeVals[treeInd] = max(treeVals[treeInd << 1], treeVals[(treeInd << 1) + 1]);
}
int32_t GetMax(int32_t left, int32_t right, int32_t treeInd, int32_t treeLeft, int32_t treeRight) {
	if(treeLeft > treeRight)
		return 0;
	if(treeRight < left || treeLeft > right)
		return 0;
	if(treeLeft >= left && treeRight <= right)
		return treeVals[treeInd];
	
	int32_t treeMid = (treeLeft + treeRight) >> 1;
	int32_t res1 = GetMax(left, right, treeInd << 1, treeLeft, treeMid);
	int32_t res2 = GetMax(left, right, (treeInd << 1) + 1, treeMid + 1, treeRight);

	return max(res1, res2);
}

int main() {
	std::ifstream fin("arbint.in");
	std::ofstream fout("arbint.out");

	int32_t n, m;
	fin >> n >> m;
	for(int32_t i = 1; i <= n; ++i) {
		int32_t val;
		fin >> val;
		UpdateTree(i, val, 1, 1, n);
	}

	for(int32_t i = 1; i <= m; ++i) {
		int32_t c, a, b;
		fin >> c >> a >> b;

		if(c == 0) {
			fout << GetMax(a, b, 1, 1, n) << '\n';
		} else {
			UpdateTree(a, b, 1, 1, n);
		}
	}

	fin.close();
	fout.close();

	return 0;
}