Cod sursa(job #2215656)

Utilizator DawlauAndrei Blahovici Dawlau Data 23 iunie 2018 00:13:14
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

vector <int> ST;
int N, M;

void Build() {

	for (int node = N - 1; node; --node)
		ST[node] = max(ST[node << 1], ST[(node << 1) | 1]);
}

inline void Update(const int &idx, const int &val) {

	int node = idx + N - 1;
	ST[node] = val;

	for(node >>= 1; node; node >>= 1)
		ST[node] = max(ST[node << 1], ST[(node << 1) | 1]);
}

inline int Query(const int &lft, const int &rgt) {

	int lftNode = lft + N - 1;
	int rgtNode = rgt + N - 1;
	int ans = 0;

	while (lftNode <= rgtNode) {

		ans = max(ans, max(ST[lftNode], ST[rgtNode]));

		lftNode = (lftNode + 1) >> 1;  // try to go to the next branch
		rgtNode = (rgtNode - 1) >> 1;  // try to go to the prev branch
	}

	return ans;
}

int main() {

	fin >> N >> M;
	ST.resize(2 * N);

	for (int idx = 0; idx < N; ++idx)
		fin >> ST[idx + N];

	Build();

	for (; M; --M) {

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

		if (code == 0)
			fout << Query(a, b) << '\n';
		else
			Update(a, b);
	}
}