Codeforces Beta Round #95 (Div. 2)

E. Yet Another Task with Queens
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A queen is the strongest chess piece. In modern chess the queen can move any number of squares in any horizontal, vertical or diagonal direction (considering that there're no other pieces on its way). The queen combines the options given to the rook and the bishop.

There are m queens on a square n × n chessboard. You know each queen's positions, the i-th queen is positioned in the square (ri, ci), where ri is the board row number (numbered from the top to the bottom from 1 to n), and ci is the board's column number (numbered from the left to the right from 1 to n). No two queens share the same position.

For each queen one can count w — the number of other queens that the given queen threatens (attacks). For a fixed attack direction only the first queen in this direction is under attack if there are many queens are on the ray of the attack. Obviously, for any queen w is between 0 and 8, inclusive.

Print the sequence t0, t1, ..., t8, where ti is the number of queens that threaten exactly i other queens, i.e. the number of queens that their w equals i.

<p< p=""><p< p="">
Input
<p< p="">

The first line of the input contains a pair of integers n, m (1 ≤ n, m ≤ 105), where n is the size of the board and m is the number of queens on the board. Then m following lines contain positions of the queens, one per line. Each line contains a pair of integers ri, ci (1 ≤ ri, ci ≤ n) — the queen's position. No two queens stand on the same square.

<p< p=""><p< p="">
Output
<p< p="">

Print the required sequence t0, t1, ..., t8, separating the numbers with spaces.

<p< p=""><p< p="">
Sample test(s)
<p< p=""><p< p="">
input
8 4
4 3
4 8
6 5
1 6
output
0 3 0 1 0 0 0 0 0 
input
10 3
1 1
1 2
1 3
output
0 2 1 0 0 0 0 0 0 


#include<iostream>
using namespace std;

#define N 300000
struct Point { int r, c; } p[N];
int high[N], low[N], Left[N], Right[N], dia1[N][2], dia2[N][2];

int main()
{
    int n, m;
    cin >> n >> m;
    for ( int i = 1; i < N; i++ )
    {
        high[i] = -1; low[i] = N;
        Left[i] = N; Right[i] = -1;
        dia1[i][0] = N; dia1[i][1] = -1;
        dia2[i][0] = N; dia2[i][1] = -1;
    }

    for ( int i = 1; i <= m; i++ )
    {
        cin >> p[i].r >> p[i].c;
        high[p[i].c] = max ( high[p[i].c], p[i].r );
        low[p[i].c] = min ( low[p[i].c], p[i].r );
        Left[p[i].r] = min ( Left[p[i].r], p[i].c );
        Right[p[i].r] = max ( Right[p[i].r], p[i].c );
        dia1[p[i].r+p[i].c][0] = min ( dia1[p[i].r+p[i].c][0], p[i].c );
        dia1[p[i].r+p[i].c][1] = max ( dia1[p[i].r+p[i].c][1], p[i].c );
        dia2[p[i].r-p[i].c+n][0] = min ( dia2[p[i].r-p[i].c+n][0], p[i].c );
        dia2[p[i].r-p[i].c+n][1] = max ( dia2[p[i].r-p[i].c+n][1], p[i].c );
    }

    int attak[10] = {0};
    for ( int i = 1; i <= m; i++ )
    {
        int cnt = 0;
        if ( p[i].r < high[p[i].c] ) cnt++;
        if ( p[i].r > low[p[i].c] ) cnt++;
        if ( p[i].c < Right[p[i].r] ) cnt++;
        if ( p[i].c > Left[p[i].r] ) cnt++;
        if ( p[i].c > dia1[p[i].r+p[i].c][0] ) cnt++;
        if ( p[i].c < dia1[p[i].r+p[i].c][1] ) cnt++;
        if ( p[i].c > dia2[p[i].r-p[i].c+n][0] ) cnt++;
        if ( p[i].c < dia2[p[i].r-p[i].c+n][1] ) cnt++;
        attak[cnt]++;
    }

    for ( int i = 0; i <= 7; i++ )
        cout << attak[i] << ' ';
    cout << attak[8] << endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值