Cod sursa(job #3365301)

Utilizator gilbusJoita Cristian gilbus Data 18 septembrie 2026 16:38:53
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
int n, m;
vector<int>sir, ait;
void build(int nod, int st, int dr) {
	if (st == dr) {
		ait[nod] = sir[st];
		return;
	}
	int mijloc = (st + dr) / 2;
	build(nod * 2, st, mijloc);
	build(nod * 2 + 1, mijloc + 1, dr);
	ait[nod] = max(ait[2 * nod], ait[2 * nod + 1]);
}
void Update(int nod, int st, int dr, int poz, int val) {
	if (st == dr) {
		ait[nod] = val;
		return;
	}
	int mijloc = (st + dr) / 2;
	if (poz <= mijloc) {
		Update(nod * 2, st, mijloc, poz, val);
	}
	else {
		Update(nod * 2 + 1, mijloc + 1, dr, poz, val);
	}
	ait[nod] = max(ait[2 * nod], ait[2 * nod + 1]);
}
int query(int nod, int st, int dr, int L, int R) {
	if (L <= st && R >= dr) {
		return ait[nod];
	}
	else if (st > R || dr < L) {
		return 0;
	}
	int mijloc = (st + dr) / 2;
	return max(query(nod * 2, st, mijloc, L, R), query(nod * 2 + 1, mijloc + 1, dr, L, R));
}
int main() {
	fin >> n >> m;
	sir.resize(n + 1);
	ait.resize((n + 1) * 4);
	for (int i = 1; i <= n; i++) {
		fin >> sir[i];
	}
	build(1, 1, n);
	int op, a, b;
	while (m--) {
		fin >> op >> a >> b;
		if (op == 0) {
			fout << query(1, 1, n, a, b) << '\n';
		}
		else {
			Update(1, 1, n, a, b);
		}
	}
}