Cod sursa(job #2948683)

Utilizator axel5919Marius Boroica axel5919 Data 27 noiembrie 2022 23:18:04
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.92 kb
#include <bits/stdc++.h>
#define ll long long
#define vec vector
#define INF INT_MAX
#define u_int unsigned int
using namespace std;

#define access_in "arbint.in"
#define access_out "arbint.out"

#ifndef DEBUG
#define cin in
#define cout out
ifstream in(access_in);
ofstream out(access_out);
#endif

int v[400001];
int vv[100001];
int startA, startB, target;

int build(ll nod, int a, int b) {
  if (a > b)
    return 0;
  if (a == b)
    return v[nod] = vv[a];
  int tm = a + (b - a) / 2;
  v[nod * 2] = build(nod * 2, a, tm);
  v[nod * 2 + 1] = build(nod * 2 + 1, tm + 1, b);
  v[nod] = max(v[nod * 2], v[nod * 2 + 1]);
  return v[nod];
}

int modify(ll nod, int a, int b) {
  if (a > b) {
    return vv[b];
  }
  if (a == b) {
    if (a == target)
      v[nod] = vv[target];
    return v[nod];
  }
  if (b < target || a > target) {
    return v[nod];
  }
  int tm = a + (b - a) / 2;
  v[nod] = max(modify(nod * 2, a, tm), modify(nod * 2 + 1, tm + 1, b));
  return v[nod];
}

int query(ll nod, int a, int b) {
  if (a > b) {
    return 0;
  }
  if (a >= startA && b <= startB) {
    return v[nod];
  }
  if (b < startA || a > startB) {
    return 0;
  }
  int tm = a + (b - a) / 2;
  return max(query(nod * 2, a, tm), query(nod * 2 + 1, tm + 1, b));
}

void solve() {
  int n, m, cmd, a, b;
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    cin >> vv[i];
  build(1, 0, n - 1);
  while (m--) {
    cin >> cmd >> a >> b;
    switch (cmd) {
    case 0:
      startA = a - 1, startB = b - 1;
      cout << query(1, 0, n - 1) << '\n';
      break;
    case 1:
      vv[a - 1] = b;
      target = a - 1;
      modify(1, 0, n - 1);
      break;
    }
  }
}

int main() {
#if defined(DEBUG)
  freopen("in.txt", "r", stdin);
#endif
#if defined(WRITE_OUT)
  freopen("out.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // int t = 1;
  // cin >> t;
  // while (t--)
  solve();
#ifndef DEBUG
  in.close();
  out.close();
#endif
}