Cod sursa(job #3233998)

Utilizator betybety bety bety Data 5 iunie 2024 19:15:21
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("arbint.in");
ofstream out("arbint.out");
const int inf = 1e9 + 7;
const int lim = 1e5 + 4;
int tree[4 * lim];
int v[lim];
void build(int nod, int l, int r) {
  if (l == r) {
    tree[nod] = v[l];
    return;
  }
  int med = (l + r) >> 1;
  build(2 * nod, l, med);
  build(2 * nod + 1, med + 1, r);
  tree[nod] = max(tree[2 * nod], tree[2 * nod + 1]);
}
void update(int nod, int l, int r, int pos, int val) {
  if (l == r) {
    tree[nod] = val;
    return;
  }
  int med = (l + r) >> 1;
  if (pos <= med) {
    update(2 * nod, l, med, pos, val);
  } else {
    update(2 * nod + 1, med + 1, r, pos, val);
  }
  tree[nod] = max(tree[2 * nod], tree[2 * nod + 1]);
}
int query(int nod, int l, int r, int a, int b) {
  if (a <= l and r <= b) {
    return tree[nod];
  }
  int med = (l + r) >> 1;
  int lmax = -inf, rmax = -inf;
  if (a <= med) {
    lmax = query(2 * nod, l, med, a, b);
  }
  if (b > med) {
    rmax = query(2 * nod + 1, med + 1, r, a, b);
  }
  return max(lmax, rmax);
}
int n, m;
int t, x, y;
int main() {
  in >> n >> m;
  for (int i = 1; i <= n; ++i) {
    in >> v[i];
  }
  build(1, 1, n);
  while (m--) {
    in >> t >> x >> y;
    if (t == 0) {
      out << query(1, 1, n, x, y) << '\n';
    } else {
      update(1, 1, n, x, y);
    }
  }
  return 0;
}