Codeforces701B Cells Not Under Attack 数学推理

本文介绍了一个关于棋盘上放置车的问题,通过分析车的攻击范围来计算未受攻击格子的数量。文章提供了详细的解题思路及代码实现,适用于算法初学者理解如何利用数据结构解决实际问题。

Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.

The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.

You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.

Input

The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000,1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.

Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.

Output

Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.

Examples
input
3 3
1 1
3 1
2 2
output
4 2 0 
input
5 2
1 5
5 1
output
16 9 
input
100000 1
300 400
output
9999800001 
Note

On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.

题意是放棋子,没有与任何棋子处于同一行的格子数为多少。
观察样例,可以发现一个事实,剩下的格子如果还存在的话,一定能够成一个矩形,我们从这个角度去考虑;
看样例,放了第一个棋子,长宽都小了一,那是不是每放一个棋子,长宽均减一呢,并不是这样的,第二个只是宽度减一;
从这,我们可以看出后放的棋子只有不与先前的棋子同列,宽才减一; 不与先前的棋子同行,长才减一;
可用 v 数组记录前面出现过的棋子的横纵坐标。
两个注意点:答案超 int ;长宽等于0就直接输出0.
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long LL;
int x[100005],y[100005];
bool v1[100005],v2[100005];
int main()
{
    int m;
    __int64 n,p,q;
    while(~scanf("%I64d%d",&n,&m)){
    p=q=n-1;
    for(int i=0;i<m;i++){
        scanf("%d%d",&x[i],&y[i]);
    }
    memset(v1,false,sizeof(v1));
    memset(v2,false,sizeof(v2));
    for(int t=0;t<m;t++){
        if(t==0){
            v1[x[0]]=true;
            v2[y[0]]=true;
            printf("%I64d",p*q);
        }else{
            if(!p||!q){
                printf(" 0");
            }else{
                if(!v1[x[t]]){
                    p--;
                }
                if(!v2[y[t]]){
                    q--;
                }
                v1[x[t]]=v2[y[t]]=true;
                printf(" %I64d",p*q);
            }
        }
    }
    printf("\n");
    }
    return 0;
}



### Codeforces 'Black Cells' 问题解析 在解决 **Codeforces 的 Black Cells** 问题时,通常需要考虑棋盘上的黑色单元格如何影响白色单元格的可达性。此问题的核心在于理解哪些白色单元格可以通过路径到达其他白色单元格而不经过任何黑色单元格。 #### 解决方案概述 一种常见的方法是通过图论中的连通性分析来解决问题。具体来说,可以将整个棋盘视为一个网格图,其中每个白色单元格是一个节点,两个相邻的白色单元格之间存在一条边。如果某些区域被黑色单元格隔断,则这些区域会形成独立的连通分量[^1]。 以下是实现该解决方案的主要思路: 1. 使用广度优先搜索(BFS)或深度优先搜索(DFS)遍历所有白色单元格。 2. 对于每一个未访问过的白色单元格,启动一次 BFS 或 DFS 来标记与其相连的所有白色单元格。 3. 记录每次 BFS 或 DFS 所能触及到的白色单元格数量,并将其作为当前连通分量的大小。 4. 统计所有的连通分量及其对应的大小。 下面提供了一个基于 Python 实现的代码示例,用于演示上述逻辑: ```python from collections import deque def bfs(grid, visited, rows, cols, start_x, start_y): queue = deque() queue.append((start_x, start_y)) visited[start_x][start_y] = True count = 0 directions = [(0, 1), (1, 0), (-1, 0), (0, -1)] while queue: current_x, current_y = queue.popleft() count += 1 for dx, dy in directions: new_x, new_y = current_x + dx, current_y + dy if ( 0 <= new_x < rows and 0 <= new_y < cols and not visited[new_x][new_y] and grid[new_x][new_y] != '*' ): visited[new_x][new_y] = True queue.append((new_x, new_y)) return count def solve_black_cells(grid, rows, cols): visited = [[False]*cols for _ in range(rows)] connected_components = [] for i in range(rows): for j in range(cols): if not visited[i][j] and grid[i][j] != '*': size = bfs(grid, visited, rows, cols, i, j) connected_components.append(size) return connected_components ``` 这段代码定义了 `bfs` 和 `solve_black_cells` 函数,分别负责执行广度优先搜索以及整体处理输入数据并返回各个连通分量的大小列表。 #### 进一步优化与注意事项 尽管上述算法能够有效解决问题,但在实际应用过程中仍需注意以下几点: - 输入规模较大时可能需要进一步优化时间复杂度和空间占用情况; - 特殊边界条件下的行为验证非常重要,比如全黑或者全白的情况; - 如果涉及更多复杂的约束条件,则可能还需要引入动态规划或其他高级技术加以辅助解决[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值