Cod sursa(job #2082674)

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

#define MAX_N 100000

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

#define fastcall __attribute__((optimize("-O3")))
#define inline __inline__ __attribute__((always_inline))


#include <ctype.h>
const int MAX_BUFF = 65536 * 32;
int pos = 0;
char buff[MAX_BUFF];
char c;
int ptr = 0;
int d[10], size, q;

inline fastcall void put(int x) {
  size = 0;
  if (x == 0) {
    d[size++] = 0;
  }
  while (x) {
    q = x / 10;
    d[size++] = x - (q << 3) - (q << 1);
    x = q;
  }
  while (size) {
    buff[ptr++] = d[--size] + '0';
  }
  buff[ptr++] = '\n';
}

inline fastcall void fscanf(int &x) {
  x = 0;
  do {
    c = buff[pos++];
  } while (!isdigit(c));
  do {
    x = (x << 3) + (x << 1) + c - '0';
    c = buff[pos++];
  } 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) {
  int i;
  FILE *f = fopen("arbint.in", "r");
  fread(buff, 1, MAX_BUFF, f);
  fscanf(N);
  fscanf(Q);
  for (i = 1; i <= N; i++) {
    fscanf(tree[i + N]);
  }
  for (i = N; i >= 1; i--) {
    tree[i] = MAX(tree[i << 1], tree[(i << 1) | 1]);
  }

  int task, a, b;
  while (Q) {
    fscanf(task);
    fscanf(a);
    fscanf(b);
    if (task == 0) {
      put(query(a, b + 1));
    } else {
      update(a, b);
    }
    Q--;
  }

  freopen("arbint.out", "w", stdout);
  fwrite(buff, 1, ptr, stdout);
  return 0;
}