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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &o) {
  out << "(" << o.first << "," << o.second << ")";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> V) {
  for (int i = 0; i < V.size(); i++) {
    out << V[i];
    if (i != V.size() - 1)
      out << " ";
  }
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> Mat) {
  for (int i = 0; i < Mat.size(); i++) {
    if (i != 0)
      out << endl;
    out << Mat[i];
  }
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> mp) {
  out << "{ ";
  for (auto it = mp.begin(); it != mp.end(); it++) {
    out << it->first << ":" << it->second;
    if (mp.size() - 1 != distance(mp.begin(), it))
      out << ", ";
  }
  out << " }";
  return out;
}

/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */
ll solve() {
  ll res = 0;
  ll H, W;
  cin >> H >> W;
  vector<vector<ll>> M(H + 2, vector<ll>(W + 2, LINF));
  vector<pll> Section(H * W);
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      ll x;
      cin >> x;
      x--;
      M[i][j] = x;
      Section[x] = {i, j};
    }
  }

  vector<vector<ll>> flag(H + 2, vector<ll>(W + 2, -1));
  int id = 0;
  int d[4] = {1, 0, -1, 0};
  for (int i = 0; i < H * W; i++) {
    ll x, y;
    tie(x, y) = Section[i];

    vector<ll> F;
    for (int k = 0; k < 4; k++) {
      ll nx = x + d[k];
      ll ny = y + d[k ^ 1];
      if (M[nx][ny] < M[x][y]) {
        if (flag[nx][ny] == INF) {
          F.push_back(INF);
          F.push_back(INF + 1);
        } else {
          F.push_back(flag[nx][ny]);
        }
      }
    }
    sort(F.begin(), F.end());
    F.erase(unique(F.begin(), F.end()), F.end());
    if (F.empty()) {
      flag[x][y] = id++;
    } else if (F.size() == 1) {
      flag[x][y] = F[0];
    } else {
      flag[x][y] = INF;
      res++;
    }
  }
  return res;
}
int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << solve() << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define debug(x) cerr << #x << ": " << x << endl

using LL = long long;
template <class T> using V = vector<T>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int H, W;
  cin >> H >> W;
  V<V<int>> vs(H, V<int>(W));
  using PII = pair<int, int>;
  V<pair<int, PII>> q;
  FOR(i, 0, H) {
    FOR(j, 0, W) {
      cin >> vs[i][j];
      q.push_back({vs[i][j], {i, j}});
    }
  }
  const int DY[] = {0, 0, 1, -1};
  const int DX[] = {1, -1, 0, 0};
  SORT(q);
  // unordered_set
  V<V<set<int>>> RootFlow(H, V<set<int>>(W));
  FOR(i, 0, SZ(q)) {
    int m = q[i].first;
    int y = q[i].second.first, x = q[i].second.second;
    if (!SZ(RootFlow[y][x])) {
      RootFlow[y][x].insert(m); // s
    }
    FOR(k, 0, 4) {
      int ny = y + DY[k], nx = x + DX[k];
#define IN(a, b, c) a <= b &&b < c
      if (IN(0, ny, H) && IN(0, nx, W)) {
        if (vs[ny][nx] > vs[y][x]) {
          for (const auto &it : RootFlow[y][x]) {
            RootFlow[ny][nx].insert(it);
          }
        }
      }
    }
  }

  int ans = 0;
  FOR(i, 0, H) {
    FOR(j, 0, W) { ans += (SZ(RootFlow[i][j]) > 1); }
  }
  cout << ans << endl;
  return 0;
}