Broadcast Tower Offers
Time limit2sMemory limit512 MB
For each offered tower height, find the best position along a row of buildings and report how many buildings to its west can receive its westward signal.
- Level
Hard8 of 10
- Topics
- Stack, Sorting, Prefix sum, Binary search
- Solved
- No attempts yet
Problem
City X has buildings standing in one row from west to east. The westmost building is number 1 and the eastmost is number . The heights are integers , and no two buildings have the same height. The city government plans to build one broadcast tower in the same row. The tower can stand west of building 1, between two neighbouring buildings, or east of building . Its height is , and differs from every building height.
Because of an unusual design, the tower sends signals only to the west. A signal is a horizontal ray that travels parallel to the ground, and rays leave the whole body of the tower, from the top down to the bottom. The tower therefore emits a continuous band of rays whose width equals the height of the tower. A ray stops where it hits a building. Each building has a receiver on its roof, and a building receives the message if at least one ray reaches that receiver.
In other words, building receives the message exactly when all three conditions hold: building stands west of the tower, is not greater than the height of the tower, and no building with standing between building and the tower is higher than building .

In the figure above the buildings that receive the message are 2, 5, 6, and 9.
Only one tower will be built, but the city government received offers of different heights. The offers are numbered from 1 to . Every offered height differs from the other offered heights and from every building height. Before the decision is made, the city leaders want to know, for each offer, the largest number of buildings that receive the message. Each offer is measured on its own best position.
You are given the heights of the buildings and the heights of the offered towers. Write a program that prints, for each offer, the largest number of buildings that receive the message.
Input
The first line contains the number of buildings and the number of offers , separated by a space.
The second line contains the heights of the buildings, separated by spaces, in order from building 1 to building .
The third line contains the heights of the offered towers, separated by spaces, in order from offer 1 to offer .
Output
Print integers on one line, separated by spaces. The -th number is the largest number of buildings that receive the message when the tower of offer is built in its best position.
Constraints
- Every building height and every offered tower height is an integer between and
- The building heights are pairwise different
- The offered heights are pairwise different, and none of them equals a building height
Explanation
The pictures below show a best position for each offer in the example.

Position of tower 1. The buildings that receive the message are 10, 12, 13, 15, and 16.

Position of tower 2. The buildings that receive the message are 2, 3, 5, 6, 7, and 8.

Position of tower 3. The buildings that receive the message are 12, 13, 15, and 16.