Walls
Time limit3sMemory limit256 MB
Each wall must cover every laser column P_j in attack order, and the task asks for the fewest unit moves needed per wall.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
JOI released a TV game, and you got a copy. It is well made, and you have played it every day for fun.
One day, a stage nicknamed 'Laser' appeared among the gamers. It is extremely hard, and even excellent players clear it only with very low probability. After many attempts, you realized that a very fast decision might clear it, and you wondered whether a program could handle it.
The Laser stage is a field with N walls. The field is a rectangle divided into square cells. Each cell is written as with integers . is the bottom-left cell, and is the cell reached from by moving cells right and cells up.
When the stage starts, an enemy appears and attacks. There are M attacks in order. In the -th attack, the enemy fires a laser straight from cell down to cell .
Each wall lies on consecutive cells with the same y coordinate. Wall () is a rectangle of width and height 1. At the start, wall occupies the cells from to . Before the first attack and between attacks, you may move the walls left or right any number of times. One move shifts one wall one cell to the right or one cell to the left.
A laser becomes weaker when it hits a wall. You want to minimize the laser power by moving the walls so that every laser hits every wall. You also want to make as few wall moves as possible.
The initial position of each wall and the position of each enemy attack are given. When every laser hits every wall, find the minimum number of moves for each wall.
Input
The first line contains integers N and M, separated by a space. There are N walls, and the enemy attacks M times.
Each of the next N lines contains integers and , separated by a space. At the start, wall is placed from cell to cell .
Each of the next M lines contains an integer . In the -th attack, the enemy fires a laser from cell to cell .
Output
Print N lines. On the -th line, print the minimum number of moves for wall .
Constraints
()
()