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.
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.
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.
3 3 1 1 3 1 2 2
4 2 0
5 2 1 5 5 1
16 9
100000 1 300 400
9999800001
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 数组记录前面出现过的棋子的横纵坐标。
#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;
}
本文介绍了一个关于棋盘上放置车的问题,通过分析车的攻击范围来计算未受攻击格子的数量。文章提供了详细的解题思路及代码实现,适用于算法初学者理解如何利用数据结构解决实际问题。
1509

被折叠的 条评论
为什么被折叠?



