Cod sursa(job #3134523)

Utilizator daryusoctavyanDarius Octavian Chifor daryusoctavyan Data 29 mai 2023 11:32:19
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>

using namespace std;

int pow_of_two(int n) {
  int res = 1;

  while (res < n) {
    res *= 2;
  }

  return res;
}

int f(int a, int b) {
  return max(a, b);
}

void update(int pos, int element, vector<int> &v) {
  v[pos] = element;
  
  while (pos != 1) {
    pos = pos / 2;
    v[pos] = f(v[2 * pos], v[2 * pos + 1]);
  }
}

int query(int left, int right, vector<int> &v) {
  int res = 0;

  while (left <= right) {
    if (left % 2 == 1) {
      res = f(res, v[left]);
      left++;
    }

    if (right % 2 == 0) {
      res = f(res, v[right]);
      right--;
    }

    left /= 2;
    right /= 2;
  }

  return res;
}

int main() {
  ifstream cin("arbint.in");
  ofstream cout("arbin.out");

  int n, m;
  cin >> n >> m;
  int len = pow_of_two(n);

  vector<int> v(2 * len, 0);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    update(i + len, x, v);
  }

  while (m--) {
    int problem_type;
    cin >> problem_type;
    if (problem_type == 0) {
      int left, right;
      cin >> left >> right;
      left--;
      right--;
      cout << query(len + left, len + right, v) << endl;
    } else if (problem_type == 1) {
      int pos, element;
      cin >> pos >> element;
      pos--;
      update(len + pos, element, v);
    }
  }
 
  return 0;
}