C++ With Explaination
It\'s the same as prefixSum, but in this problem, we need to calculate prefixXOR and in a 2-D matrix.\n\nWhen i == 0 and j == 0, the predixXOR[i][j] is just equ
It\'s the same as prefixSum, but in this problem, we need to calculate prefixXOR and in a 2-D matrix.\n\nWhen i == 0 and j == 0, the `predixXOR[i][j]` is just equal to `matrix[0][0]`.\n\nWhen i == 0 and j != 0, the `predixXOR[i][j]` is just equal to `predixXOR[i][j-1] ^ matrix[i][j]`.\n\nWhen i != 0 and j == 0, the `predixXOR[i][j]` is just equal to `predixXOR[i-1][j] ^ matrix[i][j]`.\n\n![image](https://assets.leetcode.com/users/images/7073d862-6020-4165-9e2c-474c639cae21_1611461046.7700498.png)\n\n\nThen how can we calculate `predixXOR[i][j]` when i != 0 and j != 0 ?\n\n![image](https://assets.leetcode.com/users/images/a8d3c506-2b04-455a-80ca-a07fa78cea08_1611461054.7258484.png)\n\n\n`prefixXOR[0][1] = 5 ^ 1`, `prefixXOR[1][0] = 5 ^ 3`, and `prefixXOR[1][1]` should be `5 ^ 1 ^ 3 ^ 4`.\n\nIf we use `prefixXOR[0][1] ^ prefixXOR[1][0]`, then we do a repeated calculation, so we need to "minus" this repeated calculation.\n\nSo `predixXOR[i][j] = `predixXOR[i][j-1] ^ predixXOR[i-1][j] ^ predixXOR[i-1][j-1] ^ matrix[i][j]`.\n\nAt last, we can use heap to get the K\'th largest elements from our prefixXOR. Use sort solution maybe TLE.\n\n\n\n```cpp\nclass Solution {\npublic:\n    int kthLargestValue(vector<vector<int>>& matrix, int k) {\n        int m = matrix.size(), n = matrix[0].size();\n        \n        vector<int> coordinates;\n        vector<vector<int>> prefixXOR(m, vector<int>(n, 0));\n\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                if (i == 0 && j == 0) prefixXOR[i][j] = matrix[0][0];\n                else if (i == 0 && j != 0) prefixXOR[i][j] = prefixXOR[i][j-1] ^ matrix[i][j];\n                else if (i != 0 && j == 0) prefixXOR[i][j] = prefixXOR[i-1][j] ^ matrix[i][j];\n                else prefixXOR[i][j] = prefixXOR[i-1][j] ^ prefixXOR[i][j-1] ^ prefixXOR[i-1][j-1] ^ matrix[i][j];\n            }\n        }\n\n        priority_queue<int, vector<int>, greater<int>> heap;\n\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                if (heap.size() == k) {\n                    if (prefixXOR[i][j] > heap.top()) {\n                        heap.pop();\n                        heap.push(prefixXOR[i][j]);\n                    }\n                }\n                else heap.push(prefixXOR[i][j]);\n            }\n        }\n        return heap.top();\n    }\n};\n```
['C']