Cod sursa(job #1503550)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 16 octombrie 2015 14:31:14
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
#include <stdio.h>
#include <ctype.h>

#define MAX_N 100000
#define BUFFSIZE 65536

char buff[BUFFSIZE];
int pos = BUFFSIZE;

inline char GetChar(FILE *f) {
  if ( pos == BUFFSIZE ) {
    fread( buff, 1, BUFFSIZE, f );
    pos = 0;
  }
  return buff[ pos++ ];
}

inline int ReadInt(FILE *f) {
  register int q = 0;
  char c;

  do {
    c = GetChar(f);
  } while ( !isdigit(c) );
  do {
    q = (10 * q) + (c - '0');
    c = GetChar(f);
  } while ( isdigit(c) );
  return q;
}

int T[2 * MAX_N];
int n;

inline int MAX(int A, int B) {
  return A > B ? A : B;
}

void aintSet(int x, int key) {
  int q;

  x += n;
  T[x] = key;
  while ( x > 1 ) {
    q = x >> 1;
    T[q] = MAX( T[x], T[x ^ 1] );
    x = q;
  }
}

int aintQuery(int x, int y) {
  int q = -1;

  x += n;
  y += n;
  while ( x < y ) {
    if ( x & 1 ) {
      q = MAX( q, T[x] );
      x++;
    }
    if ( y & 1 ) {
      y--;
      q = MAX( q, T[y] );
    }
    x >>= 1;
    y >>= 1;
  }
  return q;
}

int main(void) {
  FILE *fin = fopen("arbint.in", "r");
  FILE *fout = fopen("arbint.out", "w");
  int m;
  int opType, x, y;

  n = ReadInt(fin); m = ReadInt(fin);
  for ( int i = 0; i < n; i++ ) {
    T[i + n] = ReadInt(fin);
  }

  for ( int i = n - 1; i > 0; i-- ) {
    T[i] = MAX( T[i << 1], T[(i << 1) | 1] );
  }

  for ( int i = 0; i < m; i++ ) {
    opType = ReadInt(fin); x = ReadInt(fin); y = ReadInt(fin);
    if ( opType == 0 ) {
      fprintf( fout, "%d\n", aintQuery( x - 1, y ) );
    } else {
      aintSet( x - 1, y );
    }
  }
  fclose(fin);
  fclose(fout);
  return 0;
}