Book Club

Interview

Time limit1sMemory limit128 MB

Summary
Given each of N cows' answers to NQ questions, count how many cows match all P given question-answer pairs simultaneously.
Level

Medium4 of 10

Topics
Hash map, Implementation, Array, Brute force
Solved
No attempts yet

Problem

Bessie is looking for cows to join her book club. The herd has NN (2≤N≤50,0002 \le N \le 50{,}000) cows numbered 1…N1 \ldots N, but she wants only the most discerning and social cows.

Like a dating service, she created a questionnaire and asked each cow to answer its NQNQ (1≤NQ≤501 \le NQ \le 50) questions (named R1…RNQR_1 \ldots R_{NQ}). These are questions such as "How much do you enjoy reading science fiction?", and every answer is an integer from 11 to 55.

Your job is to tabulate the results and answer a query such as: "How many cows answered 22 to question 33, 44 to question 77, and 11 to question 88?" A query has PP (1≤P≤101 \le P \le 10) parts; part jj gives a question number QjQ_j (1≤Qj≤NQ1 \le Q_j \le NQ) and a required answer AjA_j (1≤Aj≤51 \le A_j \le 5). Output a single integer: the number of cows that answered AjA_j to question QjQ_j for every jj from 11 to PP. A cow is counted only when it satisfies all PP parts at once.

For instance, a herd of 44 cows answering 55 questions might respond like this:

Cow     Question
 ID   1  2  3  4  5
 1    1  1  1  1  1
 2    1  2  3  4  5
 3    1  2  1  2  3
 4    2  1  1  2  2

Each row is one cow's answers; column kk is that cow's answer to question kk.

Input

  • Line 11: three space-separated integers NN, NQNQ, and PP.
  • Lines 2…N+12 \ldots N+1: line i+1i+1 contains NQNQ space-separated integers, cow ii's answers R1…RNQR_1 \ldots R_{NQ}.
  • Lines N+2…N+1+PN+2 \ldots N+1+P: line j+N+1j+N+1 contains two space-separated integers QjQ_j and AjA_j.

Output

  • Line 11: a single integer, the number of cows that satisfy all of Bessie's criteria.

Examples2

  1. Example 1

    Input
    4 5 2
    1 1 1 1 1
    1 2 3 4 5
    1 2 1 2 3
    2 1 1 2 2
    1 1
    3 1
    
    Expected output
    2
    
  2. Example 2

    Input
    4 5 2
    1 1 1 1 1
    1 2 3 4 5
    1 2 1 2 3
    2 1 1 2 2
    1 2
    2 1
    
    Expected output
    1