Problem p00193 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input {
  int n;
  char c;
  char str[1024];
  string s;
  ll l;
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  ll nextLong() {
    scanf("%lld", &l);
    return l;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  string nextLine() {
    getline(cin, s);
    return s;
  }
};
Input in;

int dx1[] = {-1, -1, 0, +1, 0, -1};
int dy1[] = {0, -1, -1, 0, +1, +1};
int dx2[] = {-1, 0, +1, +1, +1, 0};
int dy2[] = {0, -1, -1, 0, +1, +1};

int d[110][110];
bool used[110][110];

bool isOutOfRange(int x, int y, int w, int h) {
  if (x <= 0 || y <= 0 || x > w || y > h)
    return true;
  return false;
}

int main() {
  while (1) {
    int w = in.nextInt(), h = in.nextInt();
    if (w == 0 && h == 0)
      break;
    int s = in.nextInt();
    queue<pii> q;
    rep(i, 110) rep(j, 110) d[i][j] = INF;
    rep(i, s) {
      int a = in.nextInt(), b = in.nextInt();
      q.push(mp(0, a * 1000 + b));
      d[a][b] = 0;
    }

    while (!q.empty()) {
      int x = q.front().se / 1000, y = q.front().se % 1000, c = q.front().fi;
      q.pop();
      rep(i, 6) {
        int nx = x + ((y % 2 == 1) ? dx1[i] : dx2[i]);
        int ny = y + ((y % 2 == 1) ? dy1[i] : dy2[i]);
        if (isOutOfRange(nx, ny, w, h) || d[nx][ny] != INF)
          continue;

        d[nx][ny] = c + 1;
        q.push(mp(c + 1, nx * 1000 + ny));
      }
    }

    int t = in.nextInt(), ans = 0;
    rep(i, t) {
      int a = in.nextInt(), b = in.nextInt(), tmp = d[a][b] == 0 ? 0 : 1;
      q.push(mp(0, a * 1000 + b));
      rep(i, 110) rep(j, 110) used[i][j] = false;
      used[a][b] = true;
      while (!q.empty()) {
        int x = q.front().se / 1000, y = q.front().se % 1000, c = q.front().fi;
        q.pop();
        rep(i, 6) {
          int nx = x + ((y % 2 == 1) ? dx1[i] : dx2[i]);
          int ny = y + ((y % 2 == 1) ? dy1[i] : dy2[i]);
          if (isOutOfRange(nx, ny, w, h) || d[nx][ny] <= c + 1 || used[nx][ny])
            continue;

          tmp++;
          used[nx][ny] = true;
          q.push(mp(c + 1, nx * 1000 + ny));
        }
      }
      ans = max(ans, tmp);
      // cout<<" "<<tmp<<endl;
    }
    printf("%d\\n", ans);

    // rep(i, h + 1){
    // 	rep(j, w + 1){
    // 		if(d[j][i] == INF) printf("   ");
    // 		else if(i & 1) printf("%2d ", d[j][i]);
    // 		else printf(" %2d", d[j][i]);
    // 	}
    // 	printf("\\n");
    // }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input {
  int n;
  char c;
  char str[1024];
  string s;
  ll l;
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  ll nextLong() {
    scanf("%lld", &l);
    return l;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  string nextLine() {
    getline(cin, s);
    return s;
  }
};
Input in;

int dx1[] = {-1, -1, 0, +1, 0, -1};
int dy1[] = {0, -1, -1, 0, +1, +1};
int dx2[] = {-1, 0, +1, +1, +1, 0};
int dy2[] = {0, -1, -1, 0, +1, +1};

int d[110][110];
bool used[110][110];

bool isOutOfRange(int x, int y, int w, int h) {
  if (x <= 0 || y <= 0 || x > w || y > h)
    return true;
  return false;
}

int main() {
  while (1) {
    int w = in.nextInt(), h = in.nextInt();
    if (w == 0 && h == 0)
      break;
    int s = in.nextInt();
    queue<pii> q;
    rep(i, 110) rep(j, 110) d[i][j] = INF;
    rep(i, s) {
      int a = in.nextInt(), b = in.nextInt();
      q.push(mp(0, a * 1000 + b));
      d[a][b] = 0;
    }

    while (!q.empty()) {
      int x = q.front().se / 1000, y = q.front().se % 1000, c = q.front().fi;
      q.pop();
      // cout<<x<<" "<<y<<" "<<c<<endl;
      rep(i, 6) {
        int nx = x + ((y % 2 == 1) ? dx1[i] : dx2[i]);
        int ny = y + ((y % 2 == 1) ? dy1[i] : dy2[i]);
        // cout<<" "<<nx<<" "<<ny<<endl;
        if (isOutOfRange(nx, ny, w, h) || d[nx][ny] < c + 1)
          continue;

        d[nx][ny] = c + 1;
        q.push(mp(c + 1, nx * 1000 + ny));
      }
    }

    int t = in.nextInt(), ans = 0;
    rep(i, t) {
      int a = in.nextInt(), b = in.nextInt(), tmp = 1;
      q.push(mp(0, a * 1000 + b));
      // vector<vector<int> > v(w + 1, vi(h + 1, 0));
      rep(i, w + 1) rep(j, h + 1) used[i][j] = false;
      while (!q.empty()) {
        int x = q.front().se / 1000, y = q.front().se % 1000, c = q.front().fi;
        q.pop();
        rep(i, 6) {
          int nx = x + ((y % 2 == 1) ? dx1[i] : dx2[i]);
          int ny = y + ((y % 2 == 1) ? dy1[i] : dy2[i]);
          if (isOutOfRange(nx, ny, w, h) || d[nx][ny] <= c + 1 ||
              used[nx][ny] /*v[nx][ny] == 1*/)
            continue;

          tmp++;
          // v[nx][ny] = 1;
          used[nx][ny] = true;
          q.push(mp(c + 1, nx * 1000 + ny));
        }
      }
      ans = max(ans, tmp);
    }
    printf("%d\\n", ans);

    // rep(i, h){
    // 	rep(j, w){
    // 		if(j & 1) printf("%2d  ", d[j][i]);
    // 		else printf("  %2d", d[j][i]);
    // 	}
    // 	printf("\\n");
    // }
  }
}