Pagini recente » Cod sursa (job #1819849) | Cod sursa (job #998033) | Cod sursa (job #2146178) | Cod sursa (job #2816210) | Cod sursa (job #1484420)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
class SegTree
{
int n;
vector<int> t;
public:
SegTree(const vector<int> & a)
{
n = a.size();
t.resize(2*n);
for (int i = 0; i < n; ++i) {
t[n+i] = a[i];
}
for (int i = n-1; i >= 1; --i) {
t[i] = max(t[2*i], t[2*i+1]);
}
}
void update(int idx, int val)
{
idx += n;
while (t[idx] != val && idx >= 1) {
t[idx] = val;
val = max(t[idx], t[idx^1]);
idx /= 2;
}
}
int getmax(int lo, int hi)
{
lo += n;
hi += n;
int res = 0;
while (lo < hi) {
if (lo%2 == 0) {
lo /= 2;
}
else {
res = max(res, t[lo]);
lo = lo/2+1;
}
if (hi%2 == 1) {
hi /= 2;
}
else {
res = max(res, t[hi]);
hi = hi/2-1;
}
}
if (lo == hi) {
res = max(res, t[lo]);
}
return res;
}
};
int main() {
ifstream fin("arbint.in");
ofstream fout("arbint.out");
int n, m;
fin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
fin >> a[i];
}
SegTree seg(a);
for (int i = 0; i < m; ++i) {
int op;
fin >> op;
if (op == 0) {
int lo, hi;
fin >> lo >> hi;
fout << seg.getmax(--lo, --hi) << '\n';
}
else {
int idx, val;
fin >> idx >> val;
seg.update(--idx, val);
}
}
return 0;
}