Cod sursa(job #2082659)

Utilizator stoianmihailStoian Mihail stoianmihail Data 6 decembrie 2017 17:41:10
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.56 kb
#include <stdio.h>

#define MAX_N 100000

int N, Q;
int tree[MAX_N * 2 + 1];

#include <ctype.h>
const int MAX_BUFF = 65536 * 4;
int pos = MAX_BUFF;
char buff[MAX_BUFF];
char c;

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

void fscanf(int &x, FILE *f) {
  x = 0;
  do {
    c = getChar(f);
  } while (!isdigit(c));
  do {
    x = (x << 3) + (x << 1) + c - '0';
    c = getChar(f);
  } while (isdigit(c));
}

inline int MAX(int X, int Y) {
  return X > Y ? X : Y;
}

void update(int x, int val) {
  x += N;
  tree[x] = val;
  while (x > 1) {
    tree[x >> 1] = MAX(tree[x], tree[x ^ 1]);
    x >>= 1;
  }
}

int query(int a, int b) {
  int ans = 0;
  a += N;
  b += N;
  while (a < b) {
    if (a & 1) {
      ans = MAX(ans, tree[a++]);
    }
    if (b & 1) {
      ans = MAX(ans, tree[--b]);
    }
    a >>= 1;
    b >>= 1;
  }
  return ans;
}

int main(void) {
  FILE *f = fopen("arbint.in", "r");

  int i;
  fscanf(N, f);
  fscanf(Q, f);
  for (i = 1; i <= N; i++) {
    fscanf(tree[i + N], f);
  }
  for (i = N; i >= 1; i--) {
    tree[i] = MAX(tree[i << 1], tree[(i << 1) | 1]);
  }

  int task, a, b;
  freopen("arbint.out", "w", stdout);
  while (Q) {
    fscanf(task, f);
    fscanf(a, f);
    fscanf(b, f);
    if (task == 0) {
      fprintf(stdout, "%d\n", query(a, b + 1));
    } else {
      update(a, b);
    }
    Q--;
  }
  freopen("chimic.out", "w", stdout);
  fprintf(stdout, "%lld\n", tree[1]);
  return 0;
}