New Home
Time limit5sMemory limit1024 MB
Stores of k types each occupy a point and an open year interval; for each (location, year) query, report the maximum over types of the distance to the nearest open store of that type, or -1 if some type is missing.
- Level
Hard9 of 10
- Topics
- Segment tree, Binary search, Sorting, Divide and conquer
- Solved
- No attempts yet
Problem
Wu-Fu Street is a perfectly straight street. It is described as a one-dimensional number line, so every building on it sits at a single number. Xiao-Ming the time traveler knows every store that opened, is open, or will open on the street: there are stores of types. Store is given by four integers , , , , meaning the location of the store, the type of the store, the year its business starts, and the year its business ends. Store is open in year when .
Xiao-Ming wants to pick a year and a location on Wu-Fu Street to live in, and he narrowed his preference list to pairs of a location and a year. Pair is given by two integers , . He rates a pair by its inconvenience index, the inaccessibility of the store type that is hardest to reach. The inaccessibility of type for a pair is the distance from the location to the nearest type- store that is open in that year, so the inconvenience index is the largest of those distances over all types. In some years Wu-Fu Street does not carry all types. The inconvenience index of such a pair is .
Find the inconvenience index of every pair.
Input
The first line contains three integers , , : the number of stores, the number of types, and the number of queries (, ).
Each of the next lines describes one store with four integers , , , (, , ).
Each of the next lines contains one query, two integers and ().
Output
Print integers, one per line, in the order the queries are given. The -th of them is the inconvenience index of the -th query.
Hint
The first sample has four stores, two types, and four queries.
- First query: Xiao-Ming lives at location 5 in year 3. Stores 1 and 2 are open that year, the distance to store 1 is 2, and the distance to store 2 is 4. The answer is 4.
- Second query: he lives at location 5 in year 6. Stores 1 and 3 are open and both are at distance 2. The answer is 2.
- Third query: he lives at location 5 in year 9. Stores 1 and 4 are open and both have type 1, so no store of type 2 is open. The answer is .
- Fourth query: the same situation, so the answer is .
The second sample has two stores, one type, and three queries. Both stores are at location 1, and every query asks about location 1. At least one store is open in the first two queries, so the answer is 0. Both stores are closed in the third query, so the answer is .
The third sample has one store and one query. The distance between the two locations is 99999999.