C++ || EASY TO UNDERSTAND || BINARY SEARCH
Approach\n Describe your approach to solving the problem. \n1. The code processes a series of queries, each representing either a row or a column in a matrix.\n
# Approach\n<!-- Describe your approach to solving the problem. -->\n1. The code processes a series of queries, each representing either a row or a column in a matrix.\n2. It stores the values assigned to each row and column in separate vectors (row and col).\n3. The code sorts the row queries based on their order of occurrence.\n4. It calculates the cumulative sum of row values, representing the sum of values in each row up to that point.\n5. It creates a vector (ans) with the indices of the row queries.\n6. The code iterates through the column queries and finds the relevant row queries using binary search.\n7. It calculates the sum based on the contributions of rows and columns.\n8. Finally, it returns the computed sum as the result.\n\n# Complexity\n- Time complexity: O(N*LOGN)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: 0(3*N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& queries) {\n        vector<pair<int,long long>> row(n,{0,0}),col(n,{0,0});\n        for(int i=0;i<queries.size();++i){\n            int type=queries[i][0];\n            int x=queries[i][1];\n            int val=queries[i][2];\n            if(type==0){\n                row[x]=make_pair(i+1,val);\n            }\n            else{\n                col[x]=make_pair(i+1,val);\n            }\n        }\n        sort(row.begin(),row.end());\n        for(int i=1;i<n;++i){\n            row[i].second+=row[i-1].second;\n        }\n        vector<int> ans(n,0);\n        for(int i=0;i<n;++i){\n            ans[i]=row[i].first;\n        }\n        long long sum=0;\n        for(int i=0;i<n;++i){\n            int ind=upper_bound(ans.begin(),ans.end(),col[i].first)-ans.begin();\n            if(ind==0){\n                sum+=row[n-1].second*1ll;\n            }\n            else{\n                sum+=(ind*col[i].second)*1ll;\n                sum+=(row[n-1].second-row[ind-1].second)*1ll;\n            }\n        }\n        return sum;\n    }\n};\n```
['Binary Search', 'Prefix Sum', 'C++']