Cod sursa(job #1968910)

Utilizator MoodyFaresFares Mohamad MoodyFares Data 17 aprilie 2017 23:13:51
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <cstdio>
#include <algorithm>
#include <climits>

const int MAX_N = (1 << 17);

using namespace std;

int v[MAX_N+5], a[2*MAX_N+5];


void update(int nod, int st, int dr, int poz, int val) {
  int mid = (st + dr) / 2;
  if (st == dr) {
    a[nod] = val;
    return;
  }
  if (poz <= mid)
    update (nod * 2, st, mid, poz, val);
  else
    update (nod * 2 + 1, mid + 1, dr, poz, val);
  a[nod] = max(a[nod * 2], a[nod * 2 + 1]);
}

int query(int nod, int st, int dr, int x, int y) {
  int mid = (st + dr) / 2, SOL = 0;
  if (x <= st && dr <= y)
    return a[nod];
  if (x <= mid)
    SOL = max (SOL, query(nod * 2, st, mid, x, y));
  if (y > mid){
    SOL = max (SOL, query(nod * 2 + 1, mid + 1, dr, x, y));
    SOL = max (SOL, query(nod * 2 + 1, mid + 1, dr, x, y));
  }
  return SOL;
}

void init(int nod, int st, int dr) {
  int mid = (st + dr) / 2;
  if (st == dr) {
    a[nod] = v[st];
    return;
  }
  init (nod * 2, st, mid);
  init (nod * 2 + 1, mid + 1, dr);
  a[nod] = max (a[nod * 2], a[nod * 2 + 1]);
}

int main() {
  freopen ( "arbint.in", "r", stdin );
  freopen ( "arbint.out", "w", stdout );
  
  int n, m, i, p2, op, a, b;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; ++i)
    scanf ("%d", &v[i]);
  
  p2 = 1;
  while (p2 < n)
    p2 *= 2;
  init (1, 1, p2);
  
  for (i = 1; i <= m; ++i) {
    scanf ("%d%d%d", &op, &a, &b);
    if (op == 1)
      update(1, 1, p2, a, b);
    else
      printf ("%d\n", query (1, 1, p2, a, b));
  }
  
  return 0;
}