Cod sursa(job #1449953)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 11 iunie 2015 02:06:16
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.8 kb
#include<bits/stdc++.h>

using namespace std;

#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')

typedef long long int lld;
const int INF = (1LL << 30) - 1;
const lld LINF = (1LL << 62) - 1;
const int NMAX = 100000 + 5;

template<typename T>
class SegmentTree {

private:
	vector<T> AI;
	int N;

	void upd(int nod, int lo, int hi, int poz, T val) {
		if (lo == hi) {
			AI[nod] = val;
			return;
		}

		int mi = (lo + hi) / 2;

		if (poz <= mi)
			upd(2 * nod + 0, lo, mi + 0, poz, val);
		else
			upd(2 * nod + 1, mi + 1, hi, poz, val);

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

	T qry(int nod, int lo, int hi, int L, int R) {
		if (L <= lo && hi <= R)
			return AI[nod];

		if (R < lo || hi < L)
			return -1;

		int mi = (lo + hi) / 2;

		T q1 = qry(2 * nod + 0, lo, mi + 0, L, R);
		T q2 = qry(2 * nod + 1, mi + 1, hi, L, R);

		return max(q1, q2);
	}

	void bld(int nod, int lo, int hi, T V[]) {
		if (lo == hi) {
			AI[nod] = V[lo];
			return;
		}

		int mi = (lo + hi) / 2;

		bld(2 * nod + 0, lo, mi + 0, V);
		bld(2 * nod + 1, mi + 1, hi, V);

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

public:
	SegmentTree() {}

	SegmentTree(int N) {
		this->N = N;
		AI.resize(4 * N + 20);
	}

	SegmentTree(int N, T V[]) {
		this->N = N;
		AI.resize(4 * N + 20);
		bld(1, 1, N, V);
	}

	void update(int position, T value) {
		upd(1, 1, N, position, value);
	}

	T query(int left, int right) {
		return qry(1, 1, N, left, right);
	}
};

SegmentTree<int> A;
int N, M;
int V[NMAX];

int main() {
	int t, x, y;

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

	scanf("%d%d", &N, &M);

	for (int i = 1; i <= N; i++)
		scanf("%d", &V[i]);

	A = SegmentTree<int>(N, V);

	while (M--) {
		scanf("%d%d%d", &t, &x, &y);

		if (t == 0)
			printf("%d\n", A.query(x, y));
		else
			A.update(x, y);
	}

	return 0;
}