Cod sursa(job #1909117)

Utilizator lflorin29Florin Laiu lflorin29 Data 7 martie 2017 11:40:31
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include <bits/stdc++.h>

using namespace std;

const int nMax = 100003;

int n, m, a[nMax];

struct AintMax {
	int *aint;
	AintMax(const int sz = 0) {
		aint = new int[4 * sz + 8];
	}
	inline void update(int nod) {
		aint[nod] = max(aint[nod * 2], aint[nod * 2 + 1]);
	}

	void update(int pos, int value, int st, int dr, int nod) {
		if (st == dr) {
			aint[nod] = value;
			return;
		}

		int mid = (st + dr) / 2;

		if (pos <= mid)
			update(pos, value, st, mid, nod * 2);
		else update(pos, value, mid + 1, dr, nod * 2 + 1);

		update(nod);
	}

	int query(int x, int y, int st, int dr, int nod) {
		if (x > dr || st > y)
			return 0;

		if (st >= x && dr <= y) {
			return aint[nod];
		}

		int mid = (st + dr) / 2;
		return max(query(x, y, st, mid, nod * 2),
		           query(x, y, mid + 1, dr, nod * 2 + 1));
	}
};

AintMax Aint;

int main() {
	freopen("arbint.in", "r", stdin);
	freopen("arbint.out", "w", stdout);

	scanf("%d %d", &n, &m);

	for (int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
	}

	Aint = AintMax(n);

	for (int i = 1; i <= n; ++i)
		Aint.update(i, a[i], 1, n, 1);

	for (int i = 1; i <= m; ++i) {
		int tip, x, y;
		scanf("%d%d%d", &tip, &x, &y);

		if (tip == 0) {
			printf("%d\n", Aint.query(x, y, 1, n, 1));
		}
		else {
			Aint.update(x, y, 1, n, 1);
		}

	}
	return 0;
}