Problem p00297 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "climits"
#include "stdio.h"
#define MAX 200000
int star;
int dif;
int box_x[MAX];
int x[MAX];
int y[MAX];
int box_y[MAX];
int blight[MAX];
int box[MAX];
int end[MAX] = {};
int x_max = 0;
int x_min = INT_MAX;
int y_max = 0;
int y_min = INT_MAX;
int child;
int fig = 1;
int RMinQx[MAX * 4];
int RMinQy[MAX * 4];
int RMaxQx[MAX * 4];
int RMaxQy[MAX * 4];
int left[MAX * 4];
int right[MAX * 4];
long long int ans = 0;

void MergeSort(int n[], int _x[], int _y[], int left, int right) {
  int mid;
  if (left == right)
    return;
  mid = (left + right) / 2;
  MergeSort(n, _x, _y, left, mid);
  MergeSort(n, _x, _y, mid + 1, right);
  for (int i = left; i <= mid; i++) {
    box[i] = n[i];
    box_x[i] = _x[i];
    box_y[i] = _y[i];
  }
  for (int i = mid + 1, j = right; i <= right; i++, j--) {
    box[i] = n[j];
    box_x[i] = _x[j];
    box_y[i] = _y[j];
  }
  int a = left, b = right;
  for (int i = left; i <= right; i++) {
    if (box[a] <= box[b]) {
      n[i] = box[a];
      _x[i] = box_x[a];
      _y[i] = box_y[a++];
    } else {
      n[i] = box[b];
      x[i] = box_x[b];
      y[i] = box_y[b--];
    }
  }
}

int max(int a, int b) {
  if (a > b)
    return a;
  return b;
}

int min(int a, int b) {
  if (a < b)
    return a;
  return b;
}

int SegMax(int x[], int a) {
  if (a < child) {
    x[a] = max(SegMax(x, a * 2), SegMax(x, a * 2 + 1));
  }
  return x[a];
}

int SegMin(int x[], int a) {
  if (a < child) {
    x[a] = min(SegMin(x, a * 2), SegMin(x, a * 2 + 1));
  }
  return x[a];
}

int RangeL(int l[], int a) {
  if (a < child) {
    l[a] = min(RangeL(l, a * 2), RangeL(l, a * 2 + 1));
  }
  return l[a];
}

int RangeR(int r[], int a) {
  if (a < child) {
    r[a] = max(RangeR(r, a * 2), RangeR(r, a * 2 + 1));
  }
  return r[a];
}

int RMinQ(int n[], int a, int b, int num) {
  if (a > right[num] || b < left[num])
    return INT_MAX;
  if (a <= left[num] && b >= right[num])
    return n[num];
  return min(RMinQ(n, a, b, num * 2), RMinQ(n, a, b, num * 2 + 1));
}

int RMaxQ(int n[], int a, int b, int num) {
  if (a > right[num] || b < left[num])
    return 0;
  if (a <= left[num] && b >= right[num])
    return n[num];
  return max(RMaxQ(n, a, b, num * 2), RMaxQ(n, a, b, num * 2 + 1));
}

int main() {
  scanf("%d %d", &star, &dif);
  for (int i = 0; i < star; i++) {
    scanf("%d %d %d", &x[i], &y[i], &blight[i]);
  }
  while (1) {
    if (fig >= star) {
      child = fig;
      break;
    }
    fig *= 2;
  }
  MergeSort(blight, x, y, 0, star - 1);
  for (int i = 0; i < star; i++) {
    RMaxQx[child + i] = x[i];
    RMinQx[child + i] = x[i];
    RMaxQy[child + i] = y[i];
    RMinQy[child + i] = y[i];
  }
  for (int i = star; i < child; i++) {
    RMaxQx[child + i] = 0;
    RMinQx[child + i] = INT_MAX;
    RMaxQy[child + i] = 0;
    RMinQy[child + i] = INT_MAX;
  }
  SegMax(RMaxQx, 1);
  SegMax(RMaxQy, 1);
  SegMin(RMinQx, 1);
  SegMin(RMinQy, 1);

  // printf("%d %d %d %d\n", RMaxQx[1], RMaxQy[1], RMinQx[1], RMinQy[1]);

  for (int i = 0; i < child; i++) {
    left[child + i] = i;
    right[child + i] = i;
  }

  RangeR(right, 1);
  RangeL(left, 1);

  // for (int i = 1; i < child; i++) {
  //	printf("%d %d\n", left[i], right[i]);
  // }

  for (int i = 1; i < star; i++) {
    if (blight[i] - blight[0] > dif)
      break;
    end[0] = i;
  }
  for (int i = 1; i < star; i++) {
    for (int j = end[0]; j < star; j++) {
      if (blight[j] - blight[i] > dif)
        break;
      end[i] = j;
    }
    //	printf("%d\n", end[i]);
  }
  for (int i = 0; i < star; i++) {
    /*	printf("%d\n", RMaxQ(RMaxQx, i, end[i], 1));*/
    // printf("%lld\n", (long long int)(RMaxQ(RMaxQx, i, end[i], 1) - (long long
    // int)RMinQ(RMinQx, i, end[i], 1))*((long long int)RMaxQ(RMaxQy, i, end[i],
    // 1) - (long long int)RMinQ(RMinQy, i, end[i], 1)));
    if (ans < (long long int)(RMaxQ(RMaxQx, i, end[i], 1) -
                              (long long int)RMinQ(RMinQx, i, end[i], 1)) *
                  ((long long int)RMaxQ(RMaxQy, i, end[i], 1) -
                   (long long int)RMinQ(RMinQy, i, end[i], 1))) {
      ans = (long long int)(RMaxQ(RMaxQx, i, end[i], 1) -
                            (long long int)RMinQ(RMinQx, i, end[i], 1)) *
            ((long long int)RMaxQ(RMaxQy, i, end[i], 1) -
             (long long int)RMinQ(RMinQy, i, end[i], 1));
    }
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include "climits"
#include "stdio.h"
#define MAX 200000
int star;
int dif;
int box_x[MAX];
int x[MAX];
int y[MAX];
int box_y[MAX];
int blight[MAX];
int box[MAX];
int end[MAX] = {};
int x_max = 0;
int x_min = INT_MAX;
int y_max = 0;
int y_min = INT_MAX;
int child;
int fig = 1;
int RMinQx[MAX * 4];
int RMinQy[MAX * 4];
int RMaxQx[MAX * 4];
int RMaxQy[MAX * 4];
int left[MAX * 4];
int right[MAX * 4];
long long int ans = 0;

void MergeSort(int n[], int _x[], int _y[], int left, int right) {
  int mid;
  if (left == right)
    return;
  mid = (left + right) / 2;
  MergeSort(n, _x, _y, left, mid);
  MergeSort(n, _x, _y, mid + 1, right);
  for (int i = left; i <= mid; i++) {
    box[i] = n[i];
    box_x[i] = _x[i];
    box_y[i] = _y[i];
  }
  for (int i = mid + 1, j = right; i <= right; i++, j--) {
    box[i] = n[j];
    box_x[i] = _x[j];
    box_y[i] = _y[j];
  }
  int a = left, b = right;
  for (int i = left; i <= right; i++) {
    if (box[a] <= box[b]) {
      n[i] = box[a];
      _x[i] = box_x[a];
      _y[i] = box_y[a++];
    } else {
      n[i] = box[b];
      x[i] = box_x[b];
      y[i] = box_y[b--];
    }
  }
}

int max(int a, int b) {
  if (a > b)
    return a;
  return b;
}

int min(int a, int b) {
  if (a < b)
    return a;
  return b;
}

int SegMax(int x[], int a) {
  if (a < child) {
    x[a] = max(SegMax(x, a * 2), SegMax(x, a * 2 + 1));
  }
  return x[a];
}

int SegMin(int x[], int a) {
  if (a < child) {
    x[a] = min(SegMin(x, a * 2), SegMin(x, a * 2 + 1));
  }
  return x[a];
}

int RangeL(int l[], int a) {
  if (a < child) {
    l[a] = min(RangeL(l, a * 2), RangeL(l, a * 2 + 1));
  }
  return l[a];
}

int RangeR(int r[], int a) {
  if (a < child) {
    r[a] = max(RangeR(r, a * 2), RangeR(r, a * 2 + 1));
  }
  return r[a];
}

int RMinQ(int n[], int a, int b, int num) {
  if (a > right[num] || b < left[num])
    return INT_MAX;
  if (a <= left[num] && b >= right[num])
    return n[num];
  return min(RMinQ(n, a, b, num * 2), RMinQ(n, a, b, num * 2 + 1));
}

int RMaxQ(int n[], int a, int b, int num) {
  if (a > right[num] || b < left[num])
    return 0;
  if (a <= left[num] && b >= right[num])
    return n[num];
  return max(RMaxQ(n, a, b, num * 2), RMaxQ(n, a, b, num * 2 + 1));
}

int main() {
  scanf("%d %d", &star, &dif);
  for (int i = 0; i < star; i++) {
    scanf("%d %d %d", &x[i], &y[i], &blight[i]);
  }
  while (1) {
    if (fig >= star) {
      child = fig;
      break;
    }
    fig *= 2;
  }
  MergeSort(blight, x, y, 0, star - 1);
  for (int i = 0; i < star; i++) {
    RMaxQx[child + i] = x[i];
    RMinQx[child + i] = x[i];
    RMaxQy[child + i] = y[i];
    RMinQy[child + i] = y[i];
  }
  for (int i = star; i < child; i++) {
    RMaxQx[child + i] = 0;
    RMinQx[child + i] = INT_MAX;
    RMaxQy[child + i] = 0;
    RMinQy[child + i] = INT_MAX;
  }
  SegMax(RMaxQx, 1);
  SegMax(RMaxQy, 1);
  SegMin(RMinQx, 1);
  SegMin(RMinQy, 1);

  // printf("%d %d %d %d\n", RMaxQx[1], RMaxQy[1], RMinQx[1], RMinQy[1]);

  for (int i = 0; i < child; i++) {
    left[child + i] = i;
    right[child + i] = i;
  }

  RangeR(right, 1);
  RangeL(left, 1);

  // for (int i = 1; i < child; i++) {
  //	printf("%d %d\n", left[i], right[i]);
  // }

  for (int i = 1; i < star; i++) {
    if (blight[i] - blight[0] > dif)
      break;
    end[0] = i;
  }
  for (int i = 1; i < star; i++) {
    for (int j = end[i - 1]; j < star; j++) {
      if (blight[j] - blight[i] > dif)
        break;
      end[i] = j;
    }
    //	printf("%d\n", end[i]);
  }
  for (int i = 0; i < star; i++) {
    /*	printf("%d\n", RMaxQ(RMaxQx, i, end[i], 1));*/
    // printf("%lld\n", (long long int)(RMaxQ(RMaxQx, i, end[i], 1) - (long long
    // int)RMinQ(RMinQx, i, end[i], 1))*((long long int)RMaxQ(RMaxQy, i, end[i],
    // 1) - (long long int)RMinQ(RMinQy, i, end[i], 1)));
    if (ans < (long long int)(RMaxQ(RMaxQx, i, end[i], 1) -
                              (long long int)RMinQ(RMinQx, i, end[i], 1)) *
                  ((long long int)RMaxQ(RMaxQy, i, end[i], 1) -
                   (long long int)RMinQ(RMinQy, i, end[i], 1))) {
      ans = (long long int)(RMaxQ(RMaxQx, i, end[i], 1) -
                            (long long int)RMinQ(RMinQx, i, end[i], 1)) *
            ((long long int)RMaxQ(RMaxQy, i, end[i], 1) -
             (long long int)RMinQ(RMinQy, i, end[i], 1));
    }
  }
  printf("%lld\n", ans);
  return 0;
}