{ "data": { "question": { "questionId": "1572", "questionFrontendId": "1476", "boundTopicId": null, "title": "Subrectangle Queries", "titleSlug": "subrectangle-queries", "content": "

Implement the class SubrectangleQueries which receives a rows x cols rectangle as a matrix of integers in the constructor and supports two methods:

\n\n

1. updateSubrectangle(int row1, int col1, int row2, int col2, int newValue)

\n\n\n\n

2. getValue(int row, int col)

\n\n\n\n

 

\n

Example 1:

\n\n
\nInput\n["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue","getValue"]\n[[[[1,2,1],[4,3,4],[3,2,1],[1,1,1]]],[0,2],[0,0,3,2,5],[0,2],[3,1],[3,0,3,2,10],[3,1],[0,2]]\nOutput\n[null,1,null,5,5,null,10,5]\nExplanation\nSubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,2,1],[4,3,4],[3,2,1],[1,1,1]]);  \n// The initial rectangle (4x3) looks like:\n// 1 2 1\n// 4 3 4\n// 3 2 1\n// 1 1 1\nsubrectangleQueries.getValue(0, 2); // return 1\nsubrectangleQueries.updateSubrectangle(0, 0, 3, 2, 5);\n// After this update the rectangle looks like:\n// 5 5 5\n// 5 5 5\n// 5 5 5\n// 5 5 5 \nsubrectangleQueries.getValue(0, 2); // return 5\nsubrectangleQueries.getValue(3, 1); // return 5\nsubrectangleQueries.updateSubrectangle(3, 0, 3, 2, 10);\n// After this update the rectangle looks like:\n// 5   5   5\n// 5   5   5\n// 5   5   5\n// 10  10  10 \nsubrectangleQueries.getValue(3, 1); // return 10\nsubrectangleQueries.getValue(0, 2); // return 5\n
\n\n

Example 2:

\n\n
\nInput\n["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue"]\n[[[[1,1,1],[2,2,2],[3,3,3]]],[0,0],[0,0,2,2,100],[0,0],[2,2],[1,1,2,2,20],[2,2]]\nOutput\n[null,1,null,100,100,null,20]\nExplanation\nSubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,1,1],[2,2,2],[3,3,3]]);\nsubrectangleQueries.getValue(0, 0); // return 1\nsubrectangleQueries.updateSubrectangle(0, 0, 2, 2, 100);\nsubrectangleQueries.getValue(0, 0); // return 100\nsubrectangleQueries.getValue(2, 2); // return 100\nsubrectangleQueries.updateSubrectangle(1, 1, 2, 2, 20);\nsubrectangleQueries.getValue(2, 2); // return 20\n
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 593, "dislikes": 1402, "isLiked": null, "similarQuestions": "[]", "exampleTestcases": "[\"SubrectangleQueries\",\"getValue\",\"updateSubrectangle\",\"getValue\",\"getValue\",\"updateSubrectangle\",\"getValue\",\"getValue\"]\r\n[[[[1,2,1],[4,3,4],[3,2,1],[1,1,1]]],[0,2],[0,0,3,2,5],[0,2],[3,1],[3,0,3,2,10],[3,1],[0,2]]\r\n[\"SubrectangleQueries\",\"getValue\",\"updateSubrectangle\",\"getValue\",\"getValue\",\"updateSubrectangle\",\"getValue\"]\r\n[[[[1,1,1],[2,2,2],[3,3,3]]],[0,0],[0,0,2,2,100],[0,0],[2,2],[1,1,2,2,20],[2,2]]\r", "categoryTitle": "Algorithms", "contributors": [], "topicTags": [ { "name": "Array", "slug": "array", "translatedName": null, "__typename": "TopicTagNode" }, { "name": "Design", "slug": "design", "translatedName": null, "__typename": "TopicTagNode" }, { "name": "Matrix", "slug": "matrix", "translatedName": null, "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "C++", "langSlug": "cpp", "code": "class SubrectangleQueries {\npublic:\n SubrectangleQueries(vector>& rectangle) {\n \n }\n \n void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {\n \n }\n \n int getValue(int row, int col) {\n \n }\n};\n\n/**\n * Your SubrectangleQueries object will be instantiated and called as such:\n * SubrectangleQueries* obj = new SubrectangleQueries(rectangle);\n * obj->updateSubrectangle(row1,col1,row2,col2,newValue);\n * int param_2 = obj->getValue(row,col);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Java", "langSlug": "java", "code": "class SubrectangleQueries {\n\n public SubrectangleQueries(int[][] rectangle) {\n \n }\n \n public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {\n \n }\n \n public int getValue(int row, int col) {\n \n }\n}\n\n/**\n * Your SubrectangleQueries object will be instantiated and called as such:\n * SubrectangleQueries obj = new SubrectangleQueries(rectangle);\n * obj.updateSubrectangle(row1,col1,row2,col2,newValue);\n * int param_2 = obj.getValue(row,col);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Python", "langSlug": "python", "code": "class SubrectangleQueries(object):\n\n def __init__(self, rectangle):\n \"\"\"\n :type rectangle: List[List[int]]\n \"\"\"\n \n\n def updateSubrectangle(self, row1, col1, row2, col2, newValue):\n \"\"\"\n :type row1: int\n :type col1: int\n :type row2: int\n :type col2: int\n :type newValue: int\n :rtype: None\n \"\"\"\n \n\n def getValue(self, row, col):\n \"\"\"\n :type row: int\n :type col: int\n :rtype: int\n \"\"\"\n \n\n\n# Your SubrectangleQueries object will be instantiated and called as such:\n# obj = SubrectangleQueries(rectangle)\n# obj.updateSubrectangle(row1,col1,row2,col2,newValue)\n# param_2 = obj.getValue(row,col)", "__typename": "CodeSnippetNode" }, { "lang": "Python3", "langSlug": "python3", "code": "class SubrectangleQueries:\n\n def __init__(self, rectangle: List[List[int]]):\n \n\n def updateSubrectangle(self, row1: int, col1: int, row2: int, col2: int, newValue: int) -> None:\n \n\n def getValue(self, row: int, col: int) -> int:\n \n\n\n# Your SubrectangleQueries object will be instantiated and called as such:\n# obj = SubrectangleQueries(rectangle)\n# obj.updateSubrectangle(row1,col1,row2,col2,newValue)\n# param_2 = obj.getValue(row,col)", "__typename": "CodeSnippetNode" }, { "lang": "C", "langSlug": "c", "code": "\n\n\ntypedef struct {\n \n} SubrectangleQueries;\n\n\nSubrectangleQueries* subrectangleQueriesCreate(int** rectangle, int rectangleSize, int* rectangleColSize) {\n \n}\n\nvoid subrectangleQueriesUpdateSubrectangle(SubrectangleQueries* obj, int row1, int col1, int row2, int col2, int newValue) {\n \n}\n\nint subrectangleQueriesGetValue(SubrectangleQueries* obj, int row, int col) {\n \n}\n\nvoid subrectangleQueriesFree(SubrectangleQueries* obj) {\n \n}\n\n/**\n * Your SubrectangleQueries struct will be instantiated and called as such:\n * SubrectangleQueries* obj = subrectangleQueriesCreate(rectangle, rectangleSize, rectangleColSize);\n * subrectangleQueriesUpdateSubrectangle(obj, row1, col1, row2, col2, newValue);\n \n * int param_2 = subrectangleQueriesGetValue(obj, row, col);\n \n * subrectangleQueriesFree(obj);\n*/", "__typename": "CodeSnippetNode" }, { "lang": "C#", "langSlug": "csharp", "code": "public class SubrectangleQueries {\n\n public SubrectangleQueries(int[][] rectangle) {\n \n }\n \n public void UpdateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {\n \n }\n \n public int GetValue(int row, int col) {\n \n }\n}\n\n/**\n * Your SubrectangleQueries object will be instantiated and called as such:\n * SubrectangleQueries obj = new SubrectangleQueries(rectangle);\n * obj.UpdateSubrectangle(row1,col1,row2,col2,newValue);\n * int param_2 = obj.GetValue(row,col);\n */", "__typename": "CodeSnippetNode" }, { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} rectangle\n */\nvar SubrectangleQueries = function(rectangle) {\n \n};\n\n/** \n * @param {number} row1 \n * @param {number} col1 \n * @param {number} row2 \n * @param {number} col2 \n * @param {number} newValue\n * @return {void}\n */\nSubrectangleQueries.prototype.updateSubrectangle = function(row1, col1, row2, col2, newValue) {\n \n};\n\n/** \n * @param {number} row \n * @param {number} col\n * @return {number}\n */\nSubrectangleQueries.prototype.getValue = function(row, col) {\n \n};\n\n/** \n * Your SubrectangleQueries object will be instantiated and called as such:\n * var obj = new SubrectangleQueries(rectangle)\n * obj.updateSubrectangle(row1,col1,row2,col2,newValue)\n * var param_2 = obj.getValue(row,col)\n */", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "class SubrectangleQueries {\n constructor(rectangle: number[][]) {\n\n }\n\n updateSubrectangle(row1: number, col1: number, row2: number, col2: number, newValue: number): void {\n\n }\n\n getValue(row: number, col: number): number {\n\n }\n}\n\n/**\n * Your SubrectangleQueries object will be instantiated and called as such:\n * var obj = new SubrectangleQueries(rectangle)\n * obj.updateSubrectangle(row1,col1,row2,col2,newValue)\n * var param_2 = obj.getValue(row,col)\n */", "__typename": "CodeSnippetNode" }, { "lang": "PHP", "langSlug": "php", "code": "class SubrectangleQueries {\n /**\n * @param Integer[][] $rectangle\n */\n function __construct($rectangle) {\n \n }\n \n /**\n * @param Integer $row1\n * @param Integer $col1\n * @param Integer $row2\n * @param Integer $col2\n * @param Integer $newValue\n * @return NULL\n */\n function updateSubrectangle($row1, $col1, $row2, $col2, $newValue) {\n \n }\n \n /**\n * @param Integer $row\n * @param Integer $col\n * @return Integer\n */\n function getValue($row, $col) {\n \n }\n}\n\n/**\n * Your SubrectangleQueries object will be instantiated and called as such:\n * $obj = SubrectangleQueries($rectangle);\n * $obj->updateSubrectangle($row1, $col1, $row2, $col2, $newValue);\n * $ret_2 = $obj->getValue($row, $col);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Swift", "langSlug": "swift", "code": "\nclass SubrectangleQueries {\n\n init(_ rectangle: [[Int]]) {\n \n }\n \n func updateSubrectangle(_ row1: Int, _ col1: Int, _ row2: Int, _ col2: Int, _ newValue: Int) {\n \n }\n \n func getValue(_ row: Int, _ col: Int) -> Int {\n \n }\n}\n\n/**\n * Your SubrectangleQueries object will be instantiated and called as such:\n * let obj = SubrectangleQueries(rectangle)\n * obj.updateSubrectangle(row1, col1, row2, col2, newValue)\n * let ret_2: Int = obj.getValue(row, col)\n */", "__typename": "CodeSnippetNode" }, { "lang": "Kotlin", "langSlug": "kotlin", "code": "class SubrectangleQueries(rectangle: Array) {\n\n fun updateSubrectangle(row1: Int, col1: Int, row2: Int, col2: Int, newValue: Int) {\n \n }\n\n fun getValue(row: Int, col: Int): Int {\n \n }\n\n}\n\n/**\n * Your SubrectangleQueries object will be instantiated and called as such:\n * var obj = SubrectangleQueries(rectangle)\n * obj.updateSubrectangle(row1,col1,row2,col2,newValue)\n * var param_2 = obj.getValue(row,col)\n */", "__typename": "CodeSnippetNode" }, { "lang": "Go", "langSlug": "golang", "code": "type SubrectangleQueries struct {\n \n}\n\n\nfunc Constructor(rectangle [][]int) SubrectangleQueries {\n \n}\n\n\nfunc (this *SubrectangleQueries) UpdateSubrectangle(row1 int, col1 int, row2 int, col2 int, newValue int) {\n \n}\n\n\nfunc (this *SubrectangleQueries) GetValue(row int, col int) int {\n \n}\n\n\n/**\n * Your SubrectangleQueries object will be instantiated and called as such:\n * obj := Constructor(rectangle);\n * obj.UpdateSubrectangle(row1,col1,row2,col2,newValue);\n * param_2 := obj.GetValue(row,col);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Ruby", "langSlug": "ruby", "code": "class SubrectangleQueries\n\n=begin\n :type rectangle: Integer[][]\n=end\n def initialize(rectangle)\n \n end\n\n\n=begin\n :type row1: Integer\n :type col1: Integer\n :type row2: Integer\n :type col2: Integer\n :type new_value: Integer\n :rtype: Void\n=end\n def update_subrectangle(row1, col1, row2, col2, new_value)\n \n end\n\n\n=begin\n :type row: Integer\n :type col: Integer\n :rtype: Integer\n=end\n def get_value(row, col)\n \n end\n\n\nend\n\n# Your SubrectangleQueries object will be instantiated and called as such:\n# obj = SubrectangleQueries.new(rectangle)\n# obj.update_subrectangle(row1, col1, row2, col2, new_value)\n# param_2 = obj.get_value(row, col)", "__typename": "CodeSnippetNode" }, { "lang": "Scala", "langSlug": "scala", "code": "class SubrectangleQueries(_rectangle: Array[Array[Int]]) {\n\n def updateSubrectangle(row1: Int, col1: Int, row2: Int, col2: Int, newValue: Int) {\n \n }\n\n def getValue(row: Int, col: Int): Int = {\n \n }\n\n}\n\n/**\n * Your SubrectangleQueries object will be instantiated and called as such:\n * var obj = new SubrectangleQueries(rectangle)\n * obj.updateSubrectangle(row1,col1,row2,col2,newValue)\n * var param_2 = obj.getValue(row,col)\n */", "__typename": "CodeSnippetNode" }, { "lang": "Rust", "langSlug": "rust", "code": "struct SubrectangleQueries {\n\n}\n\n\n/** \n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl SubrectangleQueries {\n\n fn new(rectangle: Vec>) -> Self {\n \n }\n \n fn update_subrectangle(&self, row1: i32, col1: i32, row2: i32, col2: i32, new_value: i32) {\n \n }\n \n fn get_value(&self, row: i32, col: i32) -> i32 {\n \n }\n}\n\n/**\n * Your SubrectangleQueries object will be instantiated and called as such:\n * let obj = SubrectangleQueries::new(rectangle);\n * obj.update_subrectangle(row1, col1, row2, col2, newValue);\n * let ret_2: i32 = obj.get_value(row, col);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Racket", "langSlug": "racket", "code": "(define subrectangle-queries%\n (class object%\n (super-new)\n\n ; rectangle : (listof (listof exact-integer?))\n (init-field\n rectangle)\n \n ; update-subrectangle : exact-integer? exact-integer? exact-integer? exact-integer? exact-integer? -> void?\n (define/public (update-subrectangle row1 col1 row2 col2 newValue)\n\n )\n ; get-value : exact-integer? exact-integer? -> exact-integer?\n (define/public (get-value row col)\n\n )))\n\n;; Your subrectangle-queries% object will be instantiated and called as such:\n;; (define obj (new subrectangle-queries% [rectangle rectangle]))\n;; (send obj update-subrectangle row1 col1 row2 col2 new-value)\n;; (define param_2 (send obj get-value row col))", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"98.9K\", \"totalSubmission\": \"112.2K\", \"totalAcceptedRaw\": 98924, \"totalSubmissionRaw\": 112193, \"acRate\": \"88.2%\"}", "hints": [ "Use brute force to update a rectangle and, response to the queries in O(1)." ], "solution": null, "status": null, "sampleTestCase": "[\"SubrectangleQueries\",\"getValue\",\"updateSubrectangle\",\"getValue\",\"getValue\",\"updateSubrectangle\",\"getValue\",\"getValue\"]\r\n[[[[1,2,1],[4,3,4],[3,2,1],[1,1,1]]],[0,2],[0,0,3,2,5],[0,2],[3,1],[3,0,3,2,10],[3,1],[0,2]]\r", "metaData": "{\n \"classname\": \"SubrectangleQueries\",\n \"constructor\": {\n \"params\": [\n {\n \"type\": \"integer[][]\",\n \"name\": \"rectangle\"\n }\n ]\n },\n \"methods\": [\n {\n \"params\": [\n {\n \"type\": \"integer\",\n \"name\": \"row1\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"col1\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"row2\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"col2\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"newValue\"\n }\n ],\n \"name\": \"updateSubrectangle\",\n \"return\": {\n \"type\": \"void\"\n }\n },\n {\n \"params\": [\n {\n \"type\": \"integer\",\n \"name\": \"row\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"col\"\n }\n ],\n \"name\": \"getValue\",\n \"return\": {\n \"type\": \"integer\"\n }\n }\n ],\n \"return\": {\n \"type\": \"boolean\"\n },\n \"systemdesign\": true,\n \"manual\": false\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": true, "envInfo": "{\"cpp\": [\"C++\", \"

Compiled with clang 11 using the latest C++ 20 standard.

\\r\\n\\r\\n

Your code is compiled with level two optimization (-O2). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\"], \"java\": [\"Java\", \"

OpenJDK 17. Java 8 features such as lambda expressions and stream API can be used.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\\r\\n

Includes Pair class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.

\"], \"python\": [\"Python\", \"

Python 2.7.12.

\\r\\n\\r\\n

Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.

\\r\\n\\r\\n

For Map/TreeMap data structure, you may use sortedcontainers library.

\\r\\n\\r\\n

Note that Python 2.7 will not be maintained past 2020. For the latest Python, please choose Python3 instead.

\"], \"c\": [\"C\", \"

Compiled with gcc 8.2 using the gnu11 standard.

\\r\\n\\r\\n

Your code is compiled with level one optimization (-O1). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\\r\\n\\r\\n

For hash table operations, you may use uthash. \\\"uthash.h\\\" is included by default. Below are some examples:

\\r\\n\\r\\n

1. Adding an item to a hash.\\r\\n

\\r\\nstruct hash_entry {\\r\\n    int id;            /* we'll use this field as the key */\\r\\n    char name[10];\\r\\n    UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n    HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n
\\r\\n

\\r\\n\\r\\n

2. Looking up an item in a hash:\\r\\n

\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n    struct hash_entry *s;\\r\\n    HASH_FIND_INT(users, &user_id, s);\\r\\n    return s;\\r\\n}\\r\\n
\\r\\n

\\r\\n\\r\\n

3. Deleting an item in a hash:\\r\\n

\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n    HASH_DEL(users, user);  \\r\\n}\\r\\n
\\r\\n

\"], \"csharp\": [\"C#\", \"

C# 10 with .NET 6 runtime

\"], \"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.

\"], \"ruby\": [\"Ruby\", \"

Ruby 3.1

\\r\\n\\r\\n

Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms

\"], \"swift\": [\"Swift\", \"

Swift 5.5.2.

\"], \"golang\": [\"Go\", \"

Go 1.21

\\r\\n

Support https://godoc.org/github.com/emirpasic/gods@v1.18.1 library.

\"], \"python3\": [\"Python3\", \"

Python 3.10.

\\r\\n\\r\\n

Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.

\\r\\n\\r\\n

For Map/TreeMap data structure, you may use sortedcontainers library.

\"], \"scala\": [\"Scala\", \"

Scala 2.13.7.

\"], \"kotlin\": [\"Kotlin\", \"

Kotlin 1.9.0.

\"], \"rust\": [\"Rust\", \"

Rust 1.58.1

\\r\\n\\r\\n

Supports rand v0.6\\u00a0from crates.io

\"], \"php\": [\"PHP\", \"

PHP 8.1.

\\r\\n

With bcmath module

\"], \"typescript\": [\"Typescript\", \"

TypeScript 5.1.6, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2022 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"], \"racket\": [\"Racket\", \"

Run with Racket 8.3.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }