文章标题 codeforces 701B:Cells Not Under Attack(水+set)

本文介绍了一个关于在n*n的棋盘上放置车的问题。通过使用set记录已放置车的行和列,每次放置后计算未被攻击的单元格数量。提供了一种简单有效的解决方案,并附带了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cells Not Under Attack

Description

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.

Sample Input

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

题意:在一个空的n*n的棋盘上放车,放上的车的列和行就被覆盖了,题目然我们输出没放一个棋子,剩下没被覆盖的数目
分析:可以用set记录已经被放过棋子的行和列,然后:总格子数-行n+列*n-行列。
代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<queue> 
#include<set>
#include<algorithm>
using namespace std;
long long n;
long long m;
long long x[100005],y[100005];
set <long long>hang;
set <long long>lie;
int main ()
{
    while (scanf ("%lld%lld",&n,&m)!=EOF){
        hang.clear() ;
        lie.clear() ;
        for (int i=0;i<m;i++){
            scanf ("%lld%lld",&x[i],&y[i]);
        }
        long long sum=n*n;
        for (int i=0;i<m;i++){
            hang.insert(x[i]);
            lie.insert(y[i]);
            long long temp1=sum;
            long long t1=hang.size() ;
            long long t2=lie.size() ;
            long long temp=t1 * n+t2 *n-t1 *t2 ;
            temp1-=temp;
            if (i==0)printf ("%lld",temp1);
            else printf (" %lld",temp1);
        }
        printf ("\n");
    }       
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值