<p>You are given a 2D <code>matrix</code> of size <code>m x n</code>, consisting of non-negative integers. You are also given an integer <code>k</code>.</p>

<p>The <strong>value</strong> of coordinate <code>(a, b)</code> of the matrix is the XOR of all <code>matrix[i][j]</code> where <code>0 &lt;= i &lt;= a &lt; m</code> and <code>0 &lt;= j &lt;= b &lt; n</code> <strong>(0-indexed)</strong>.</p>

<p>Find the <code>k<sup>th</sup></code> largest value <strong>(1-indexed)</strong> of all the coordinates of <code>matrix</code>.</p>

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

<pre>
<strong>Input:</strong> matrix = [[5,2],[1,6]], k = 1
<strong>Output:</strong> 7
<strong>Explanation:</strong> The value of coordinate (0,1) is 5 XOR 2 = 7, which is the largest value.
</pre>

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

<pre>
<strong>Input:</strong> matrix = [[5,2],[1,6]], k = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The value of coordinate (0,0) is 5 = 5, which is the 2nd largest value.
</pre>

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

<pre>
<strong>Input:</strong> matrix = [[5,2],[1,6]], k = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The value of coordinate (1,0) is 5 XOR 1 = 4, which is the 3rd largest value.</pre>

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

<ul>
	<li><code>m == matrix.length</code></li>
	<li><code>n == matrix[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 1000</code></li>
	<li><code>0 &lt;= matrix[i][j] &lt;= 10<sup>6</sup></code></li>
	<li><code>1 &lt;= k &lt;= m * n</code></li>
</ul>

class Solution {
public:
    int kthLargestValue(vector<vector<int>>& matrix, int k) {
    int n=matrix.size();
    int m=matrix[0].size();
    priority_queue<int,vector<int>,greater<int>>pq;
    //min heap we will push the greater elements in this heap 
    vector<int>lastprefix_xor(m+1,0);
    for(int i=0;i<m;i++){
        lastprefix_xor[i+1]=lastprefix_xor[i]^matrix[0][i];
        if(pq.size()==k){
            if(pq.top()<lastprefix_xor[i+1]){
                pq.pop();
                pq.push(lastprefix_xor[i+1]);
            }
        }
        else pq.push(lastprefix_xor[i+1]);
    }
    for(int row=1;row<n;row++){
        vector<int>curr_prefix_xor(m+1,0);
        vector<int>temp_xor(m+1,0);
        //temp will act as the last xor for the next iteration
        for(int i=0;i<m;i++){
        curr_prefix_xor[i+1]=curr_prefix_xor[i]^matrix[row][i];
        temp_xor[i+1]=lastprefix_xor[i+1]^curr_prefix_xor[i+1];
        if(pq.size()==k){
            if(pq.top()<temp_xor[i+1]){
                pq.pop();
                pq.push(temp_xor[i+1]);
            }
        }
        else pq.push(temp_xor[i+1]);           
        }
        lastprefix_xor=temp_xor;
    } 
    return pq.top(); 
     /*    int m=nums.size(),n=nums[0].size();
        vector<vector<int>>top=nums;
        for(int j=0;j<n;j++){
            for(int i=1;i<m;i++){
                top[i][j]^=top[i-1][j];
            }
        }
        priority_queue<int,vector<int>,greater<int>>pq;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                nums[i][j]=top[i][j];
                if(j)
                    nums[i][j]^=nums[i][j-1];
                int curr=nums[i][j];
                if(pq.size()==k){
                    if(pq.top()<curr){
                        pq.pop();
                        pq.push(curr);
                    }
                }else{
                    pq.push(curr);
                }
            }
        }
        return pq.top();*/
    }
};
128300