Viruses
Time limit1sMemory limit128 MB
Given up to 24 virus sources with distinct daily hours, determine how many cells each one ends up occupying on an n by n board.
- Level
Hard8 of 10
- Topics
- Geometry, BFS, Sorting, Implementation
- Solved
- No attempts yet
Problem
We have an board whose cells are indexed by pairs of integers with ; the cell is in the bottom-left corner. Every cell starts empty. As time passes, viruses of several kinds appear on the board and begin to multiply.
Each virus has a fixed appearance time, given as a day and an hour, together with a fixed appearance cell . When a virus's appearance moment arrives it occupies its appearance cell, as long as that cell is still empty. If the cell is already occupied by another virus, then this virus never appears and occupies nothing.
From then on, on every following day at the same hour, the virus activates and replicates: every still-empty cell that is adjacent to a cell already held by this virus becomes occupied by that virus. Two cells and are adjacent when (that is, each of the eight surrounding cells). The viruses keep replicating day after day until the whole board is occupied.
Every virus has a distinct hour of appearance, so within any single day the viruses act one after another in increasing order of their hour. Because of this, the virus that ends up owning each cell is determined without ambiguity.
Write a program that reads the description of the viruses and, for each virus, reports how many cells it occupies once the board has been completely filled.
Input
The first line contains two integers and separated by a single space, where and .
Each of the next lines contains four integers , , and separated by single spaces, where and . They are, respectively, the hour, the day, and the coordinates of one virus's appearance. No two viruses share the same hour .
Output
Print integers, one per line. The -th line holds the number of cells occupied by the -th virus (in the input order) once the board is full. A virus that never appears occupies cells.
Notes
In the first sample the table below shows, for each cell, the day on which it became occupied. The rows are drawn so that cell is in the bottom-left corner (the top row is and the bottom row is ), while columns go from on the left to on the right.
The virus scheduled to appear at on day finds that cell already taken, so it never appears and occupies cells.