codeforces B. Cells Not Under Attack (数学)

来源:互联网 发布:视频打马赛克软件 编辑:程序博客网 时间:2024/05/01 21:06
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 0001 ≤ 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 31 13 12 2
output
4 2 0 
input
5 21 55 1
output
16 9 
input
100000 1300 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.

题意:给出一个n*n的网格,往里面放m个棋子,每个棋子可以攻击同一行和同一列的位置,问每次放一个棋子之后还有多少安全的位置。
思路:考虑剩下的安全位置的形状是矩形(累加组合),记录剩余的行列数,相乘就是结果。
代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;bool visc[100010],visr[100010];int main(){__int64 n,m;int x,y;memset(visr,0,sizeof(visr));//标记访问的行数 memset(visc,0,sizeof(visc));//标记访问的列数 scanf("%I64d%I64d",&n,&m);__int64 a=n,b=n;for(int i=1;i<=m;i++){scanf("%d%d",&x,&y);if(a==0||b==0)//没有安全位置 {printf("0\n");continue;}if(!visr[x])//这一行还没有访问 {a--;visr[x]=1;}if(!visc[y])//同理 {b--;visc[y]=1;}printf("%I64d\n",a*b);}return 0;}


0 0
原创粉丝点击