Pagini recente » Cod sursa (job #379974) | Cod sursa (job #2728858) | Cod sursa (job #2050091) | Cod sursa (job #3171387) | Cod sursa (job #966357)
Cod sursa(job #966357)
#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();
}