Pagini recente » Cod sursa (job #341825) | Cod sursa (job #61153) | Cod sursa (job #1575279) | Rating SUPREME (SUPREME007) | Cod sursa (job #2055812)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("arbint.in");
ofstream out("arbint.out");
const int NMAX = 100000;
int n, m, ni; //number of internal nodes in the full segment tree
int arb[4 * (1 + NMAX)];
void update(int i) { //updatezi un nod cu indicele i
if(0 < i) {
arb[i] = max(arb[2 * i], arb[2 * i + 1]);
update(i / 2);
}
}
int query(int from, int to) { //interoghezi un interval
int answer = 0; //valoare stabila in raport cu functionalitatea query-ul
if(from == to)
answer = arb[from];
else if(from < to) {
if(from % 2 == 1) {
answer = max(answer, arb[from]);
from++;
}
if(to % 2 == 0) {
answer = max(answer, arb[to]);
to--;
}
answer = max(answer, query(from / 2, to / 2));
}
return answer;
}
int main() {
in >> n >> m;
int exp = 0;
while((1<<exp) < n)
exp++;
ni = (1<<exp) - 1;
for(int i = 1; i <= n; i++) {
in >> arb[ni + i];
update((ni + i) / 2);
}
for(int i = 1; i <= m; i++) {
int op, x, y;
in >> op >> x >> y;
if(op == 1) {
arb[ni + x] = y;
update((ni + x) / 2);
} else {
out << query(ni + x, ni + y) << '\n';
}
}
return 0;
}