Cod sursa(job #2439785)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 16 iulie 2019 21:16:33
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
// https://www.infoarena.ro/problema/arbint
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

const char input_file[] = "arbint.in";
const char output_file[] = "arbint.out";

ifstream is(input_file);
ofstream os(output_file);

int n, m, N;
vector<int> tree;

void Query(int left, int right) {
  int result = 0;
  left += N - 1;
  right += N - 1;
  while (left <= right) {
    result = max(result, max(tree[left], tree[right]));
    left = (left + 1) / 2;
    right = (right - 1) / 2;
  }

  os << result << "\n";
}

void SetValue(const int index, const int value) {
  tree[N + index - 1] = value;
  for (int k = (N + index - 1) / 2; k; k /= 2) {
    tree[k] = max(tree[2 * k], tree[2 * k + 1]);
  }
}


int main() {
  is >> n >> m;
  for (N = 1; N < n; N <<= 1);
  tree = vector<int>(2 * N);
  for (int i = 0; i < n; ++i) {
    is >> tree[N + i];
  }
  for (int i = N - 1; i; --i) {
    tree[i] = max(tree[2 * i], tree[2 * i + 1]);
  }

  int type, x, y;
  while (m--) {
    is >> type >> x >> y;
    type == 0 ? Query(x, y) : SetValue(x, y);
  }

  is.close();
  os.close();
  return 0;
}