This page is still under construction.

Parts of this page are still being built. What you see may change.

Viruses

Time limit1sMemory limit128 MB

Summary
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 n×nn \times n board whose cells are indexed by pairs of integers (x,y)(x, y) with 1≤x,y≤n1 \le x, y \le n; the cell (1,1)(1, 1) 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 (x,y)(x, y). 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 (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) are adjacent when max⁡(∣x2−x1∣,∣y2−y1∣)=1\max(|x_2 - x_1|, |y_2 - y_1|) = 1 (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 nn and kk separated by a single space, where 1≤n≤10000001 \le n \le 1000000 and 1≤k≤241 \le k \le 24.

Each of the next kk lines contains four integers hh, dd, xx and yy separated by single spaces, where 0≤h≤230 \le h \le 23 and 1≤d,x,y≤n1 \le d, x, y \le n. They are, respectively, the hour, the day, and the coordinates of one virus's appearance. No two viruses share the same hour hh.

Output

Print kk integers, one per line. The ii-th line holds the number of cells occupied by the ii-th virus (in the input order) once the board is full. A virus that never appears occupies 00 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 (1,1)(1, 1) is in the bottom-left corner (the top row is y=5y = 5 and the bottom row is y=1y = 1), while columns go from x=1x = 1 on the left to x=5x = 5 on the right.

54333
44323
34333
44322
54321

The virus scheduled to appear at (3,3)(3, 3) on day 33 finds that cell already taken, so it never appears and occupies 00 cells.

Examples2

  1. Example 1

    Input
    5 4
    1 2 4 4
    2 1 5 1
    0 3 1 3
    3 3 3 3
    
    Expected output
    10
    7
    8
    0
    
  2. Example 2

    Input
    1 1
    7 1 1 1
    
    Expected output
    1