Angle Beats Gym - 102361A

Angle Beats

 Gym - 102361A 

Given nn points P1,P2,⋯,PnP1,P2,⋯,Pn on 2D plane and qq queries. In ii-th query, a point AiAi is given, and you should determine the number of tuples (u,v)(u,v) that 1≤u<v≤n1≤u<v≤n and Ai,Pu,PvAi,Pu,Pv form a non-degenerate right-angled triangle.

Input

The first line contains two positive integers n,q (2≤n≤2000,1≤q≤2000)n,q (2≤n≤2000,1≤q≤2000), denoting the number of given points and the number of queries.

Next nn lines each contains two integers xi,yi (|xi|,|yi|≤109)xi,yi (|xi|,|yi|≤109), denoting a given point PiPi.

Next qq lines each contains two integers xi,yi (|xi|,|yi|≤109)xi,yi (|xi|,|yi|≤109), denoting a query point AiAi.

It is guaranteed that the input n+qn+q points are all pairwise distinct.

Output

Output qq lines each contains a non-negative integer, denoting the answer to corresponding query.

Example

Input

4 2
0 1
1 0
0 -1
-1 0
0 0
1 1

Output

4
3

Note

For query (0,0)(0,0), the 4 right-angled triangles are

  • {(0,0),(0,1),(1,0)}{(0,0),(0,1),(1,0)}
  • {(0,0),(0,1),(−1,0)}{(0,0),(0,1),(−1,0)}
  • {(0,0),(0,−1),(1,0)}{(0,0),(0,−1),(1,0)}
  • {(0,0),(0,−1),(−1,0)}{(0,0),(0,−1),(−1,0)}

For query (1,1)(1,1), the 3 right-angled triangles are

  • {(1,1),(0,1),(1,0)}{(1,1),(0,1),(1,0)}
  • {(1,1),(0,1),(0,−1)}{(1,1),(0,1),(0,−1)}   

题意:给定平面上n个点,有q次查询,每次查询输入一个点A,求A点与n个点构成的直角三角形的个数

 

这里直角三角形分为两种,一种是以给定的A点为直角的,一种是以n个点中的点为直角的;

 

对于第一种直角,求出A点和n个点之间的斜率,其中斜率相乘为-1,则两线垂直,构成一个直角A,用map先存下A与n个点的斜率,再用O(n)的时间查找,复杂度O(p*n);

 

对于第二种直角,要求n个点中每的点和其他所有点的斜率,再看这个点和A点的斜率有没有和其中相乘为-1的,复杂度O(n*(n+p));

 

另外,对于一个点的斜率y/x,是用一维的map存的,二维的会TLE,方法是先把y和x都除以gcd,使分数最简化;

然后用特定的方法把y和x hash成一个数,查找也用此hash方法产找,这样就降为一维的map了

 

#include<bits/stdc++.h>
using namespace std;
const long long hash_num = 3e9+9;

unordered_map<long long,long long>mp1;
long long Hash(long long x, long long y)
{
    return x*hash_num+y;
}

struct node
{
    long long x,y;
} a[2008],qu[2008];
long long ans[2008];

int main()
{
    long long i,j,m,n,q,xx,yy,gcd,num;
    scanf("%lld %lld",&n, &q);
    for(i = 0; i <n; i++)
    {
        scanf("%lld %lld",&a[i].x,&a[i].y);
    }
    for(i = 0; i < q; i++)
    {
        scanf("%lld %lld",&qu[i].x,&qu[i].y);
    }

    for(i = 0; i < q; i ++)//第一种直角
    {
        mp1.clear();
        for(j = 0; j < n; j++)
        {
            xx = qu[i].x-a[j].x;
            yy = qu[i].y-a[j].y;
            gcd = __gcd(xx,yy);
            xx /= gcd;
            yy /= gcd;
            num = Hash(xx,yy);
            mp1[num]++;
        }
        for(j = 0; j < n; j++)
        {
            xx = qu[i].x-a[j].x;
            yy = qu[i].y-a[j].y;
            gcd = __gcd(xx,yy);
            xx /= gcd;
            yy /= gcd;

            ans[i]+= mp1[Hash(yy,-xx)];
            ans[i]+= mp1[Hash(-yy,xx)];

        }
        ans[i]/=2;
    }

    for(i = 0; i < n; i++)//第二种直角
    {
        mp1.clear();
        for(j = 0; j < n; j++)
        {
            if(i != j)
            {
                xx = a[i].x-a[j].x;
                yy = a[i].y-a[j].y;
                gcd = __gcd(xx,yy);
                xx /= gcd;
                yy /= gcd;
                num = Hash(xx,yy);
                mp1[num]++;
            }
        }
        for(j = 0; j < q; j ++)
        {
            xx = a[i].x-qu[j].x;
            yy = a[i].y-qu[j].y;
            gcd = __gcd(xx,yy);
            xx /= gcd;
            yy /= gcd;
            ans[j]+= mp1[Hash(yy,-xx)];
            ans[j]+= mp1[Hash(-yy,xx)];
        }
    }

    for(i = 0; i < q; i++)
    {
        printf("%lld\n",ans[i]);
    }
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值