Cod sursa(job #2456245)

Utilizator Tudor06MusatTudor Tudor06 Data 13 septembrie 2019 22:38:54
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.43 kb
#include <iostream>
#include <stdio.h>

using namespace std;

int val, poz;
int aint[4 * 100000];

void update( int st, int dr, int i ) {
  if ( st == dr ) {
    aint[i] = val;
  } else {
    int mij = ( st + dr ) / 2;
    if ( poz <= mij ) {
      update( st, mij, i * 2 + 1 );
    } else {
      update( mij + 1, dr, i * 2 + 2 );
    }
    aint[i] = max( aint[i * 2 + 1], aint[i * 2 + 2] );
  }
}

int query( int a, int b, int st, int dr, int i ) {
  if ( a == st && b == dr )
    return aint[i];
  else {
    int mij = ( st + dr ) / 2;
    if ( b <= mij ) {
      return query( a, b, st, mij, i * 2 + 1 );
    } else if ( a > mij ) {
      return query( a, b, mij + 1, dr, i * 2 + 2 );
    } else {
      return max( query( a, mij, st, mij, i * 2 + 1 ),
                  query( mij + 1, b, mij + 1, dr, i * 2 + 2 ) );
    }
  }
}

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