LCC/Moose '19 Contest 4 S5 - Subpairs

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 5.0s
Memory limit: 512M

Author:
Problem type

Given two integers A, B, we define the pair (A, B) to be a subpair of a set S if both A and B are in S.

Given N sets, your task is to answer Q queries of the following form: given two integers A and B, how many of the N sets is (A, B) a subpair of?

Input Specification

The input begins with a single integer N (1 \le N \le 100,000), the number of sets.

The next N lines each describe one of the sets. Each line begins with an integer K and is followed by K numbers A_i (1 \le A_i \le 10^9), the elements of the set. The numbers in each set are guaranteed to be unique. The total number of elements across all sets will not exceed 10^6.

The next line contains a single integer Q (1 \le Q \le 100,000), the number of queries to answer. The next Q lines each contain the pair of different integers A, B (1 \le A, B \le 10^9) to query.

For 30% of points, N \le 100.

Output Specification

Output a single integer: the sum of the answers for each query.

Sample Input 1

3
2 1 3
2 1 2
3 1 2 3
4
1 2
1 3
2 3
4 5

Sample Output 1

5

Comments

There are no comments at the moment.