Editorial for OTHS Coding Competition 2 P4 - Magic Barrier
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Authors:
,Subtask 1:
The first subtask can be brute forced by checking within the range of each query, and identifying if the queried value is present.
Time Complexity:
Subtask 2:
Notice that it is mentioned in the problem statement that all values are unique. Thus, we can store the coordinates of each value present in the original grid using a HashMap. For each query, we can retrieve the coordinates of the value from the HashMap, and check if the coordinates are within the range of the given query.
Note that in languages such as Java, it is more optimal to use HashMap compared to TreeMap.
In Python, a dict can be used, and in C++, an unordered_map or map can be used.
Time Complexity: or
Comments
I missed that the grid consists of unique integers and was not able to solve it. How would the problem be solved if the grid did not have necessarily unique numbers ?
Off the top of my head, you can process the queries offline, by batching them according to their value of . The problem then reduces to 2d range sum queries, which you can do with coordinate compression and a PSA.
You should end up with something that looks like .
If the grid is sorted, you can use a Binary Search approach.