Tractor Paths
Time limit4sMemory limit1024 MB
Given N intervals with sorted endpoints, answer queries for the shortest path length between two tractors and count special tractors on some shortest path.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Dynamic programming
- Solved
- No attempts yet
문제
Farmer John has () tractors. Tractor can only be used within the closed interval . The left endpoints satisfy , and the right endpoints satisfy . Some of the tractors are special. Two tractors and are adjacent if and intersect. Farmer John can transfer from one tractor to any adjacent tractor. A path between tractors and is a sequence of transfers that starts at , ends at , and has every pair of consecutive tractors adjacent. It is guaranteed that a path exists between tractor 1 and tractor . The length of a path is the number of transfers, which equals the number of tractors in it minus one. You are given () queries. Each query gives a pair of tractors and ().
입력
The first line contains and . The next line contains a string of length made of L and R, giving the left and right endpoints in sorted order. It is guaranteed that every proper prefix of this string has more Ls than Rs. The next line contains a bit string of length , where each bit says whether the corresponding tractor is special. The next lines each contain two integers and , specifying a query.
출력
For each query, print two integers separated by a space. The first is the length of any shortest path from tractor to tractor . The second is the number of special tractors that lie on at least one shortest path from tractor to tractor .
Hint
The tractor intervals, in order, are , , , , , , , . For the 4th query, there are three shortest paths between tractor 1 and tractor 5: 1 to 2 to 5, 1 to 3 to 5, and 1 to 4 to 5. All of them have length 2. Tractors 1, 2, 3, 4, and 5 each lie on one of these shortest paths. Since tractors 1, 2, 4, and 5 are special, there are 4 special tractors that lie on at least one shortest path from tractor 1 to tractor 5.