Loop From Right to Left
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```csharp []
public class Solution {
    public long MatrixSumQueries(int n, int[][] queries) {
        long sum = 0;
        int[] freq = [n, n];
        bool[,] visit = new bool[2, n];
        int qLen = queries.Length;
        for(int i = qLen-1; i >= 0; i--) // from right to left
        {
            int type = queries[i][0], id = queries[i][1], val = queries[i][2];
            if(!visit[type, id])
            {
                visit[type, id] = true;
                sum += (long)(val*freq[(type+1)%2]);  // sum valid of the other one
                freq[type]--;
            }
        }

        return sum;
    }
}
```
['C#']