Rectangles and Queries
InterviewTime limit2sMemory limit512 MB
Given an N by N matrix with values at most 10, answer many rectangle queries, each asking how many distinct integers appear inside the submatrix.
- Level
Medium5 of 10
- Topics
- Prefix sum, Matrix, Implementation, Brute force
- Solved
- No attempts yet
Problem
You are given a square matrix with rows and columns. Write a program that answers queries about it.
x1 y1 x2 y2: print the number of distinct integers inside the submatrix whose top left cell is and whose bottom right cell is .
Input
The first line contains (). Each of the next lines contains one row of the matrix as numbers. Rows are numbered from top to bottom and columns from left to right, both starting at 1. Every entry of the matrix is a natural number not greater than 10.
The next line contains (). Each of the next lines contains one query given as , , , , where is a row number and is a column number. (, )
Output
For each query, print the answer on its own line.