Problem p00441 (C++) — buggy submission, verdict: Runtime Error

// #include "stdio.h"
//
// int N;
// int flag[100000] = {};
// int ans;
//
// int main() {
//
//	scanf("%d", &N);
//	while (N != 0) {
//		ans = 0;
//		for (int i = 0; i < N; i++) {
//			scanf("%d", &flag[i]);
//			if (i % 2 == 1) {
//				for (int j = i - 1; j >= 0; j--) {
//					if (flag[j] == flag[i])break;
//					flag[j] = flag[i];
//				}
//			}
//		}
//		for (int i = 0; i < N; i++) {
//			if (flag[i] == 0)ans++;
//		}
//		printf("%d\\n", ans);
//		scanf("%d", &N);
//	}
//
//	return 0;
// }

#include "stdio.h"

int N;
int x[3000];
int xbox[3000];
int y[3000];
int ybox[3000];
int place[3000];
int ans = 0;
int box[3000];
int cal;
bool flag[5001][5001] = {};

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

int main() {
  scanf("%d", &N);
  while (N != 0) {
    ans = 0;
    for (int i = 0; i < N; i++) {
      scanf("%d %d", &x[i], &y[i]);
      flag[x[i]][y[i]] = true;
      place[i] = x[i] * 5001.0 + y[i];
    }
    MergeSort(place, 0, N - 1);
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        if (x[j] + y[j] - y[i] >= 0 && x[j] + y[j] - y[i] <= 5000 &&
            y[j] - x[j] + x[i] >= 0 && y[j] - x[j] + x[i] <= 5000 &&
            x[i] + y[j] - y[i] >= 0 && x[i] + y[j] - y[i] <= 5000 &&
            y[i] - x[j] + x[i] >= 0 && y[i] - x[j] + x[i] <= 5000) {
          if (flag[x[j] + y[j] - y[i]][y[j] - x[j] + x[i]] &&
              flag[x[i] + y[j] - y[i]][y[i] - x[j] + x[i]]) {
            //	printf("%d %d  %d %d  %d %d  %d %d\\n", x[i], y[i], x[j], y[j],
            //x[j] + y[j] - y[i], y[j] - x[j] + x[i], x[i] + y[j] - y[i], y[i] -
            //x[j] + x[i]); 	printf("%d\\n", (x[j] - x[i]) * (x[j] - x[i]) + (y[j]
            //- y[i]) * (y[j] - y[i]));
            if (ans <
                (x[j] - x[i]) * (x[j] - x[i]) + (y[j] - y[i]) * (y[j] - y[i]))
              ans =
                  (x[j] - x[i]) * (x[j] - x[i]) + (y[j] - y[i]) * (y[j] - y[i]);
          }
        }
      }
    }
    printf("%d\\n", ans);
    scanf("%d", &N);
  }
  return 0;
}

Error: 0

Fixed submission:

#include "stdio.h"

int N;
int x[3000];
int xbox[3000];
int y[3000];
int ybox[3000];
int place[3000];
int ans = 0;
int box[3000];
int cal;
bool flag[5001][5001] = {};

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

int main() {
  scanf("%d", &N);
  while (N != 0) {
    for (int i = 0; i < N; i++) {
      scanf("%d %d", &x[i], &y[i]);
      flag[x[i]][y[i]] = true;
      place[i] = x[i] * 5001.0 + y[i];
    }
    MergeSort(place, 0, N - 1);
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        if (flag[x[j] + y[j] - y[i]][y[j] - x[j] + x[i]] &&
            flag[x[i] + y[j] - y[i]][y[i] - x[j] + x[i]]) {
          //	printf("%d %d  %d %d  %d %d  %d %d\\n", x[i], y[i], x[j], y[j],
          //x[j] + y[j] - y[i], y[j] - x[j] + x[i], x[i] + y[j] - y[i], y[i] -
          //x[j] + x[i]); 	printf("%d\\n", (x[j] - x[i]) * (x[j] - x[i]) + (y[j] -
          //y[i]) * (y[j] - y[i]));
          if (ans <
              (x[j] - x[i]) * (x[j] - x[i]) + (y[j] - y[i]) * (y[j] - y[i]))
            ans = (x[j] - x[i]) * (x[j] - x[i]) + (y[j] - y[i]) * (y[j] - y[i]);
        }
      }
    }
    printf("%d\\n", ans);
    scanf("%d", &N);
  }
  return 0;
}