<p>Given a <code>matrix</code>&nbsp;and a <code>target</code>, return the number of non-empty submatrices that sum to <font face="monospace">target</font>.</p>

<p>A submatrix <code>x1, y1, x2, y2</code> is the set of all cells <code>matrix[x][y]</code> with <code>x1 &lt;= x &lt;= x2</code> and <code>y1 &lt;= y &lt;= y2</code>.</p>

<p>Two submatrices <code>(x1, y1, x2, y2)</code> and <code>(x1&#39;, y1&#39;, x2&#39;, y2&#39;)</code> are different if they have some coordinate&nbsp;that is different: for example, if <code>x1 != x1&#39;</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/02/mate1.jpg" style="width: 242px; height: 242px;" />
<pre>
<strong>Input:</strong> matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
<strong>Output:</strong> 4
<strong>Explanation:</strong> The four 1x1 submatrices that only contain 0.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> matrix = [[1,-1],[-1,1]], target = 0
<strong>Output:</strong> 5
<strong>Explanation:</strong> The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> matrix = [[904]], target = 0
<strong>Output:</strong> 0
</pre>

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

<ul>
	<li><code>1 &lt;= matrix.length &lt;= 100</code></li>
	<li><code>1 &lt;= matrix[0].length &lt;= 100</code></li>
	<li><code>-1000 &lt;= matrix[i][j] &lt;= 1000</code></li>
	<li><code>-10^8 &lt;= target &lt;= 10^8</code></li>
</ul>

//
// Created by smile on 2024/7/18.
//

#include <vector>
#include <unordered_map>
#include <future>

class Solution {
public:
    int numSubmatrixSumTarget(std::vector<std::vector<int>> &matrix, int target) {
        auto mSum = std::remove_reference<decltype(matrix)>::type(matrix.size() + 1,
                                                                  std::remove_reference<decltype(matrix.front())>::type(
                                                                          matrix.front().size() + 1, 0));

        int n = matrix.size();
        int m = matrix.front().size();
        // 在外圈一层0
        for (size_t i = 1; i <= n; ++i) {
            for (size_t j = 1; j <= matrix.front().size(); ++j) {
                mSum[i][j] = mSum[i][j - 1] + mSum[i - 1][j] - mSum[i - 1][j - 1] + matrix[i - 1][j - 1];
            }
        }

        std::promise<int> promise1;
        std::promise<int> promise2;
        std::future<int> future1 = promise1.get_future();
        std::future<int> future2 = promise2.get_future();
        std::thread t1(test, n, m, mSum, target, std::move(promise1));
        std::thread t2(test1, n, m, mSum, target, std::move(promise2));

        int ans;
        // 等待并获取结果
        while (true) {
            if (future1.wait_for(std::chrono::milliseconds(1)) == std::future_status::ready) {
                ans = future1.get();
                break;
            }
            if (future2.wait_for(std::chrono::milliseconds(1)) == std::future_status::ready) {
                ans = future2.get();
                break;
            }
        }
        t1.detach(); // 分离线程，以免阻塞
        t2.detach(); // 分离线程，以免阻塞
        return ans;
    }

private:
    static void test(int n, int m, const std::vector<std::vector<int>> &mSum, int target, std::promise<int> p) {
        int ans = 0;
        for (size_t iEnd = 1; iEnd <= n; ++iEnd) {
            for (size_t jEnd = 1; jEnd <= m; ++jEnd) {
                for (size_t iStart = 1; iStart <= iEnd; ++iStart) {
                    for (size_t jStart = 1; jStart <= jEnd; ++jStart) {
                        if (mSum[iEnd][jEnd] - mSum[iStart - 1][jEnd] - mSum[iEnd][jStart - 1] +
                            mSum[iStart - 1][jStart - 1] ==
                            target) {
                            ans++;
                        }
                    }
                }
            }
        }
        p.set_value(ans);
    }

    static void test1(int n, int m, const std::vector<std::vector<int>> &mSum, int target, std::promise<int> p) {
        int ans = 0;
        for (size_t up = 1; up <= n; ++up) {     // 上边界
            for (size_t down = up; down <= n; ++down) { // 下边界
                std::unordered_map<int, int> areaMap;
                for (size_t right = 1; right <= m; ++right) {
                    auto curArea = mSum[down][right] - mSum[up - 1][right];
                    if (curArea == target) {
                        ans++;
                    }
                    auto remainArea = curArea - target;
                    if (areaMap.find(remainArea) != areaMap.end()) {
                        ans += areaMap[remainArea];
                    }
                    areaMap[curArea]++;
                }
            }
        }
        p.set_value(ans);
    }
};
80485