#P5166. Chain Reaction Running

    ID: 18404 Type: Default 1000ms 256MiB

Chain Reaction Running

Chain Reaction Running

There are n students lined up for running. They are numbered from \(1\) to \(n\) in order from the front to the back. The coach shouts an instruction to speed up running. However, due to a strong wind, only a subset of the students actually hear the instruction. Moreover, any student who did not hear the instruction will still speed up if he observes at least one of his observation targets run faster.

For each student \(i\) (\(1 \le i \le n\)), a set of observation targets is given. These are students with indices strictly smaller than \(i\) (i.e. only those in front of him in the line). In other words, if any student \(j\) in the observation list of student \(i\) speeds up, then student \(i\) will also speed up.

Initially, a teacher instruction is directly delivered only to some students. Afterwards, the chain reaction occurs along the dependency graph defined by these observation relations. To generalize the situation, you are given \(q\) operations of two types:

  • Query: Given three integers 1 L R, assume that all students with indices in the interval \([L, R]\) have heard the teacher's instruction (and hence will run faster initially). What is the minimum number of additional students that must be directly instructed (i.e. forced to run faster) so that eventually all \(n\) students run faster? The chain reaction works as follows: process students in increasing order (from 1 to \(n\)); a student \(i\) will run faster if either he was directly instructed or if at least one of his observation targets (which all have indices smaller than \(i\)) is running faster.
  • Modification: Given four integers 2 L R x (with \(x < L \le R\)), for every student with indices in the interval \([L, R]\), add student \(x\) to his observation targets. (Note that duplicates may arise, but this does not affect the chain reaction.) After each such modification, it is guaranteed that student \(x\) is observed by every student in \([L, R]\).

The input begins with two integers \(n\) and \(q\). Then for each student \(i\) from \(1\) to \(n\), the initial observation targets are provided. The \(i\)th student’s observation list is given on one line with an integer \(k_i\) (the count) followed by \(k_i\) integers which are all less than \(i\).

Following that, there are \(q\) operations in total. For a query operation (type 1), output one line containing the minimum number of additional students that need to be directly instructed so that, after the chain reaction, every student is running faster.

Note: The dependency between students (via observation targets) is always from a lower-index student to a higher-index student, ensuring that the chain reaction is processed in increasing order.

inputFormat

The first line contains two integers \(n\) and \(q\) \( (1 \le n, q \le \text{some limit})\) — the number of students and the number of operations.

Then follow \(n\) lines. The \(i\)th of these lines describes the initial observation targets for student \(i\): it starts with an integer \(k_i\) \( (0 \le k_i < i)\) followed by \(k_i\) integers, each less than \(i\), representing the indices of the students that student \(i\) observes.

Then follow \(q\) lines, each describing an operation. Each operation is in one of the following two formats:

  • Query: 1 L R where \(1 \le L \le R \le n\).
  • Modification: 2 L R x where \(x < L \le R\) (and \(1 \le x < L\)).

It is guaranteed that in every modification operation, the observation edge \(x \to i\) (for all \(i \in [L, R]\)) is added (possibly resulting in duplicates).

outputFormat

For each query operation (type 1), output a single integer denoting the minimum number of additional students that must be instructed directly so that, after propagation, every student from \(1\) to \(n\) is running faster.

sample

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

1 1 0

</p>