Consecutive Ordering
Time limit1sMemory limit256 MB
Decide whether every vertex's closed neighbourhood forms one unbroken block in the given vertex ordering.
- Level
Medium7 of 10
- Topics
- Intervals, Two pointers, Segment tree
- Solved
- No attempts yet
Problem
An interval graph is the intersection graph of a family of closed intervals on the real line. Two vertices and are joined by an edge exactly when the corresponding intervals and intersect. The family is called an interval representation of the graph. A unit interval graph is an interval graph for which some interval representation has all intervals of the same length. Figure 1 shows a unit interval graph together with an interval representation of it.

(a) A unit interval graph .

(b) An interval representation of the graph .
Figure 1. A unit interval graph and its interval representation.
The closed neighbourhood of a vertex of a graph is the set of vertices adjacent to together with itself, that is, . For the graph of Figure 1(a), and .
Write for the position of in an ordering of the vertices. Under the ordering of the vertices of , in which for every , the closed neighbourhood of every vertex is consecutive: the positions of the vertices in are integers that follow one after another, without gaps, from the smallest to the largest. An ordering of the vertices of a graph is consecutive if the closed neighbourhood of every vertex of the graph is consecutive. For the graph of Figure 1(a), the ordering with and exchanged, where , , and for every other vertex, is consecutive as well. The ordering is not consecutive, because the closed neighbourhood of is not consecutive.
You are given closed intervals of the same length, , listed in non-decreasing order of their left endpoints, and an ordering of the vertices of the unit interval graph defined on those intervals. Write a program that decides whether the ordering is consecutive.
Input
Your program reads from standard input. The input consists of test cases, and is given on the first line.
Each test case begins with a line containing two positive integers and , the number of intervals and their common length, where is at most 100,000 and is at most 100,000,000. The next lines contain the left endpoints of , one per line, and the left endpoint of is less than or equal to the left endpoint of whenever . Every endpoint, left or right, lies between -100,000,000 and 100,000,000, inclusive. The following lines contain an ordering of the vertices of the unit interval graph defined on those intervals, one vertex number per line, from the first position to the last.
Output
Your program writes to standard output. For each test case, print exactly one line containing an integer that tells whether the given ordering is consecutive. Print 1 if it is, and -1 otherwise.