Simple direct approach 
IntuitionInstead of directly modifying a large matrix, we track updates using bitsets for rows and columns. We process queries in reverse order, updating the su
# Intuition
Instead of directly modifying a large matrix, we track updates using bitsets for rows and columns. We process queries in reverse order, updating the sum based on unmodified rows and columns.

# Approach

1. Bitsets for Row/Column Tracking:  Use `bitset<10001>` to track if a row or column has been updated.

2. Process Queries in Reverse:  Start from the last query and update the sum based on how many rows or columns are still unmodified.

3. Early Exit:  If all rows or columns are updated, stop processing further queries.

# Complexity
- Time complexity:
O(N)

- Space complexity:
O(N)

# Code
```cpp []
class Solution {
public:
    long long matrixSumQueries(int n, vector<vector<int>>& q) {
        int row=n,col=n;
        long long ans=0;
        vector<bool> rowFlag(n, 1), colFlag(n, 1);

        for(int i=q.size()-1;i>=0;i--)
        {
            if(row==0 || col==0)
                break;
if(q[i][0] == 0 && rowFlag[q[i][1]]) { ans += col * q[i][2];    rowFlag[q[i][1]] = 0; row--; }
if(q[i][0] == 1 && colFlag[q[i][1]]) { ans += row * q[i][2];    colFlag[q[i][1]] = 0; col--; }
        }

        return ans;
    }
};
```

``` Python []
class Solution:
    def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int:
        row, col = n, n
        ans = 0
        rowFlag = [True] * n
        colFlag = [True] * n

        for i in range(len(queries) - 1, -1, -1):
            if row == 0 or col == 0:
                break
            if queries[i][0] == 0 and rowFlag[queries[i][1]]:
                ans += col * queries[i][2]
                rowFlag[queries[i][1]] = False
                row -= 1
            elif queries[i][0] == 1 and colFlag[queries[i][1]]:
                ans += row * queries[i][2]
                colFlag[queries[i][1]] = False
                col -= 1

        return ans
```


``` java []
class Solution {
    public long matrixSumQueries(int n, int[][] queries) {
        int row = n, col = n;
        long ans = 0;
        boolean[] rowFlag = new boolean[n];
        boolean[] colFlag = new boolean[n];

        for (int i = queries.length - 1; i >= 0; i--) {
            if (row == 0 || col == 0) break;
            if (queries[i][0] == 0 && !rowFlag[queries[i][1]]) {
                ans += col * queries[i][2];
                rowFlag[queries[i][1]] = true;
                row--;
            }
            if (queries[i][0] == 1 && !colFlag[queries[i][1]]) {
                ans += row * queries[i][2];
                colFlag[queries[i][1]] = true;
                col--;
            }
        }
        return ans;
    }
}

```
``` C# []
public class Solution {
    public long MatrixSumQueries(int n, int[][] queries) {
        int row = n, col = n;
        long ans = 0;
        bool[] rowFlag = new bool[n];
        bool[] colFlag = new bool[n];

        for (int i = queries.Length - 1; i >= 0; i--) {
            if (row == 0 || col == 0) break;
            if (queries[i][0] == 0 && !rowFlag[queries[i][1]]) {
                ans += col * queries[i][2];
                rowFlag[queries[i][1]] = true;
                row--;
            }
            if (queries[i][0] == 1 && !colFlag[queries[i][1]]) {
                ans += row * queries[i][2];
                colFlag[queries[i][1]] = true;
                col--;
            }
        }
        return ans;
    }
}

```
``` Go []
func matrixSumQueries(n int, queries [][]int) int64 {
    row, col := n, n
    ans := int64(0)
    rowFlag := make([]bool, n)
    colFlag := make([]bool, n)

    for i := len(queries) - 1; i >= 0; i-- {
        if row == 0 || col == 0 {
            break
        }
        if queries[i][0] == 0 && !rowFlag[queries[i][1]] {
            ans += int64(col * queries[i][2])
            rowFlag[queries[i][1]] = true
            row--
        }
        if queries[i][0] == 1 && !colFlag[queries[i][1]] {
            ans += int64(row * queries[i][2])
            colFlag[queries[i][1]] = true
            col--
        }
    }
    return ans
}

```
``` rust []
impl Solution {
    pub fn matrix_sum_queries(n: i32, queries: Vec<Vec<i32>>) -> i64 {
        let mut row = n;
        let mut col = n;
        let mut ans = 0i64;  // Declare ans as i64 to match the return type
        let mut row_flag = vec![false; n as usize];
        let mut col_flag = vec![false; n as usize];

        for query in queries.iter().rev() {
            if row == 0 || col == 0 {
                break;
            }
            if query[0] == 0 && !row_flag[query[1] as usize] {
                ans += col as i64 * query[2] as i64;
                row_flag[query[1] as usize] = true;
                row -= 1;
            }
            if query[0] == 1 && !col_flag[query[1] as usize] {
                ans += row as i64 * query[2] as i64;
                col_flag[query[1] as usize] = true;
                col -= 1;
            }
        }

        ans  
    }
}


```

['C++']