Cod sursa(job #2816358)

Utilizator teodortatomirTeodor Tatomir teodortatomir Data 11 decembrie 2021 11:58:36
Problema Arbori de intervale Scor 50
Compilator c-64 Status done
Runda Arhiva educationala Marime 2.01 kb
// am recitit mesajul cu sugestii si am obsevrat ca mi-ati sugerat sa las spatii pe verticala si orizontala, de acum voi incerca sa imi aerisesc codul
#include <stdio.h>
#include <stdlib.h>
#define MAXX 100000

int v[MAXX+1], a[MAXX*2+1];
void build(int st, int dr, int nod){
  int mij, cst, cdr, maxx;

  mij = (st + dr) / 2;
  cst = nod + 1;
  cdr = nod + 2 * (mij - st + 1);

  if(st == dr)
    a[nod] = v[st - 1];
  else {
    build(st, mij, cst);
    build(mij + 1, dr, cdr);
    maxx = a[cst];
    if(a[cdr] > maxx)
      maxx = a[cdr];
    a[nod] = maxx;
  }
}
void update(int st, int dr, int nod, int p, int x) {
  int cst, cdr, mij, maxx;

  if(st == dr) {
    a[nod] = x;
    return ;
  }

  mij = (st + dr) / 2;
  cst = nod + 1;
  cdr = nod + 2 * (mij - st + 1);
  if(p <= mij)
    update(st, mij, cst, p, x);
  else
    update(mij+1, dr, cdr, p, x);
  maxx = a[cst];
  if(a[cdr] > maxx)
    maxx = a[cdr];
  a[nod] = maxx;
}
int gmaxx(int x, int y, int st, int dr, int nod) {
  int cst, cdr, mij, maxx, modif;

  if( st == x && dr == y)
    return a[nod];

  mij = (st + dr) / 2;
  cst = nod + 1;
  cdr = nod + 2 * (mij - st + 1);
  maxx = 0;
  if(x <= mij) {
    modif = mij;
    if(y < modif)
      modif = y;
    if(gmaxx(x, modif, st, mij, cst) > maxx)
      maxx = gmaxx(x, modif, st, mij, cst);
  }
  if(y > mij) {
    modif = mij + 1;
    if(x > modif)
      modif =  x;
    if(gmaxx(modif, y, mij + 1, dr, cdr) > maxx)
      maxx = gmaxx(modif, y, mij + 1, dr, cdr);
  }

  return maxx;
}
int main(){
  FILE *fin,*fout;
  int n, m, i, x, y, cer;

  fin=fopen("arbint.in", "r");
  fout=fopen("arbint.out", "w");

  fscanf(fin, "%d%d", &n, &m);
  for(i = 0; i < n; i++)
    fscanf(fin, "%d", &v[i]);

  build(1, n, 0);

  for(i = 0; i<m; i++) {
    fscanf(fin, "%d%d%d", &cer, &x, &y);
    if(cer == 0)
      fprintf(fout, "%d\n", gmaxx(x, y, 1, n, 0));
    else
      update(1, n, 0, x, y);
  }

  fclose(fin);
  fclose(fout);
  return 0;
}