<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p>

<p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p>

<p>Return <em>the</em> <code>k</code> <em>pairs</em> <code>(u<sub>1</sub>, v<sub>1</sub>), (u<sub>2</sub>, v<sub>2</sub>), ..., (u<sub>k</sub>, v<sub>k</sub>)</code> <em>with the smallest sums</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [1,7,11], nums2 = [2,4,6], k = 3
<strong>Output:</strong> [[1,2],[1,4],[1,6]]
<strong>Explanation:</strong> The first 3 pairs are returned from the sequence: [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
</pre>

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

<pre>
<strong>Input:</strong> nums1 = [1,1,2], nums2 = [1,2,3], k = 2
<strong>Output:</strong> [[1,1],[1,1]]
<strong>Explanation:</strong> The first 2 pairs are returned from the sequence: [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
</pre>

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

<ul>
	<li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>nums1</code> and <code>nums2</code> both are sorted in <strong>non-decreasing order</strong>.</li>
	<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>
	<li><code>k &lt;=&nbsp;nums1.length *&nbsp;nums2.length</code></li>
</ul>

class Solution {
public:
    struct sij {
        int sum, i, j;
        sij(int sum=0, int i=0, int j=0): sum(sum), i(i), j(j) {}
        bool operator < (const sij& b) const {
            if (this->sum != b.sum) return this->sum < b.sum;
            else if (this->i != b.i) return this->i < b.i;
            else return this->j < b.j;
        }
        bool operator > (const sij& b) const {
            if (this->sum != b.sum) return this->sum > b.sum;
            else if (this->i != b.i) return this->i > b.i;
            else return this->j > b.j;
        }
        vector<int> pack(const vector<int> & a, const vector<int> &b) {
            vector<int> ret;
            ret.push_back(a[this->i]);
            ret.push_back(b[this->j]);
            return ret;
        }
    };
    priority_queue<sij, vector<sij>, greater<sij>> pq_sij;
    map<sij, bool> map_sij;

    vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
        int n1 = nums1.size(), n2 = nums2.size();
        sij first(nums1[0] + nums2[0], 0, 0);
        pq_sij.push(first);

        vector<vector<int>> ret;
        while (ret.size() < k) {
            sij x = pq_sij.top();
            pq_sij.pop();
            if (map_sij.find(x) == map_sij.end()) {
                ret.push_back(x.pack(nums1, nums2));
                map_sij[x] = true;
                if (x.i < n1 - 1) pq_sij.push(sij(nums1[x.i+1] + nums2[x.j], x.i+1, x.j));
                if (x.j < n2 - 1) pq_sij.push(sij(nums1[x.i] + nums2[x.j+1], x.i, x.j+1));
            }
        }

        return ret;
    }
};
144728