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

来源:互联网 发布:贴吧爆吧软件安卓 编辑:程序博客网 时间:2024/05/18 17:26

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;}
0 0
原创粉丝点击