Cod sursa(job #2213831)

Utilizator DawlauAndrei Blahovici Dawlau Data 17 iunie 2018 17:39:35
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.54 kb
#include <fstream>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;

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

class SegmentTree {

	private:

		inline int leftSon(const int &node) {

			return (node << 1);
		}

		inline int rightSon(const int &node) {

			return ((node << 1) | 1);
		}

		vector <int> ST;

	public:

		SegmentTree(const int &N) {

			ST.resize((1 << (int)(log2(N) + 2)) + 1);
		}

		void update(int node, int low, int high, const int &idx, const int &no) {

			if (low == high) {

				ST[node] = no;
				return;
			}

			int mid = low + ((high - low) >> 1);
			if (idx <= mid)
				update(leftSon(node), low, mid, idx, no);
			else
				update(rightSon(node), mid + 1, high, idx, no);

			ST[node] = max(ST[leftSon(node)], ST[rightSon(node)]);
		}

		int query(int node, int low, int high, const int &a, const int &b) {

			if (a <= low && high <= b)
				return ST[node];

			int mid = low + ((high - low) >> 1);
			int ans = -1;

			if (a <= mid)
				ans = query(leftSon(node), low, mid, a, b);
			if (mid + 1 <= b)
				ans = max(ans, query(rightSon(node), mid + 1, high, a, b));

			return ans;
		}
};

int main() {

	int N, M;
	fin >> N >> M;

	SegmentTree ST(N);

	for (int idx = 1; idx <= N; ++idx) {

		int no;
		fin >> no;
		ST.update(1, 1, N, idx, no);
	}

	for (; M; --M) {

		int code, a, b;
		fin >> code >> a >> b;

		if (code)
			ST.update(1, 1, N, a, b);
		else
			fout << ST.query(1, 1, N, a, b) << '\n';
	}
}