Pagini recente » Cod sursa (job #1074930) | Cod sursa (job #441396) | Cod sursa (job #2922549) | Borderou de evaluare (job #1331036) | Cod sursa (job #2684937)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 100001;
int n, m, arr[NMAX];
struct SegmentTree {
int leftmost, rightmost;
int value;
SegmentTree *left, *right;
SegmentTree(int leftmost, int rightmost, int array[]) : leftmost(leftmost), rightmost(rightmost) {
if (leftmost == rightmost) {
value = array[leftmost];
} else {
int mid = leftmost + (rightmost - leftmost) / 2;
left = new SegmentTree(leftmost, mid, array);
right = new SegmentTree(mid + 1, rightmost, array);
recompute();
}
}
void recompute() {
if (leftmost == rightmost) return;
value = max(left->value, right->value);
}
void update(int index, int newValue) {
if (leftmost == rightmost) {
value = newValue;
return;
}
if (index <= left->rightmost) {
left->update(index, newValue);
} else {
right->update(index, newValue);
}
recompute();
}
int query(int l, int r) {
if (l > rightmost || r < leftmost) return INT_MIN;
if (l <= leftmost && r >= rightmost) return value;
return max(left->query(l, r), right->query(l, r));
}
};
int main() {
freopen("arbint.in", "r", stdin);
freopen("arbint.out", "w", stdout);
scanf("%d %d ", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d ", &arr[i]);
SegmentTree *tree = new SegmentTree(1, n, arr);
while (m--) {
int type, a, b;
scanf("%d %d %d ", &type, &a, &b);
if (type == 0)
printf("%d\n", tree->query(a, b));
else
tree->update(a, b);
}
}