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

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2646"

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

#define call_from_test
#ifndef call_from_test
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
#endif
// BEGIN CUT HERE
struct FastIO {
  FastIO() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} fastio_beet;
// END CUT HERE
#ifndef call_from_test
signed main() { return 0; }
#endif

#ifndef call_from_test
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
#endif
// BEGIN CUT HERE
template <typename F> struct FixPoint : F {
  FixPoint(F &&f) : F(forward<F>(f)) {}
  template <typename... Args> decltype(auto) operator()(Args &&...args) const {
    return F::operator()(*this, forward<Args>(args)...);
  }
};
template <typename F> inline decltype(auto) MFP(F &&f) {
  return FixPoint<F>{forward<F>(f)};
}
// END CUT HERE
#ifndef call_from_test
// INSERT ABOVE HERE
signed main() {
  int h, w;
  cin >> h >> w;
  vector<string> st(h);
  for (int i = 0; i < h; i++)
    cin >> st[i];

  vector<vector<int>> used(h, vector<int>(w, 0));

  auto dfs = MFP([&](auto dfs, int y, int x) -> void {
    used[y][x] = 1;
    if (y > 0 && st[y - 1][x] != '#' && !used[y - 1][x])
      dfs(y - 1, x);
    if (y + 1 < h && st[y + 1][x] != '#' && !used[y + 1][x])
      dfs(y + 1, x);
    if (x > 0 && st[y][x - 1] != '#' && !used[y][x - 1])
      dfs(y, x - 1);
    if (x + 1 < w && st[y][x + 1] != '#' && !used[y][x + 1])
      dfs(y, x + 1);
  });

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (st[i][j] == 's')
        dfs(i, j);

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (st[i][j] == 'g')
        cout << (used[i][j] ? "Yes" : "No") << endl;
  return 0;
}
/*
  https://atcoder.jp/contests/atc001/tasks/dfs_a
*/
#endif

#ifndef call_from_test
#include <bits/stdc++.h>
using namespace std;
#endif
// BEGIN CUT HERE
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T, typename U, typename H = hash<T>>
using gmap = cc_hash_table<T, U, H>;
// END CUT HERE
#ifndef call_from_test
// INSERT ABOVE HERE
signed AGC023_A() {
  using ll = long long;
  int n;
  scanf("%d", &n);
  vector<ll> as(n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &as[i]);

  gmap<ll, ll> mp;
  ll ans = 0, sum = 0;
  mp[sum]++;
  for (int i = 0; i < n; i++) {
    sum += as[i];
    ans += mp[sum];
    mp[sum]++;
  }

  printf("%lld\\n", ans);
  return 0;
}
/*
  verified on 2019/12/09
  https://atcoder.jp/contests/agc023/tasks/agc023_a
*/

signed main() {
  AGC023_A();
  return 0;
}
#endif

#undef call_from_test

template <typename T> struct chash {
  uint64_t operator()(T x) const {
    uint64_t y(x);
    y += 0x9e3779b97f4a7c15;
    y = (y ^ (y >> 30)) * 0xbf58476d1ce4e5b9;
    y = (y ^ (y >> 27)) * 0x94d049bb133111eb;
    return y ^ (y >> 31);
  }
};

signed main() {
  using ll = long long;

  int n, m;
  cin >> n >> m;

  m++;
  vector<int> as(m), bs(m);
  for (int i = 0; i < m; i++)
    cin >> as[i];
  for (int i = 1; i < m; i++)
    cin >> bs[i];

  vector<ll> hs;
  MFP([&](auto dfs, int l, int r) -> void {
    int x = lower_bound(as.begin(), as.end(), r) - as.begin();
    if (as[x - 1] <= l && r <= as[x])
      return;
    hs.emplace_back(((ll)l << 31) | r);
    int m = (l + r) >> 1;
    dfs(l, m);
    dfs(m, r);
  })
  (0, (1 << n));

  sort(hs.begin(), hs.end());
  hs.erase(unique(hs.begin(), hs.end()), hs.end());

  gmap<ll, int, chash<ll>> idx;
  for (int i = 0; i < (int)hs.size(); i++)
    idx[hs[i]] = i;

  vector<vector<int>> dp(n + 1, vector<int>(hs.size(), -1));

  int ans = (1 << n) - MFP([&](auto dfs, int l, int r, int d, int k) -> int {
              int x = lower_bound(as.begin(), as.end(), r) - as.begin();
              if (as[x - 1] <= l && r <= as[x]) {
                int v = bs[x], tmp = 0;
                tmp += (k == v);
                tmp += (r - l) >> (n - v + 1);
                return tmp;
              }

              if (~dp[k][idx[((ll)l << 31) | r]])
                return dp[k][idx[((ll)l << 31) | r]];
              int &res = (dp[k][idx[((ll)l << 31) | r]] = 0);

              int m = (l + r) >> 1;
              res = max(res, dfs(l, m, d + 1, d) + dfs(m, r, d + 1, k));
              res = max(res, dfs(l, m, d + 1, k) + dfs(m, r, d + 1, d));
              return res;
            })(0, 1 << n, 1, 0);

  cout << ans << endl;
  return 0;
}


Error: MLE

Fixed submission:

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

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n, m;
  cin >> n >> m;

  m++;
  vector<Int> a(m), b(m);
  for (Int i = 0; i < m; i++)
    cin >> a[i];
  for (Int i = 1; i < m; i++)
    cin >> b[i];

  vector<Int> hs;
  function<void(Int, Int)> predfs = [&](Int l, Int r) {
    Int x = lower_bound(a.begin(), a.end(), r) - a.begin();
    if (a[x - 1] <= l && r <= a[x])
      return;

    hs.emplace_back(l);
    Int m = (l + r) >> 1;
    predfs(l, m);
    predfs(m, r);
  };

  predfs(0, (1 << n));
  sort(hs.begin(), hs.end());
  hs.erase(unique(hs.begin(), hs.end()), hs.end());
  map<Int, Int> idx;
  for (Int i = 0; i < (Int)hs.size(); i++)
    idx[hs[i]] = i;
  auto dp = make_v<Int>(n + 2, n + 2, hs.size());
  fill_v(dp, -1);

  function<Int(Int, Int, Int, Int)> dfs = [&](Int l, Int r, Int d,
                                              Int k) -> Int {
    Int x = lower_bound(a.begin(), a.end(), r) - a.begin();
    if (a[x - 1] <= l && r <= a[x]) {
      Int v = b[x], tmp = 0;
      tmp += (k == v);
      tmp += (r - l) >> (n - v + 1);
      return tmp;
    }

    if (~dp[k][d][idx[l]])
      return dp[k][d][idx[l]];
    Int &res = (dp[k][d][idx[l]] = 0);

    Int m = (l + r) >> 1;
    res = max(res, dfs(l, m, d + 1, d) + dfs(m, r, d + 1, k));
    res = max(res, dfs(l, m, d + 1, k) + dfs(m, r, d + 1, d));
    return res;
  };

  cout << (1 << n) - dfs(0, 1 << n, 1, 0) << endl;
  return 0;
}