#P5861. [IOI 2015] teams

[IOI 2015] teams

Description

There are NN students in the class, numbered from 00 to N1N-1. Every day, the teacher has some projects that need to be completed by students. Each project must be completed within one day by a team of students. The projects may have different difficulty levels. For each project, the teacher knows how many students should be chosen to form a team to complete it.

Different students have different preferences for team size. More precisely, for student ii, they are only willing to work in a team whose size is between A[i]A[i] and B[i]B[i] (including A[i]A[i] and B[i]B[i]). Each day, a student can be assigned to at most one team. Some students may not be assigned to any team. Each team is responsible for exactly one project.

The teacher has already selected the projects for each of the next QQ days. For each day, you need to determine whether there exists an assignment of students such that every project has a team responsible for it.

Input Format

  • Line 11 contains a positive integer NN, the number of students in the class.
  • Lines 22 to N+1N+1 each contain two integers A[i]A[i], B[i]B[i].
  • Line N+2N+2 contains a positive integer QQ.
  • Lines N+3N+3 to N+Q+2N+Q+2 each contain a positive integer MM, the number of projects to be completed that day, followed by a sequence KK of length MM. K[i]K[i] (1iM1 \le i \le M) denotes the required team size for project ii.

Output Format

Output QQ lines. For each query, your program must output whether there exists a team assignment plan that can complete all projects of that day. If it is possible to form teams to complete all projects for that day, output 1; otherwise, output 0.

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

1
0

Hint

For 100%100\% of the testdata, 1N5×1051 \le N \le 5 \times 10^5, 1Q2×1051 \le Q \le 2 \times 10^5, M2×105\sum M \leq 2 \times 10^5.

Translated by ChatGPT 5