Cod sursa(job #2585928)

Utilizator mariaghinescu22Ghinescu Maria mariaghinescu22 Data 19 martie 2020 15:30:40
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

const int INF = 1 << 30;
const int N = 1 << 18;
int n, m, a, b, type, poz, val, t[N];

void update(int p, int st, int dr) {
	if (st == dr) {
		t[p] = val;
		return;
	}
	int m = (st + dr) / 2;
	if (poz <= m) update(2 * p, st, m);
	else update(2 * p + 1, m + 1, dr);
	t[p] = max(t[2 * p], t[2 * p + 1]);
}

int questioning(int p, int st, int dr) {   //max[st, dr] intersectat cu [a, b]
	if (a <= st && dr <= b) return t[p];
	int m = (st + dr) / 2, maxst = -INF, maxdr = -INF;
	if (a <= m) maxst = questioning(2 * p, st, m);
	if (b > m) maxdr = questioning(2 * p + 1, m + 1, dr);
	return max(maxst, maxdr);
}

int main() {
	in >> n >> m;
	for (int i = 1; i <= n; i++) {
		in >> val;
		poz = i;
		update(1, 1, n);
	}
	for (int i = 1; i <= m; i++) {
		in >> type;
		if (type == 0) {
			in >> a >> b;
			out << questioning(1, 1, n) << '\n';
		}
		if (type == 1) {
			in >> poz >> val;
			update(1, 1, n);
		}
	}
	return 0;
}