首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【CodeForces】701B - Cells Not Under Attack(思维)

【CodeForces】701B - Cells Not Under Attack(思维)

作者头像
FishWang
发布2025-08-26 20:39:10
发布2025-08-26 20:39:10
2050
举报

点击打开题目

B. Cells Not Under Attack

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 arenot 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

代码语言:javascript
复制
3 3
1 1
3 1
2 2

output

代码语言:javascript
复制
4 2 0 

input

代码语言:javascript
复制
5 2
1 5
5 1

output

代码语言:javascript
复制
16 9 

input

代码语言:javascript
复制
100000 1
300 400

output

代码语言:javascript
复制
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.

这道题应该想办法转化一下。

如果放上一个棋子,而这个格子还没有受到攻击,那么它将影响一行和一列,我们不管这一行一列能和其他的有多少冲突,我们就把没有被攻击的点都移动到右下角聚集起来,就像上图最左边的一样。最左边的图的结果和在(2,2)位置上放一个棋子的结果是相等的。然后我们就发现,我们要计算的结果就是一个矩形的面积。

那么还有一种情况,就是这个棋子所在的行或者列已经可以被攻击了,那么也很简单,这一行或者列不计算即可。

代码如下:

代码语言:javascript
复制
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
bool w[100000+11];		//x、y被影响的格子数 
bool h[100000+11];
int nx,ny;
int main()
{
	int n,k;
	while (~scanf ("%d %d",&n,&k))
	{
		CLR(w,false);
		CLR(h,false);
		queue<__int64> q;		//队列存结果 
		nx = ny = 0;
		while (k--)
		{
			int x,y;
			scanf ("%d %d",&x,&y);
			if (!h[x])
			{
				h[x] = true;
				nx++;
			}
			if (!w[y])
			{
				w[y] = true;
				ny++;
			}
			q.push((__int64)(n-nx) * (n-ny));
		}
		while (q.size() != 1)
		{
			printf ("%I64d ",q.front());
			q.pop();
		}
		printf ("%I64d\n",q.front());
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-08-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档