Cod sursa(job #1777019)

Utilizator cella.florescuCella Florescu cella.florescu Data 12 octombrie 2016 00:06:08
Problema Arbori de intervale Scor 100
Compilator c Status done
Runda cerculdeinfo-lectia2-arborideintervale Marime 1.02 kb
#include <stdio.h>
#include <stdlib.h>
#define max(x, y) x>y?x:y

int arb[262145], a, b;

void update(int poz, int l, int r){
  if(l==r)
    arb[poz]=b;
  else{
    int m=(l+r)>>1;
    if(a<=m)
      update(2*poz, l, m);
    else
      update(2*poz+1, m+1, r);
    arb[poz]=max(arb[2*poz], arb[2*poz+1]);
  }
}

int query(int poz, int l, int r){
  if(a<=l && r<=b)
    return arb[poz];
  int m=(l+r)>>1, max1=0, max2=0;
  if(a<=m)
    max1=query(2*poz, l, m);
  if(m<b)
    max2=query(2*poz+1, m+1, r);
  return max(max1, max2);
}

int main()
{
    FILE *fin, *fout;
    int n, m, i, op;
    fin=fopen("arbint.in", "r");
    fscanf(fin, "%d%d", &n, &m);
    for(a=1; a<=n; a++){
      fscanf(fin, "%d", &b);
      update(1, 1, n);
    }
    fout=fopen("arbint.out", "w");
    for(i=0; i<m; i++){
      fscanf(fin, "%d%d%d", &op, &a, &b);
      if(op==0)
        fprintf(fout, "%d\n", query(1, 1, n));
      else
        update(1, 1, n);
    }
    fclose(fin);
    fclose(fout);
    return 0;
}