Intersection Construction
InterviewTime limit2sMemory limit1024 MB
Given N intervals and Q queries [l, r], find the fewest intervals whose intersection is exactly [l, r], or print -1 if no selection works.
- Level
Medium4 of 10
- Topics
- Array, Hash map, Implementation
- Solved
- No attempts yet
Problem
An interval is the set of all real numbers that are at least and at most .
intervals are given. Write a program that answers queries of the following form.
- For the given and , can you choose one or more of the intervals so that their intersection is exactly ? If so, what is the minimum number of intervals you must choose?
Input
The first line contains the number of intervals . ()
Each of the next lines contains two integers and , separated by a space, which describe the interval . ()
The next line contains the number of queries . ()
Each of the next lines contains two integers and , separated by a space. ()
Output
For each query, print one line. If no selection of intervals has intersection exactly , print . Otherwise, print the minimum number of intervals that must be selected.