CodeForces - 651C(map,make_pair)

Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi).

They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan considers the distance between watchmen i and j to be |xi - xj| + |yi - yj|. Daniel, as an ordinary person, calculates the distance using the formula .

The success of the operation relies on the number of pairs (i, j) (1 ≤ i < j ≤ n), such that the distance between watchman i and watchmen j calculated by Doctor Manhattan is equal to the distance between them calculated by Daniel. You were asked to compute the number of such pairs.

Input

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

Each of the following n lines contains two integers xi and yi (|xi|, |yi| ≤ 109).

Some positions may coincide.

Output

Print the number of pairs of watchmen such that the distance between them calculated by Doctor Manhattan is equal to the distance calculated by Daniel.

Example
Input
3
1 1
7 5
1 5
Output
2
Input
6
0 0
0 1
0 2
-1 1
0 1
1 1
Output
11
Note      其实就是看看有多少一样的横坐标或者纵坐标,注意可能有相同的点。
//111.CPP
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <string>
#define LL long long
using namespace std;
map<int, int>x;
map<int, int>y;
map<pair<int, int>,int>xy;
int main()
{
	 int n;
	 cin>>n;int xx,yy;
	 long long  t=0;
	 for(int i=0;i<n;i++)
     {
         scanf("%d%d",&xx,&yy);
         t+=x[xx]++;
         t+=y[yy]++;
         t-=xy[make_pair(xx,yy)]++;
     }
     cout<<t<<endl;
}

Pairs 

C++标准程序库中凡是“必须返回两个值”的函数, 也都会利用pair对象 
class

pair可以将两个值视为一个单元。容器类别map和multimap就是使用pairs来管理其健值/实值(key/va

lue)的成对元素。 
pair被定义为struct,因此可直接存取pair中的个别值.

两个pairs互相比较时, 第一个元素正具有较高的优先级. 
例: 
namespace std{ 
template <class T1, class T2> 
bool operator< (const pair<T1, T2>&x, const pair<T1, T2>&y){ 
return x.first<y.first || ((y.first<x.first)&&x.second<y.second); 

}

make_pair():

无需写出型别, 就可以生成一个pair对象 
例: 
std::make_pair(42, '@'); 
而不必费力写成: 
std::pair<int, char>(42, '@')

当有必要对一个接受pair参数的函数传递两个值时, make_pair()尤其显得方便, 
void f(std::pair<int, const char*>);

void foo{ 
f(std::make_pair(42, '@')); //pass two values as pair 
}

1 pair的应用

pair是将2个数据组合成一个数据,当需要这样的需求时就可以使用pair,如stl中的map就是将key和value放在一起来保存。另一个应用是,当一个函数需要返回2个数据的时候,可以选择pair。 pair的实现是一个结构体,主要的两个成员变量是first second 因为是使用struct不是class,所以可以直接使用pair的成员变量。

2 make_pair函数

template pair make_pair(T1 a, T2 b) { return pair(a, b); }

很明显,我们可以使用pair的构造函数也可以使用make_pair来生成我们需要的pair。 一般make_pair都使用在需要pair做参数的位置,可以直接调用make_pair生成pair对象很方便,代码也很清晰。 另一个使用的方面就是pair可以接受隐式的类型转换,这样可以获得更高的灵活度。灵活度也带来了一些问题如:

std::pair<int, float>(1, 1.1);

std::make_pair(1, 1.1);

是不同的,第一个就是float,而第2个会自己匹配成double。

参考:https://www.cnblogs.com/zzy19961112/p/6790797.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值