Count the database rows that match each query in every column not marked -1.
Easy2Brute forceMatrixInterviewNo attempts yetTime limit1sMemory limit64 MBMirko got a summer internship at a large IT company. The company keeps a database of N rows and M columns.
On his first day Mirko received Q queries. Each query is a list of M numbers. Some of the numbers were lost in transit, and every lost position is written as -1. Mirko wants to know how many rows of the database correspond to each query, that is, how many rows hold the same number as the query in every position that is not -1.
For example, if the query is -1 3 2, count the rows whose first column holds any number, whose second column holds 3, and whose third column holds 2.
Help Mirko and answer all of the queries.
The first line contains two integers N (1≤N≤103) and M (1≤M≤103), the size of the database.
Each of the next N lines contains M integers Aij (1≤Aij≤106), the content of the database.
The next line contains Q (1≤Q≤50), the number of queries.
Each of the next Q lines contains M integers Bij (Bij=−1 or 1≤Bij≤106), the description of the ith query.
Print Q lines. The ith line holds the answer to the ith query.
The database in the first example has four rows. The first query asks how many rows have the number 2 in the third column. Rows 1 (1 5 2) and 3 (4 3 2) qualify, so the answer is 2. The second query asks for rows with 3 in the second column and 2 in the third column. Only row 3 (4 3 2) qualifies, so the answer is 1. The third query is all -1, so it asks for the total number of rows, which is 4.