• CodeForces


    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

    In the first sample, the distance between watchman 1 and watchman 2 is equal to |1 - 7| + |1 - 5| = 10 for Doctor Manhattan and for Daniel. For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same distances.

    对题目给出的条件化简,得知满足条件的一对点的关系为(x1==x2||y1==y2)

    直接用数组存储,然后每个点之间进行比较的时间复杂度为O(n^2),题目中n=2e5,因此要采用降低时间复杂度的方法,那么使用map是最佳的选择。

    事实上,除去重合的点的情况,我们不需要知道点的两个坐标,只要知道一条线上有多少个点,若一条线有n个点,则该线上有n(n-1)/2对点满足题目条件。

    那么考虑重合的点,若有n个点坐标为(x,y),那么在x线和y线上,这n个点被统计了两次,则减去n(n-1)/2对点即可。

    综上,用三个map分别存储x坐标,y坐标和重复的点,代码如下:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<map>
    using namespace std;
    const int MAX=2e5+10;
    #define INF 0x7fffffff
    #define ll long long
    #define FOR(i,n) for(i=1;i<=n;i++)
    #define mst(a) memset(a,0,sizeof(a))
    typedef pair<int,int> p;
    map<int,int> x,y;
    map<p,int> mp;
    
    int main()
    {
        ll m,n,i,j,a,b,ans=0;
        cin>>n;
        FOR(i,n)
        {
            cin>>a>>b;
            x[a]++;
            y[b]++;
            mp[p(a,b)]++;
        }
        map<int,int>::iterator it;
        map<p,int>::iterator itt;
        for(it=x.begin();it!=x.end();it++)
        {
            j=it->second;
            ans+=j*(j-1)/2;
        }
        for(it=y.begin();it!=y.end();it++)
        {
            j=it->second;
            ans+=j*(j-1)/2;
        }
        for(itt=mp.begin();itt!=mp.end();itt++)
        {
            j=itt->second;
            ans-=j*(j-1)/2;
        }
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    【C#】调度程序进程已挂起,但消息仍在处理中;
    【WCF】wcf不支持的返回类型
    power design教程
    C# 动态修改Config
    【C#】delegate(委托) 将方法作为参数在类class 之间传递
    【.NET】Nuget包,把自己的dll放在云端
    【C#】C# 队列,
    【Chrome】新建Chrome插件,新建,事件行为,本地存储
    【IIS】iis6.1下添加两个ftp站点,
    【Jquery】$.Deferred 对象
  • 原文地址:https://www.cnblogs.com/qq936584671/p/7125844.html
Copyright © 2020-2023  润新知