codeforces #345 div 2 C. Watchmen (容斥)
题目链接 题意:求曼哈顿距离和平方根距离相等的点的对数? 思路:化简发现是绝对值乘积等于0,容斥搞搞。
/* ***********************************************
Author :111qqz
Created Time :2016年03月07日 星期一 18时43分02秒
File Name :code/C.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=2E6+7;
1struct node
2{
3 int x,y;
1 bool operator < (node b)const
2 {
3 if (x==b.x) return y<b.y;
4 return x<b.x;
5 }
6}p[N];
1bool cmp( node a,node b)
2{
3 return a.y<b.y;
4}
5LL cal( LL x)
6{
7 LL res = x*(x+1)/2;
8 return res;
9}
10int n;
11LL a,b,c;
12LL cnta,cntb,cntc;
13int main()
14{
15 #ifndef ONLINE_JUDGE
16 freopen("code/in.txt","r",stdin);
17 #endif
1 ios::sync_with_stdio(false);
2 cin>>n;
3 for ( int i = 1 ;i <= n ; i++) cin>>p[i].x>>p[i].y;
sort(p+1,p+n+1);
a=b=c=0LL;
cnta=cntb=cntc=0LL;
1 for ( int i = 1 ; i <= n-1 ; i++)
2 {
3 if (p[i].x==p[i+1].x)
4 {
5 cnta++;
6 }
7 else
8 {
9 a+=cal(cnta);
10 cnta = 0 ;
11 }
12 }
13 a +=cal(cnta);
1 for ( int i = 1 ; i <= n-1 ; i++)
2 {
3 if (p[i].x==p[i+1].x&&p[i].y==p[i+1].y)
4 {
5 cntc++;
6 }
7 else
8 {
9 c +=cal(cntc);
10 cntc = 0 ;
11 }
12 }
c+=cal(cntc);
1 sort(p+1,p+n+1,cmp);
2 for ( int i = 1 ; i <= n-1 ; i ++)
3 {
4 if (p[i].y==p[i+1].y)
5 {
6 cntb++;
7 }
8 else
9 {
10 b +=cal(cntb);
11 cntb = 0 ;
12 }
13 }
14 b +=cal(cntb);
1 LL ans=0LL;
2 ans = a+b-c;
3 cout<<ans<<endl;
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}