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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  int sx, sy, gx, gy, n, lx, ly;
  set<pair<int, int>> ng;
  cin >> sx >> sy >> gx >> gy >> n;
  rep(i, 0, n) {
    int x, y;
    cin >> x >> y;
    ng.insert(make_pair(x, y));
  }
  cin >> lx >> ly;

  const int dx[] = {0, 1, 1, 0, -1, -1, 0};
  const int dy[2][7] = {{1, 0, -1, -1, -1, 0, 0}, {1, 1, 0, -1, 0, 1, 0}};
  map<tuple<int, int, int>, int> dp;
  priority_queue<pair<int, tuple<int, int, int>>,
                 vector<pair<int, tuple<int, int, int>>>,
                 greater<pair<int, tuple<int, int, int>>>>
      updates;
  dp.insert(make_pair(make_tuple(sx, sy, 0), 0));
  updates.push(make_pair(0, make_tuple(sx, sy, 0)));
  while (!updates.empty()) {
    auto p = updates.top();
    updates.pop();
    const int x = get<0>(p.second), y = get<1>(p.second);
    const int t = get<2>(p.second), c = p.first;
    {
      auto it = dp.find(make_tuple(x, y, t));
      if (it != dp.end() and c > it->second)
        continue;
    }
    if (x == gx and y == gy) {
      cout << c << endl;
      return;
    }
    const int dir = abs(x * y * t) % 6;
    rep(i, 0, 7) {
      const int nx = x + dx[i], ny = y + dy[(x + 256) % 2][i];
      if (abs(nx) > lx or abs(ny) > ly)
        continue;
      const auto next_key = make_tuple(nx, ny, (t + 1) % 6);
      if (ng.find(make_pair(nx, ny)) != ng.end())
        continue;
      if (dp.find(next_key) == dp.end())
        dp.insert(make_pair(next_key, inf));
      auto it = dp.find(next_key);
      int tmp1 = c + (i != dir), &tmp2 = it->second;
      if (tmp1 >= tmp2)
        continue;
      tmp2 = tmp1;
      updates.push(make_pair(tmp2, next_key));
    }
  }
  cout << -1 << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cout << fixed << setprecision(8);
  solve();
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  int sx, sy, gx, gy, n, lx, ly;
  set<pair<int, int>> ng;
  cin >> sx >> sy >> gx >> gy >> n;
  rep(i, 0, n) {
    int x, y;
    cin >> x >> y;
    ng.insert(make_pair(x, y));
  }
  cin >> lx >> ly;

  const int dx[] = {0, 1, 1, 0, -1, -1, 0};
  const int dy[2][7] = {{1, 0, -1, -1, -1, 0, 0}, {1, 1, 0, -1, 0, 1, 0}};
  int ans = inf;
  map<tuple<int, int, int>, int> dp;
  priority_queue<pair<int, tuple<int, int, int>>,
                 vector<pair<int, tuple<int, int, int>>>,
                 greater<pair<int, tuple<int, int, int>>>>
      updates;
  dp.insert(make_pair(make_tuple(sx, sy, 0), 0));
  updates.push(make_pair(0, make_tuple(sx, sy, 0)));
  while (!updates.empty()) {
    auto p = updates.top();
    updates.pop();
    const int x = get<0>(p.second), y = get<1>(p.second);
    const int t = get<2>(p.second), c = p.first;
    {
      auto it = dp.find(make_tuple(x, y, t));
      if (it != dp.end() and c > it->second)
        continue;
    }
    if (x == gx and y == gy) {
      cout << c << endl;
      return;
    }
    const int dir = abs(x * y * t) % 6;
    rep(i, 0, 7) {
      const int nx = x + dx[i], ny = y + dy[(x + 256) % 2][i];
      if (abs(nx) > lx or abs(ny) > ly)
        continue;
      const auto next_key = make_tuple(nx, ny, (t + 1) % 6);
      if (ng.find(make_pair(nx, ny)) != ng.end())
        continue;
      if (dp.find(next_key) == dp.end())
        dp.insert(make_pair(next_key, inf));
      auto it = dp.find(next_key);
      int tmp1 = c + (i != dir), &tmp2 = it->second;
      if (tmp1 > tmp2)
        continue;
      tmp2 = tmp1;
      updates.push(make_pair(tmp2, next_key));
    }
  }
  cout << -1 << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cout << fixed << setprecision(8);
  solve();
  return 0;
}