Nested ranges
Time: 1 s · Memory: 256 MB
- Sorting
Statement
You are given ranges . For every range answer two questions: does it contain some other range, and is it contained in some other range?
Range contains if and . Identical ranges contain each other; a range does not count itself.
Input format
The first line contains (). The next lines contain and ().
Output format
The first line contains numbers: 1 if range contains another range, otherwise 0. The second line contains numbers: 1 if range is contained in another range, otherwise 0.
Examples
Input
4 1 6 2 4 4 8 3 6
Output
1 0 0 0 0 1 0 1
Note
Sort the ranges by left end in increasing order and, for equal left ends, by right end in decreasing order. Then every range before has left end : range is contained in another the largest right end among the previous ones is . Similarly, range contains another the smallest right end among the following ones is . Two passes (prefix maximum and suffix minimum) — .
Handle identical ranges separately: one of them comes first in sorted order, yet it also lies inside the next one. In the example contains and .
Source: CSES, problem 2168.
