Cod sursa(job #1690600)

Utilizator mateicosCostescu Matei mateicos Data 15 aprilie 2016 12:44:01
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include <cstdio>

using namespace std;

int a[400000], max;

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

void querry(int nod, int st, int dr,int c, int b){
  if(c <= st && dr <= b){
    if (a[nod] > max){
        max = a[nod];
    }
    return ;
  }
  int mid = (st + dr) / 2;
  if(c <= mid)
    querry(2 * nod, st, mid, c,  b);
  if(b > mid)
    querry(2 * nod + 1, mid + 1, dr, c, b);
}

int main()
{
    freopen("arbint.in","r",stdin);
    freopen("arbint.out","w",stdout);
    int n, m, i, k, op, c, b;
    scanf("%d%d", &n, &m);
    for(i = 1;i <=n;i++){
      scanf("%d", &k);
      update(1, 1, n, i, k);
    }
    for(i = 1;i <= m;i++){
      scanf("%d%d%d", &op, &c, &b);
      if(op == 0){
        max = 0;
        querry(1, 1, n, c, b);
        printf("%d\n", max);
      }
      else{
        update(1, 1, n, c, b);
      }
    }
    return 0;
}