Cod sursa(job #3218058)

Utilizator alexiksmAgasanov Alecsei alexiksm Data 25 martie 2024 20:16:24
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.49 kb
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>

using namespace std;

#pragma warning(disable : 4996)
;

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

int lemaxi[400000], n, m, v[100001];

void build(int nod, int st, int dr) {

	if (st == dr) {
		lemaxi[nod] = v[st];
		return;
	}

	int mij = (st + dr) / 2;
	build(2 * nod, st, mij);
	build(2 * nod + 1, mij + 1, dr);
	lemaxi[nod] = max(lemaxi[2 * nod], lemaxi[2 * nod + 1]);
}

void update(int nod, int st, int dr, int a, int b) {
	if (st == dr) {
		lemaxi[nod] = b;
		return;
	}
	int mij = (st + dr) / 2;
	if (a <= mij) update(2 * nod, st, mij, a, b);
	else update(2 * nod + 1, mij + 1, dr, a, b);

	lemaxi[nod] = max(lemaxi[2 * nod], lemaxi[2 * nod + 1]);
}

int query(int nod, int st, int dr, int a, int b) {
	if (a <= st && dr <= b) 
		return lemaxi[nod];
	int answer = -1;
	int mij = (st + dr) / 2;
	if (mij >= a) answer = max(answer, query(2 * nod, st, mij, a, b));
	if (mij < b) answer = max(answer, query(2 * nod + 1, mij + 1, dr, a, b));
	return answer;
}

int main() {

	fin >> n >> m;

	for (int i = 1; i <= n; i++)
		fin >> v[i];

	build(1, 1, n);

	for (int i = 1; i <= m; i++) {
		int cer, a, b;
		fin >> cer >> a >> b;
		if (!cer) {
			fout << query(1, 1, n, a, b) << '\n';
		}
		else {
			update(1, 1, n, a, b);
		}
	}

	return 0;
}