codeforces 701 B. Cells Not Under Attack

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

Examples
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 
分析:这道题刚开始没做出来,看了题解。这道题说让你放棋子,当把这个棋子放下后当前行和当前列都会受到这个棋子的攻击,问你,当放下一个棋子后没有受到攻击的格子还有多少。
其实只要统计那些行那些列被棋子占据了即可。用两个set分别保存当前状态下那些行那些列被占据,然后用总棋子数(n*n) 减去被占的行数即:r.size() * n,和被占的列数即: (n - 已受到影的
行)  ( n - r.size()) * l.size(); 就完成了。


 1 /*************************************************************************
 2     > File Name: cfC.cpp
 3     > Author: 
 4     > Mail: 
 5     > Created Time: 2016年08月08日 星期一 13时42分35秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<bits/stdc++.h>
10 using namespace std;
11 typedef long long ll;
12 set<ll> r,l;
13 
14 int main()
15 {
16     ios::sync_with_stdio(false);
17     cin.tie(0);
18     ll n,m;
19     cin >> n >> m;
20     ll x,y;
21     ll ans;
22     ll flag = 0;
23     while(m--)
24     {
25         cin >> x >> y;
26         r.insert(x);
27         l.insert(y);
28         ans = n * n - r.size() * n - (n - r.size()) * l.size();
29         if(flag)
30          cout << ' ';
31         cout << ans;
32         flag++;
33     }
34     cout << endl;
35     return 0;
36 }
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.


原文地址:https://www.cnblogs.com/PrayG/p/5749775.html