New School
Contest: Hello 2022
Difficulty rating: 2300
Tags: binary search, data structures, dp, greedy, implementation, sortings

You have decided to open a new school. You have already found $$$n$$$ teachers and $$$m$$$ groups of students. The $$$i$$$-th group of students consists of $$$k_i \geq 2$$$ students. You know age of each teacher and each student. The ages of teachers are $$$a_1, a_2, \ldots, a_n$$$ and the ages of students of the $$$i$$$-th group are $$$b_{i, 1}, b_{i, 2}, \ldots, b_{i, k_i}$$$.

To start lessons you should assign the teacher to each group of students. Such assignment should satisfy the following requirements:

- To each group exactly one teacher assigned.
- To each teacher at most $$$1$$$ group of students assigned.
- The average of students' ages in each group doesn't exceed the age of the teacher assigned to this group.

The average of set $$$x_1, x_2, \ldots, x_k$$$ of $$$k$$$ integers is $$$\frac{x_1 + x_2 + \ldots + x_k}{k}$$$.

Recently you have heard that one of the students will refuse to study in your school. After this, the size of one group will decrease by $$$1$$$ while all other groups will remain unchanged.

You don't know who will refuse to study. For each student determine if you can start lessons in case of his refusal.

Note, that it is not guaranteed that it is possible to start lessons before any refusal.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq m \leq n \leq 10^5$$$) — the number of teachers and the number of groups of students.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the ages of teachers.

The next $$$2m$$$ lines contains descriptions of groups.

The first line of description of group contains a single integer $$$k_i$$$ ($$$2 \leq k_i \leq 10^5$$$) — the number of students in this group.

The second line of description of group contains $$$k_i$$$ integers $$$b_{i, 1}, b_{i, 2}, \ldots, b_{i, k_i}$$$ ($$$1 \leq b_{i, j} \leq 10^5$$$) — the ages of students of this group.

It is guaranteed that the total sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$ and that the total sum of $$$k_1 + k_2 + \ldots + k_m$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$

Output format:
For each test case output string of symbols $$$0$$$ and $$$1$$$ of length $$$k_1 + k_2 + \ldots + k_m$$$. The $$$i$$$-th symbol of this string should be equals $$$1$$$ if it is possible to start lessons in case of the $$$i$$$-th student refuse and it should be equals $$$0$$$ otherwise.

Students are numbered by integers from $$$1$$$ to $$$k_1 + k_2 + \ldots + k_m$$$ in the order they appear in the input. Thus, students of the $$$1$$$-st group are numbered by integers $$$1$$$, $$$2$$$, $$$\ldots$$$, $$$k_1$$$, students of the $$$2$$$-nd group are numbered by integers $$$k_1 + 1$$$, $$$k_1 + 2$$$, $$$\ldots$$$, $$$k_1 + k_2$$$ and so on.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first test case there is one group of students with average age $$$\frac{25+16+37}{3}=26$$$ and one teacher with age $$$30$$$. There exists only one assignment that allows to start lessons.

If the student with age $$$16$$$ will refuse to study, the average age of students in this group will become $$$\frac{25+37}{2}=31$$$ so there won't be any assignment that allows to start lessons.

In the second test case it is impossible to start lessons initially. However, if the $$$3$$$-rd student with age $$$111$$$ will refuse to study, the average ages of groups will become $$$\frac{4 + 5}{2}=4.5$$$ and $$$\frac{11+11}{2} = 11$$$ correspondingly. Then it is possible to assing the first group to the first teacher and the second group to the third teacher.

Example input:
2
1 1
30
3
25 16 37
4 2
9 12 12 6
2
4 5
3
111 11 11

Example output:
101
00100

Editorial:
Suppose, that you know average ages of each group of studens $$$avg_1, avg_2, \ldots, avg_m$$$. How can we check whether we can start lessons? Let's sort this ages and ages of teachers in the decreasing order. Now we have $$$avg_1 \geq avg_2 \geq \ldots \geq avg_m$$$ and $$$a_1 \geq a_2 \geq \ldots \geq a_n$$$. In all the following solution I will assume that average ages of students and teachers are sorted in the decreasing order.
Let's show that we can start lessons if and only if $$$avg_i \leq a_i$$$ for all $$$1 \leq i \leq m$$$. If $$$avg_i > a_i$$$ for some $$$i$$$ then the eldest $$$i$$$ groups can be assigned only to the $$$i-1$$$ eldest teachers, so there is no possible assignment. Otherwise we can assign the $$$i$$$-th group to the $$$i$$$-th teacher.
When one of the students refuse to study, only one value of $$$avg_i$$$ changes to the new value $$$x$$$. Let's denote the new position of this group in the sorted list as $$$j$$$. Then all groups on positions between $$$i$$$ and $$$j$$$ will move by $$$1$$$ towards the initial position of group $$$i$$$.
This position $$$j$$$ can be easily found with binary search. Then we can compare $$$a_j$$$ and $$$x$$$, $$$a_k$$$ with $$$avg_k$$$ for groups that haven't move and $$$a_k$$$ with $$$avg_{k \pm 1}$$$ for groups that have moved to the neighbouring positions. It can be easy done with prefix sum arrays.
We can also show that we can do binary search in each group, but it doesn't decrease time complexity (for example, in case when all groups are small).