Cod sursa(job #966357)

Utilizator cont_de_testeCont Teste cont_de_teste Data 25 iunie 2013 19:29:40
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 2.01 kb
#include <algorithm>
#include <iostream>
#include <fstream>

int N, M;


//-----------------------------------------------------------------------------
template<int MaxElements>
	struct ArbInt {
		int A[MaxElements];
		int iLeft, iRight, Value, Position;
		ArbInt() {
			for (int i = 0; i < MaxElements; i++)
				this->A[i] = 0;
		}

		inline void Update(const int &Node, const int &Left, const int &Right) {
			if (Left == Right) {
				this->A[Node] = this->Value;
				return ;
			}
			const int &Middle = Left + (Right - Left) / 2;
			if (this->Position <= Middle)
				Update(Node * 2, Left, Middle);
			else
				Update(Node * 2 + 1, Middle + 1, Right);
			this->A[Node] = std::max(this->A[Node * 2], this->A[Node * 2 + 1]);
		}
		inline int Query(const int &Node, const int &Left, const int &Right) {
			if (this->iLeft <= Left && Right <= this->iRight) {
				return this->A[Node];
			}
			const int &Middle = Left + (Right - Left) / 2; int Result = 0;
			if (this->iLeft <= Middle)
				Result = std::max(Result, Query(Node * 2, Left, Middle));
			if (Middle < this->iRight)
				Result = std::max(Result, Query(Node * 2 + 1, Middle + 1, Right));
			return Result;
		}
		inline void update(const int &value, const int &position) {
			this->Value = value; this->Position = position;
			Update(1, 1, N);
		}
		inline int query(const int &ileft, const int &iright) {
			this->iLeft = ileft; this->iRight = iright;
			return Query(1, 1, N);
		}
	};
//-----------------------------------------------------------------------------

ArbInt<260100> AI;

void Make() {
	std::ifstream fin("arbint.in");
	fin >> N >> M;
	for (register int i = 1; i <= N; i++) {
		int aux;
		fin >> aux;
		AI.update(aux, i);
	}
	std::ofstream fout("arbint.out");
	for (register int i = 1; i <= M; i++) {
		int Operation;
		fin >> Operation;
		if (Operation == 0) {
			int Left, Right;
			fin >> Left >> Right;
			fout << AI.query(Left, Right) << '\n';
		} else {
			int Value, Position;
			fin >> Position >> Value;
			AI.update(Value, Position);
		}
	}
}

int main() {
	Make();
}