Codeforces 621B Wet Shark and Bishops 【dp】

来源:互联网 发布:mysql与oracle的关系 编辑:程序博客网 时间:2024/05/15 02:06
B. Wet Shark and Bishops
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.

Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.

Input

The first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops.

Each of next n lines contains two space separated integers xi and yi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.

Output

Output one integer — the number of pairs of bishops which attack each other.

Sample test(s)
input
51 11 53 35 15 5
output
6
input
31 12 33 5
output
0
Note

In the first sample following pairs of bishops attack each other: (1, 3)(1, 5)(2, 3)(2, 4)(3, 4) and (3, 5). Pairs (1, 2)(1, 4)(2, 5)and (4, 5) do not attack each other because they do not share the same diagonal.


题意:有n个象在1000*1000的棋盘上,对角线上的象会互相攻击,问互相攻击的象有多少对。

思路:设置dpl[i][j]表示左上角到(i, j)这一对角线象的个数,同理dpr[i][j]。dp扫一遍就好了。


AC代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <string>#define INF 1000000#define eps 1e-8#define MAXN (200000+10)#define MAXM (100000+10)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while((a)--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1#define PI acos(-1.0)#pragma comment(linker, "/STACK:102400000,102400000")#define fi first#define se secondusing namespace std;typedef pair<int, int> pii;struct Node{    int x, y;};Node num[MAXN];bool cmp(Node a, Node b){    if(a.x != b.x) return a.x < b.x;    else return a.y < b.y;}int dpl[1010][1010], dpr[1010][1010];int main(){    int n; Ri(n); CLR(dpl, 0); CLR(dpr, 0); int x, y;    for(int i = 1; i <= n; i++) Ri(x), Ri(y), dpl[x][y] = dpr[x][y] = 1;    LL ans = 0;    for(int i = 1; i <= 1000; i++)    {        for(int j = 1; j <= 1000; j++)        {            if(dpl[i][j])            {                ans += 1LL*dpl[i-1][j-1];                ans += 1LL*dpr[i-1][j+1];            }            dpl[i][j] += dpl[i-1][j-1];            dpr[i][j] += dpr[i-1][j+1];        }    }    Pl(ans);    return 0;}




0 0