#P11619. Non-crossing Intervals
Non-crossing Intervals
Non-crossing Intervals
You are given a number line representing land plots with positions from 1 to n. There are q operations. Originally, the land is empty. Each operation is one of the following two types:
- 1 l r: Place a left bracket at position l and a right bracket at position r. This corresponds to marking an interval [l,r] where a new magical element is found.
- 2 x: Remove the result of operation x. It is guaranteed that operation x was of the first type and has not been deleted before.
After each operation, you must determine if it is possible to collect the brackets from positions 1 to n (if a position has multiple brackets, all left brackets are collected before any right brackets; for the same type, the order can be arbitrary) and arrange them into a valid bracket sequence. Moreover, in this bracket sequence each pair of matching brackets must come from the same operation (i.e. from the same interval insertion).
This additional constraint is equivalent to requiring that for any two intervals (i.e. [l, r] inserted in an operation), their relationship is either one completely contains the other, or they are completely disjoint. In other words, two intervals [l1, r1] and [l2, r2] (assuming l1 < l2) must satisfy either:
- Disjointness: r1 < l2, or
- Nesting: r2 ≤ r1.
For each operation, output YES
if such an arrangement is possible, or NO
otherwise.
Input Constraints:
- 1 ≤ n, q ≤ (constraints unspecified)
- It is guaranteed that each deletion operation (type 2) refers to a valid, previously inserted, non-deleted interval.
inputFormat
The first line contains two integers n and q, representing the number of land plots and the number of operations.
The next q lines describe the operations. Each operation is in one of the following formats:
1 l r
: Place a left bracket at position l and a right bracket at position r.2 x
: Remove the result of the x-th operation (which is of type 1).
Note that positions are 1-indexed. After each operation, you need to decide if the current set of intervals is non-crossing i.e. any two intervals are either nested or disjoint.
outputFormat
Output q lines. After each operation, print YES
if it is possible to form a valid bracket sequence (with the required conditions), or NO
otherwise.
sample
6 5
1 1 4
1 2 3
1 5 6
2 2
1 2 5
YES
YES
YES
YES
NO
</p>