STC08 - Kangaroos
Byteasar, who is interested in wildlife photography, is planning a trip to Australia. He has started preparing his photographic equipment for taking pictures of kangaroos, and has to decide what items to pack. His equipment includes an extensive collection of photographic lenses with varying characteristics for different angles of view and focusing abilities. The best quality of pictures of animals, such as kangaroos, is achieved when the distance between the camera and the animal is within the optimal range of the lens.
Byteasar's trip passes through a sequence of access points for wildlife observation and photography. The Australian guides gave Byteasar a detailed description of these observation points-in particular, the distances at which kangaroos can be expected.
Obviously, not all the lenses from Byteasar's large collection will fit in his luggage, so some decisions must be made. Since Byteasar wishes to keep his lens-changes to a minimum, he wants to find-for each of the lenses-the longest contiguous sequence of observation points on the trip for which that lens is suitable. A lens is suitable for a given observation point if there exists a distance from the range of expected distances that lies within the optimal range of that lens.
The first line of the standard input contains two integers, N and M (1 <= N <= 50000, 1 <= M <= 200000), where N is the number of observation points on the trip and M is the number of lenses in Byteasar's collection.
The next N lines describe the access points for wildlife observation and photography on Byteasar's trip. Each of these lines contains two integers, Ai and Bi (1 <= Ai <= Bi <= 109), indicating that at the i-th observation point kangaroos can appear at a distance of Ai to Bi bytean feet, inclusive.
The next M lines describe the lenses. Each of these lines contains two integers, Li and Ri (1 <= Li <= Ri <= 109), indicating that the i-th lens works best for kangaroos that are at a distance from Li to Ri bytean feet from the camera, inclusive.
The standard output should consist of M lines containing exactly one integer each. The i-th output line indicates the number of observation points in the longest contiguous segment of the trip where the i-th lens can be used by Byteasar. The lenses are numbered according to their order in the input file.
For the input data:
3 3 2 5 1 3 6 6 3 5 1 10 7 9
the correct result is:
2 3 0