Cod sursa(job #2982927)

Utilizator Cezar_RusuCezar Rusu Cezar_Rusu Data 21 februarie 2023 09:58:45
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.53 kb
#include <cstdio>

#define NMAX 10002

int V[NMAX];
int n, m;
int A[NMAX*4];

void build(int, int, int);
void update(int, int, int, int, int);
int query(int, int, int, int, int);

int main() { int i;
  std::FILE *input, *output; input = std::fopen("arbint.in", "r"), output = std::fopen("arbint.out", "w");

  std::fscanf(input, "%d%d", &n, &m);
  for (i = 1; i <= n; ++i) std::fscanf(input, "%d", &V[i]);
  build(1, 1, n);

  int tip, poz, val;
  for (i = 1; i <= m; ++i) {
    std::fscanf(input, "%d%d%d", &tip, &poz, &val);
    if (tip == 1) update(1, 1, n, poz, val);
    else std::fprintf(output, "%d\n", query(1, 1, n, poz, val));
  }

  std::fclose(output);
  return 0;
}

void build(int nod, int st, int dr) {
  if (st == dr) { A[nod] = V[st]; return; }
  int mij = (st+dr)/2;
  build(nod*2, st, mij);
  build(nod*2+1, mij+1, dr);
  A[nod] = (A[nod*2] > A[nod*2+1]) ? A[nod*2] : A[nod*2+1];
}
void update(int nod, int st, int dr, int poz, int val) {
  if (st == dr) { A[nod] = val; return; }
  int mij = (st+dr)/2;
  if (mij >= poz) update(nod*2, st, mij, poz, val);
  else update(nod*2+1, mij+1, dr, poz, val);
  A[nod] = (A[nod*2] > A[nod*2+1]) ? A[nod*2] : A[nod*2+1];
}
int query(int nod, int st, int dr, int L, int R) {
  if (st >= L && dr <= R) return A[nod];
  int mij = (st+dr)/2, rez, rezmax = 0;
  if (mij >= L) rez = query(nod*2, st, mij, L, R), rezmax = (rez > rezmax) ? rez : rezmax;
  if (R > mij) rez = query(nod*2+1, mij+1, dr, L, R), rezmax = (rez > rezmax) ? rez : rezmax;
  return rezmax;
}