Cod sursa(job #3002758)

Utilizator RobyDarioCorjuc Roberto RobyDario Data 15 martie 2023 09:18:42
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.39 kb
#include <iostream>
#include <fstream>

using namespace std;

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

#define zeros(x)((x^(x-1))&x)
const int NMax = 1e5 + 5;
const int oo = -2e9;

int v[NMax],n,m;
int ai[4 * NMax];

void build(int nod, int st, int dr) {
	int mij;
	if (st == dr) {
		ai[nod] = v[st];
	}
	else {
		mij = (st + dr) / 2;
		build(2 * nod, st, mij);
		build(2 * nod + 1, mij + 1, dr);
		ai[nod] = max(ai[2 * nod], ai[2 * nod + 1]);
	}
}
void update(int nod, int st, int dr, int a, int b) {
	int mij;
	if (st == dr) {
		ai[nod] = b;
		return;
	}
	else {
		mij = (st + dr) / 2;
		if (a <= mij) {
			update(2 * nod, st, mij, a, b);
		}
		else {
			update(2 * nod + 1, mij + 1, dr, a, b);
		}
		ai[nod] = max(ai[nod * 2], ai[2 * nod + 1]);
	}
}
int query(int nod, int st, int dr, int l, int r) {
	int mij;
	if (l <= st && r >= dr) {
		return ai[nod];
	}
	else {
		int a = oo, b = oo;
		mij = (st + dr) / 2;
		if (l <= mij) {
			a = query(2 * nod, st, mij, l, r);
		}
		if (mij < r) {
			b = query(2 * nod + 1, mij + 1, dr, l, r);
		}
		return max(a, b);
	}
}
int main() {
	fin >> n>>m;
	for (int i = 1; i <= n; i++) {
		fin >> v[i];
	}
	build(1, 1, n);
	while (m--) {
		int c, x, y;
		fin >> c >> x >> y;
		if (c == 0) {
			fout<<query(1, 1, n, x, y)<<'\n';
		}
		else {
			update(1, 1, n, x, y);
		}
	}
	return 0;
}