<p>Given an <code>m x n</code> integer matrix <code>heightMap</code> representing the height of each unit cell in a 2D elevation map, return <em>the volume of water it can trap after raining</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/trap1-3d.jpg" style="width: 361px; height: 321px;" />
<pre>
<strong>Input:</strong> heightMap = [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> After the rain, water is trapped between the blocks.
We have two small ponds 1 and 3 units trapped.
The total volume of water trapped is 4.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/trap2-3d.jpg" style="width: 401px; height: 321px;" />
<pre>
<strong>Input:</strong> heightMap = [[3,3,3,3,3],[3,2,2,2,3],[3,2,1,2,3],[3,2,2,2,3],[3,3,3,3,3]]
<strong>Output:</strong> 10
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == heightMap.length</code></li>
	<li><code>n == heightMap[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 200</code></li>
	<li><code>0 &lt;= heightMap[i][j] &lt;= 2 * 10<sup>4</sup></code></li>
</ul>

using ll = long long;

#ifndef debug
#define debug(...) 1
#endif

class Solution {
public:
  int solve1(vector<vector<int>>& h) {
    const int n = h.size();
    const int m = h.front().size();

    const auto get = [&](vector<vector<int>>& dp, int i, int j) -> int {
      if (i >= 0 && j >= 0 && i < n && j < m) {
        return dp[i][j];
      }

      return 0;
    };

    vector<vector<int>> tl(n, vector<int>(m, 0));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        tl[i][j] = max(h[i][j], min(get(tl, i - 1, j), get(tl, i, j - 1)));
      }
    }

    vector<vector<int>> tr(n, vector<int>(m, 0));
    for (int i = 0; i < n; ++i) {
      for (int j = m - 1; j >= 0; --j) {
        tr[i][j] = max(h[i][j], min(get(tr, i - 1, j), get(tr, i, j + 1)));
      }
    }

    vector<vector<int>> bl(n, vector<int>(m, 0));
    for (int i = n - 1; i >= 0; --i) {
      for (int j = 0; j < m; ++j) {
        bl[i][j] = max(h[i][j], min(get(bl, i + 1, j), get(bl, i, j - 1)));
      }
    }

    vector<vector<int>> br(n, vector<int>(m, 0));
    for (int i = n - 1; i >= 0; --i) {
      for (int j = m - 1; j >= 0; --j) {
        br[i][j] = max(h[i][j], min(get(br, i + 1, j), get(br, i, j + 1)));
      }
    }

    debug("h");
    for (const auto& row : h) {
      debug(row);
    }

    debug("tl");
    for (const auto& row : tl) {
      debug(row);
    }

    debug("tr");
    for (const auto& row : tr) {
      debug(row);
    }

    debug("bl");
    for (const auto& row : bl) {
      debug(row);
    }

    debug("br");
    for (const auto& row : br) {
      debug(row);
    }

    ll ans = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        // int hold = min({tl[i][j], tr[i][j], bl[i][j], br[i][j]});
        int hold = min({
          get(tl, i - 1, j), get(tr, i - 1, j),
          get(tl, i, j - 1), get(bl, i, j - 1),
          get(tr, i, j + 1), get(br, i, j + 1),
          get(bl, i + 1, j), get(br, i + 1, j)
        });

        ans += max(hold - h[i][j], 0);
      }
    }

    return ans;
  }

  class Item {
  public:
    int i = 0;
    int j = 0;
    int m = 0;
  };

  int solve2(vector<vector<int>>& h) {
    const int n = h.size();
    const int m = h.front().size();
    const int inf = 1e9;

    const auto comp = [](const Item& l, const Item& r) -> bool {
      return l.m > r.m;
    };

    priority_queue<Item, vector<Item>, decltype(comp)> q(comp);
    for (int i = 0; i < n; ++i) {
      q.emplace(Item{i, 0, h[i][0]});
      q.emplace(Item{i, m - 1, h[i][m - 1]});
    }

    for (int j = 0; j < m; ++j) {
      q.emplace(Item{0, j, h[0][j]});
      q.emplace(Item{n - 1,j, h[n - 1][j]});
    }

    vector<vector<int>> dp(n, vector<int>(m, inf));
    const auto add = [&](const Item& v, int di, int dj) {
      const int i = v.i + di;
      const int j = v.j + dj;

      if (i >= 0 && i < n && j >= 0 && j < m) {
        const int next_max = max(v.m, h[i][j]);
        if (dp[i][j] > next_max) {
          q.emplace(Item{i, j, next_max});
        }
      }
    };

    while (!q.empty()) {
      const auto v = q.top();
      q.pop();

      if (dp[v.i][v.j] <= v.m) {
        continue;
      }

      dp[v.i][v.j] = v.m;

      add(v, -1, 0);
      add(v, 1, 0);
      add(v, 0, -1);
      add(v, 0, 1);
    }

    int ans = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        ans += max(dp[i][j] - h[i][j], 0);
      }
    }

    return ans;
  }

  int trapRainWater(vector<vector<int>>& heightMap) {
    // return solve1(heightMap);
    return solve2(heightMap);
  }
};

21068