This page is still under construction.

Parts of this page are still being built. What you see may change.

Forming Teams

Time limit4sMemory limit512 MB

Summary
For each planned day, decide whether students with accepted size ranges can fill all requested teams of the given sizes.
Level

Hard8 of 10

Topics
Greedy, Intervals, Sorting, Segment tree
Solved
No attempts yet

Problem

There are NN students numbered 00 through N−1N-1. Every day the teacher prepares one or more projects, and each project is handled by one team that the students form on that day. Projects differ in difficulty, so the size of the team that handles a project is fixed in advance.

Students differ in the team sizes they accept. Student ii can join a team only if that team has at least AiA_i and at most BiB_i members. On a single day a student belongs to at most one team, and a student may belong to no team at all. One team handles exactly one project.

If a day has MM projects and the team for project jj must have size KjK_j, then teams of sizes K0,K1,…,KM−1K_0, K_1, \ldots, K_{M-1} all have to exist on that day at the same time. The sum of the KjK_j can exceed NN.

Teams are formed again from scratch every day, so the teams of one day do not restrict any other day. The teacher has already planned QQ days. For each day, decide whether all the teams planned for that day can be formed.

Input

The first line contains the number of students NN. (1≤N≤5000001 \le N \le 500000)

Each of the next NN lines contains AiA_i and BiB_i separated by a space, in the order i=0,1,…,N−1i = 0, 1, \ldots, N-1. (1≤Ai≤Bi≤N1 \le A_i \le B_i \le N)

The next line contains the number of days QQ. (1≤Q≤2000001 \le Q \le 200000)

Each of the next QQ lines describes one day, in the planned order. A line contains the number of projects MM followed by the team sizes K0,K1,…,KM−1K_0, K_1, \ldots, K_{M-1}, with all numbers separated by spaces. (1≤M≤N1 \le M \le N, 1≤Kj≤N1 \le K_j \le N)

The sum of MM over all days is at most 200000200000.

Output

For each day, print 11 if all the teams planned for that day can be formed, and 00 otherwise. Print one answer per line, in the order the days are given.

Examples2

  1. Example 1

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

    Input
    5
    1 5
    1 5
    1 5
    1 5
    1 5
    3
    5 1 1 1 1 1
    3 2 2 1
    2 3 3
    
    Expected output
    1
    1
    0