PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000153. Thành lập các đội (Teams)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Thành lập các đội (Teams)

Teams

Source: IOI

Version: Phuoc Hung OJ Extended

Problem Statement

There are NN students numbered 11 through NN. Student ii agrees to join a team only if its size lies within the inclusive integer interval [Ai,Bi][A_i,B_i].

On a given day, there are MM projects. Project jj requires one team of exactly KjK_j students. Each student can belong to at most one team during that day. On subsequent days, students are available again.

For each day, determine whether all MM projects can be assigned disjoint teams of their required sizes, with every participating student accepting the size of their team. Unused students are allowed.

PHOJ adaptation: The original IOI grader calls init(N,A,B) once and then calls can(M,K) for each query. The following standard-I/O form preserves the exact mathematical task and responses.

Input

  • The first line contains integer NN.
  • The next NN lines each contain two integers AiA_i and BiB_i for student ii.
  • The next line contains integer QQ, the number of days.
  • Each of the next QQ lines begins with integer MjM_j, followed by MjM_j integers K1,…,KMjK_1,\ldots,K_{M_j}.

Output

For each day print 1 if all required disjoint teams can be formed and 0 otherwise. Print the answers in query order, one per line.

Subtasks

  • Subtask 1 (21 points): 1≤N≤1001\le N\le 100; 1≤Ai≤Bi≤N1\le A_i\le B_i\le N; 1≤Q≤1001\le Q\le 100; 1≤Mj≤N1\le M_j\le N; 1≤Kt≤N1\le K_t\le N; 1≤S=∑j=1QMj≤QN1\le S=\sum_{j=1}^Q M_j\le QN.
  • Subtask 2 (13 points): 1≤N≤1000001\le N\le 100000; 1≤Ai≤Bi≤N1\le A_i\le B_i\le N; Q=1Q=1; 1≤Mj≤N1\le M_j\le N; 1≤Kt≤N1\le K_t\le N; 1≤S=∑j=1QMj≤QN1\le S=\sum_{j=1}^Q M_j\le QN.
  • Subtask 3 (43 points): 1≤N≤1000001\le N\le 100000; 1≤Ai≤Bi≤N1\le A_i\le B_i\le N; 1≤Q≤1000001\le Q\le 100000; 1≤Mj≤N1\le M_j\le N; 1≤Kt≤N1\le K_t\le N; 1≤S=∑j=1QMj≤1000001\le S=\sum_{j=1}^Q M_j\le 100000.
  • Subtask 4 (23 points): 1≤N≤5000001\le N\le 500000; 1≤Ai≤Bi≤N1\le A_i\le B_i\le N; 1≤Q≤2000001\le Q\le 200000; 1≤Mj≤N1\le M_j\le N; 1≤Kt≤N1\le K_t\le N; 1≤S=∑j=1QMj≤2000001\le S=\sum_{j=1}^Q M_j\le 200000.

Examples

Input

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

Output

1
0

Explanation

The four students accept team sizes [2,4][2,4], [1,2][1,2], [2,3][2,3], $[2,3]. The first query asks for teams of sizes $1$ and $3$: the second student fills the one-person team, and the three others form the second, giving 1. The second query asks for two one-person teams. Only the second student can join either, so the answer is 0`.