Cod sursa(job #2783294)

Utilizator AlexNicuNicu Alexandru AlexNicu Data 14 octombrie 2021 10:28:50
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.72 kb
#include <fstream>

using namespace std;

ifstream cin ("arbint.in");
ofstream cout ("arbint.out");

#define NMAX 100000
#define INF 1000000001

int aint[NMAX * 4 + 1];

void update1( int poz, int val ) {
    aint[poz] = val;
    while ( poz > 1 ) {
        if (poz % 2 == 0 )
            aint[poz / 2] = max(aint[poz], aint[poz + 1]);
        else
            aint[poz / 2] = max(aint[poz - 1], aint[poz] );
        poz /= 2;
    }
}

void update2(int ind, int poz, int val, int st, int dr ) {
  if ( st == dr )
    aint[ind] = val;
  else {
    if ( (st + dr) / 2 >= poz ) {
      update2(ind * 2, poz, val, st, (st + dr) / 2 );
    }
    else {
      update2(ind * 2 + 1, poz, val, (st + dr) / 2 + 1, dr );
    }
    aint[ind] = max(aint[ind * 2], aint[ind * 2 + 1]);
  }
}

int query(int ind, int csta, int cdra, int cstq, int cdrq ) {
    if ( cstq <= csta && cdra <= cdrq ) {
        return aint[ind];
    } else {
        int aux = -INF;
        //comparam cu mijlocul
        if ( cstq <= (csta + cdra) / 2 ) {
            aux = query(ind * 2, csta, (csta + cdra) / 2, cstq, cdrq);
        }
        if ( cdrq > (csta + cdra) / 2 ) {
            aux = max(aux, query(ind * 2 + 1, (csta + cdra) / 2 + 1, cdra, cstq, cdrq ));
        }
        return aux;
    }
}

int main() {
    int n, q, i, x, a, b, tip;
    int p;
    cin >> n >> q;
    p = 1;
    while ( p <= n ) {
      p = p * 2;
    }
    for ( i = 0; i < n; i++ ) {
      cin >> x;
      update1(p + i, x);
    }
    for ( i = 0; i < q; i++ ) {
      cin >> tip >> a >> b;
      if ( tip == 0 ) {
        cout << query(1, 1, p, a, b) << "\n";
      }
      else
        update1(p + a - 1, b);
    }
    return 0;
}