Find the weight at (Xi, Yi) after M operations in a Matrix

Given n x n points on cartesian plane, the task is to find the weight at (xi, yj) after m operation. xi, yj, w denotes the operation of adding weight w at all the points on the lines x = xi and y = yj.

Examples:

Input: n = 3, m = 2
0 0 1
1 1 2
x = 1, y = 0
Output: 3
Explanation: Initially, weights are
0 0 0
0 0 0
0 0 0
After 1st operation
2 1 1
1 0 0
1 0 0
After 2nd operation
2 3 1
3 4 2
1 0 0
Clearly, weight at (x1, y0) is 3



Input: n = 2, m = 2
0 1 1
1 0 2
x = 1, y = 1
Output: 3

Naive Approach:
Consider a 2-d array arr[n][n] = {0} and perform the given operations and then, retrieve the weight at (xi, yj). This approach will take O(n*m) time.

Efficient Approach: