#K12541. Crossing the Planks
Crossing the Planks
Crossing the Planks
Villagers in a small town need to cross a river using a series of wooden planks. Each plank is represented by an interval on the river. When planks overlap or are contiguous, they form a continuous bridge. The villager can cross safely if both the starting point and the destination lie within the same continuous interval of planks.
More formally, let the planks be represented by intervals \([a_i, b_i]\) for \(i=1,2,\dots,n\). After merging all overlapping or contiguous intervals, the villager can cross from point \(s\) to point \(d\) safely if there exists a merged interval \([L, R]\) such that \(L \leq s \leq R\) and \(L \leq d \leq R\). Otherwise, if no such interval exists, the crossing is considered dangerous.
Your task is to determine for several crossing attempts whether the villager will be safe or in danger.
inputFormat
The input consists of multiple test cases. For each test case:
- The first line contains an integer (n), representing the number of planks.
- Then follow (n) lines, each with two space-separated integers representing the endpoints of a plank.
- The next line contains an integer (m), indicating the number of queries.
- This is followed by (m) lines, each containing two space-separated integers representing the starting point (s) and destination (d) for a crossing attempt.
The input terminates with a test case where (n = 0).
outputFormat
For each query in every test case, output a single line containing either "Safe" if the crossing is possible or "Danger" if it is not.## sample
3
1 5
6 10
12 15
4
0 4
3 9
7 13
10 14
0
Danger
Danger
Danger
Danger
</p>