Cod sursa(job #3357424)

Utilizator bogdanvladmihaiBogdan Vlad-Mihai bogdanvladmihai Data 9 iunie 2026 16:48:14
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.29 kb
#include <cstdio>
#include <iostream>
#include <limits>
#include <vector>

class PointUpdateRangeMaxSolver {
public:
  PointUpdateRangeMaxSolver(std::vector<int> v) : n(v.size()), tree(4 * n) {
    build(1, 0, n - 1, v);
  }

  int query(int l, int r) const { return query(1, 0, n - 1, l, r); }
  void update(int pos, int value) { update(1, 0, n - 1, pos, value); }

private:
  void build(int u, int l, int r, const std::vector<int>& v) {
    if (l == r) {
      tree[u].max_value = v[l];
      return;
    }

    int mid = (l + r) / 2;
    build(2 * u, l, mid, v);
    build(2 * u + 1, mid + 1, r, v);
    tree[u].max_value =
        std::max(tree[2 * u].max_value, tree[2 * u + 1].max_value);
  }

  int query(int u, int l, int r, int ql, int qr) const {
    if (ql <= l && r <= qr) {
      return tree[u].max_value;
    }

    int mid = (l + r) / 2;
    int answer = std::numeric_limits<int>::min();
    if (ql <= mid) {
      answer = std::max(answer, query(2 * u, l, mid, ql, qr));
    }
    if (qr > mid) {
      answer = std::max(answer, query(2 * u + 1, mid + 1, r, ql, qr));
    }
    return answer;
  }

  void update(int u, int l, int r, int pos, int value) {
    if (l == r) {
      tree[u].max_value = value;
      return;
    }

    int mid = (l + r) / 2;
    if (pos <= mid) {
      update(2 * u, l, mid, pos, value);
    } else {
      update(2 * u + 1, mid + 1, r, pos, value);
    }
    tree[u].max_value =
        std::max(tree[2 * u].max_value, tree[2 * u + 1].max_value);
  }

  // Generic structure for segment tree nodes. In this case, we only need to
  // store the maximum value in the segment, but this structure can be extended
  // to store additional information if needed.
  struct Node {
    int max_value = std::numeric_limits<int>::min();
  };

  int n;
  std::vector<Node> tree;
};

int main() {
  freopen("arbint.in", "r", stdin);
  freopen("arbint.out", "w", stdout);

  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n, m;
  std::cin >> n >> m;
  std::vector<int> v(n);
  for (int& x : v) {
    std::cin >> x;
  }

  PointUpdateRangeMaxSolver T{std::move(v)};
  for (int i = 0; i < m; i++) {
    int op_type, a, b;
    std::cin >> op_type >> a >> b;
    if (op_type == 1) {
      T.update(a - 1, b);
    } else {
      std::cout << T.query(a - 1, b - 1) << "\n";
    }
  }
  return 0;
}