Easy solution in java (O(n))
IntuitionJust traverse the grid from last and keep maintain the mapApproachComplexity
Time complexity:
O(n)
Space complexity:
O(n)Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Just traverse the grid from last and keep maintain the map
# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(n)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(n)
# Code
```java []
class Solution {
    public long matrixSumQueries(int n, int[][] queries) {
        Map<Integer,Set<Integer>> hm = new HashMap<>();
        hm.put(0, new HashSet<>());
        hm.put(1, new HashSet<>());
        long sum = 0;
        for(int i=queries.length-1;i>=0;i--) {
            int differenceBetweenRowAndColumn = queries[i][0];
            int index = queries[i][1];
            int value = queries[i][2];
            if(differenceBetweenRowAndColumn == 0) {
                Set<Integer> st = hm.get(0);
                if(st.contains(index)) {
                    continue;
                }
                sum = sum + (n-hm.get(1).size())*value;
                st.add(index);
                hm.put(0,st);
            }
            else if(differenceBetweenRowAndColumn == 1) {
                 Set<Integer> st = hm.get(1);
                if(st.contains(index)) {
                    continue;
                }
               sum = sum + (n-hm.get(0).size())*value;
                st.add(index);
                hm.put(1,st);
            }
        }
        return sum;
    }
}
```
['Ordered Map', 'Ordered Set', 'Java']