Problem p00441 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

#define sint(i) scanf("%d", &i);
#define sintt(i, j) scanf("%d%d", &i, &j);
#define sinttt(i, j, k) scanf("%d%d%d", &i, &j, &k);
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

char masu[5001][5001];
int main() {
  while (1) {
    int n;
    sint(n);
    // if(n==0)break;
    rep(i, 5001) {
      rep(j, 5001) { masu[i][j] = -1; }
    }

    int nx[3001];
    int ny[3001];

    rep(i, n) {
      sintt(nx[i], ny[i]);
      masu[nx[i]][ny[i]] = 1;
    }

    /*
    rep(i,10){
            rep(j,10){
                    printf("%2d",masu[j][9-(i)]);
            }puts("");
    }*/

    int maxi = 0;
    rep(i, n) {
      rep(j, n - i) {
        if (i == j + i) {
          continue;
        }
        int dx = nx[i] - nx[j + i];
        int dy = ny[i] - ny[j + i];

        int a[8];
        a[0] = nx[i] + dy * -1;
        a[1] = ny[i] + dx;
        a[2] = nx[j + i] + dy * -1;
        a[3] = ny[j + i] + dx;
        a[4] = nx[i] + dy;
        a[5] = ny[i] + dx * -1;
        a[6] = nx[j + i] + dy;
        a[7] = ny[j + i] + dx * -1;

        int flg1 = 0, flg2 = 0;
        rep(k, 4) {
          if (a[k] < 0 || a[k] > 5000) {
            flg1 = 1;
          }
        }
        rep(k, 4) {
          if (a[k + 4] < 0 || a[k + 4] > 5000) {
            flg2 = 1;
          }
        } /*
         rep(k,8){
                 printf("%6d",a[k]);
         }
         printf(" 1==%d 2==%d\n",flg1,flg2);
         */

        if ((flg1 == 0 && masu[nx[i] + dy * -1][ny[i] + dx] == 1 &&
             masu[nx[j + i] + dy * -1][ny[j + i] + dx] == 1) ||
            (flg2 == 0 && masu[nx[i] + dy][ny[i] + dx * -1] == 1 &&
             masu[nx[j + i] + dy][ny[j + i] + dx * -1] == 1)) {
          maxi = max(maxi, dx * dx + dy * dy);
        }
      }
    }
    printf("%d\n", maxi);
  }
}

/*
4
0 5000
5000 0
0 0
5000 5000

*/

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

#define sint(i) scanf("%d", &i);
#define sintt(i, j) scanf("%d%d", &i, &j);
#define sinttt(i, j, k) scanf("%d%d%d", &i, &j, &k);
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

char masu[5001][5001];
int main() {
  while (1) {
    int n;
    sint(n);
    if (n == 0)
      break;
    rep(i, 5001) {
      rep(j, 5001) { masu[i][j] = -1; }
    }

    int nx[3001];
    int ny[3001];

    rep(i, n) {
      sintt(nx[i], ny[i]);
      masu[nx[i]][ny[i]] = 1;
    }

    /*
    rep(i,10){
            rep(j,10){
                    printf("%2d",masu[j][9-(i)]);
            }puts("");
    }*/

    int maxi = 0;
    rep(i, n) {
      rep(j, n - i) {
        if (i == j + i) {
          continue;
        }
        int dx = nx[i] - nx[j + i];
        int dy = ny[i] - ny[j + i];

        int a[8];
        a[0] = nx[i] + dy * -1;
        a[1] = ny[i] + dx;
        a[2] = nx[j + i] + dy * -1;
        a[3] = ny[j + i] + dx;
        a[4] = nx[i] + dy;
        a[5] = ny[i] + dx * -1;
        a[6] = nx[j + i] + dy;
        a[7] = ny[j + i] + dx * -1;

        int flg1 = 0, flg2 = 0;
        rep(k, 4) {
          if (a[k] < 0 || a[k] > 5000) {
            flg1 = 1;
          }
        }
        rep(k, 4) {
          if (a[k + 4] < 0 || a[k + 4] > 5000) {
            flg2 = 1;
          }
        } /*
         rep(k,8){
                 printf("%6d",a[k]);
         }
         printf(" 1==%d 2==%d\n",flg1,flg2);
         */

        if ((flg1 == 0 && masu[nx[i] + dy * -1][ny[i] + dx] == 1 &&
             masu[nx[j + i] + dy * -1][ny[j + i] + dx] == 1) ||
            (flg2 == 0 && masu[nx[i] + dy][ny[i] + dx * -1] == 1 &&
             masu[nx[j + i] + dy][ny[j + i] + dx * -1] == 1)) {
          maxi = max(maxi, dx * dx + dy * dy);
        }
      }
    }
    printf("%d\n", maxi);
  }
}

/*
4
0 5000
5000 0
0 0
5000 5000

*/