Pagini recente » Cod sursa (job #167724) | Cod sursa (job #2603371) | Cod sursa (job #2469284) | Cod sursa (job #405351) | Cod sursa (job #1097554)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
const int MAXN = 100009;
int N, M, op, a, b, maxim, Val;
int v[4 * MAXN];
int arbore[4 * MAXN];
inline int Left_Son(int node) {
return 2 * node;
}
inline int Right_Son(int node) {
return 2 * node + 1;
}
inline int Father(int node) {
return node / 2;
}
void Query(int node, int Left, int Right) {
if (a <= Left && Right <= b)
maxim = max(maxim, arbore[node]);
else {
int Middle = (Left + Right) / 2;
if (a <= Middle)
Query(Left_Son(node), Left, Middle);
if (Middle + 1 <= b)
Query(Right_Son(node), Middle + 1, Right);
}
}
void Update(int node, int Left, int Right) {
if (Left == Right)//we've reached the node whose value we want to change
arbore[node] = Val;
else {
int Middle = (Left + Right) / 2;
//calculates the maximums between ranges [Left, Middle] and [Middle + 1, Right]
if (a <= Middle)
Update(Left_Son(node), Left, Middle);
if (Middle + 1 <= a)
Update(Right_Son(node), Middle + 1, Right);
arbore[node] = max(arbore[Left_Son(node)], arbore[Right_Son(node)]);
}
}
void Read() {
fin >> N >> M;
for (int i = 1; i <= N; ++i) {
fin >> v[i];
a = i;
Val = v[i];
Update(1, 1, N);
}
}
int main() {
Read();
while (M--) {
fin >> op >> a >> b;
if (op == 0) {//query
maxim = 0;
Val = b;
Query(1, 1, N);
fout << maxim << '\n';
}else {//update
Update(1, 1, N);
}
}
fin.close();
fout.close();
return 0;
}