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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define pb push_back
typedef pair<int, int> pi;
#define MAX 1000000
int isprime[1145141];
int table[2000][2000];
pi S[1145141];
int D[2000][2000];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
void eratos(int n) {
  rep(i, n + 1) isprime[i] = true;
  isprime[0] = isprime[1] = false;
  for (int i = 2; i * i <= n; i++) {
    if (isprime[i]) {
      int j = i + i;
      while (j <= n) {
        isprime[j] = false;
        j = j + i;
      }
    }
  }
}
int main() {
  eratos(MAX);
  // table turkuru
  memset(table, 0, sizeof(table));
  int now = 2;
  int dist = 1;
  int cnt = 0;
  int dir = 0;
  int y = 1000;
  int x = 1000;
  table[y][x] = 1;
  S[1] = pi(y, x);
  while (now <= MAX) {
    if (cnt < dist) {
      y += dy[dir];
      x += dx[dir];
      table[y][x] = now;
      S[now] = pi(y, x);
      cnt++;
      now++;
    } else {
      dir = (dir + 1) % 4;
      if (dir == 2 || dir == 0)
        dist++;
      y += dy[dir];
      x += dx[dir];
      table[y][x] = now;
      S[now] = pi(y, x);
      cnt = 1;
      now++;
    }
  }
  /*rep(i,2000)
    {
      rep(j,2000)
        {
          printf("%d ", table[i][j]);
        }
            printf("\\n");
            }*/
  // cout << table[1000][1000]<< endl;
  // cout << table[1001][1001] << endl;
  int M, N;
  int ans;
  int pp;
  int ey[3] = {1, 1, 1};
  int ex[3] = {-1, 0, 1};
  // printf("%d", isprime[table[S[947].first][S[947].second]]);
  while (true) {
    cin >> M >> N;
    if (M == 0 && N == 0)
      break;
    memset(D, -1, sizeof(D));
    queue<pi> Q;
    queue<int> R;
    ans = 0;
    pp = 0;
    Q.push(pi(S[N].first, S[N].second));
    // printf("%d %d\\n", S[N].first,S[N].second);
    R.push(0);
    while (!R.empty()) {
      int ty = Q.front().first;
      int tx = Q.front().second;

      int point = R.front();
      Q.pop();
      R.pop();

      if (isprime[table[ty][tx]]) {
        point++;
      }
      if (ans <= point) {
        if (ans < point && isprime[table[ty][tx]])
          pp = table[ty][tx];
        else if (ans == point && isprime[table[ty][tx]])
          pp = max(table[ty][tx], pp);
        ans = point;
      }

      // printf("ty=%d, tx=%d, point=%d\\n",ty,tx,point);

      if (point <= D[ty][tx])
        continue;
      D[ty][tx] = point;
      // ans = max(ans,point);
      rep(r, 3) {
        int y = ty + ey[r];
        int x = tx + ex[r];

        if (table[y][x] == 0)
          continue;
        if (table[y][x] > M)
          continue;
        Q.push(pi(y, x));
        R.push(point);
      }
    }
    printf("%d %d\\n", ans, pp);
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define pb push_back
typedef pair<int, int> pi;
#define MAX 1000000
int isprime[1145141];
int table[2000][2000];
pi S[1145141];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
void eratos(int n) {
  rep(i, n + 1) isprime[i] = true;
  isprime[0] = isprime[1] = false;
  for (int i = 2; i * i <= n; i++) {
    if (isprime[i]) {
      int j = i + i;
      while (j <= n) {
        isprime[j] = false;
        j = j + i;
      }
    }
  }
}
int main() {
  eratos(MAX);
  // table turkuru
  memset(table, 0, sizeof(table));
  int now = 2;
  int dist = 1;
  int cnt = 0;
  int dir = 0;
  int y = 1000;
  int x = 1000;
  table[y][x] = 1;
  S[1] = pi(y, x);
  while (now <= MAX) {
    if (cnt < dist) {
      y += dy[dir];
      x += dx[dir];
      table[y][x] = now;
      S[now] = pi(y, x);
      cnt++;
      now++;
    } else {
      dir = (dir + 1) % 4;
      if (dir == 2 || dir == 0)
        dist++;
      y += dy[dir];
      x += dx[dir];
      table[y][x] = now;
      S[now] = pi(y, x);
      cnt = 1;
      now++;
    }
  }
  /*rep(i,2000)
    {
      rep(j,2000)
        {
          printf("%d ", table[i][j]);
        }
            printf("\\n");
            }*/
  // cout << table[1000][1000]<< endl;
  // cout << table[1001][1001] << endl;
  int M, N;
  int ans;
  int pp;
  int ey[3] = {1, 1, 1};
  int ex[3] = {-1, 0, 1};
  while (true) {
    cin >> M >> N;
    if (M == 0 && N == 0)
      break;
    queue<pi> Q;
    queue<int> R;
    ans = 0;
    pp = 0;
    Q.push(pi(S[N].first, S[N].second));
    R.push(0);
    while (!R.empty()) {
      int ty = Q.front().first;
      int tx = Q.front().second;
      int point = R.front();
      Q.pop();
      R.pop();
      if (isprime[table[ty][tx]]) {
        point++;
      }
      if (ans <= point) {
        if (ans < point)
          pp = table[ty][tx];
        else
          pp = max(table[ty][tx], pp);
        ans = point;
      }
      // ans = max(ans,point);
      rep(r, 3) {
        int y = ty + ey[r];
        int x = tx + ex[r];

        if (table[y][x] == 0)
          continue;
        if (table[y][x] > M)
          continue;
        Q.push(pi(y, x));
        R.push(point);
      }
    }
    printf("%d %d\\n", ans, pp);
  }
  return 0;
}