2019秦皇岛ccpc A题:Angle Beats[计算几何:统计符合直角三角形的个数]+[向量hash+3hash]

题目链接


题目大意:就是已经知道一些点集合{p},然后q次询问每次给出一个点A,问你这个点和点集合里面多少对点可以构成三角形?


解题思路:很无奈这道题卡常了我们常规思路就是:我们分类讨论A这个点,因为A这个有两种情况就是A是直角点,A不是直角点
1.A是直角点:那么我们就用A这个点然后和点集合里面的每个点去连一条边然后对于每一条边我们存一下斜率然后用map去hash存一下
2.由于斜率有精度误差所有对于一个边我们在直接存x和y值,但是求得是x/y相等的那么统一除以gcd就可以了
3.但是普通的map+gcd会被卡t了,但是unorder_map可以但是这里我们要手写hash去把向量x和yhash才能映射到unorder_map里面
因为直角边会被算两次那么ans要除以2
4.如果A不是直角点:同理就先在点集合里面连边再将A和点集合里面的点相互连边一个个找


#include<bits/stdc++.h>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define hash Hash
#define next Next
#define pb push_back
#define f first
#define s second
#define y1 Y
using namespace std;
const int N = 4e3 + 10, mod = 1e9 + 7;
const int maxn = 4e5 + 10;
const long double eps = 1e-5;
const int EPS = 500 * 500;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) 
{
   read(first);
   read(args...);
}
int n, q;
struct Point
{
    int x, y;
    Point operator - (Point const & a) const {
        return (Point){x - a.x , y - a.y};
    } 
}point[N], query[N];
unordered_map<ll,int> mp;
int ans[N];

ll Hash(Point p)
{
   ll A = 233, B = 5279, C = 998244353;
   return p.x * A + p.y * B + C;
}

inline Point col(Point a)
{
    if(a.x == 0) a.y = 1;
    else if(a.y == 0) a.x = 1;
    else 
    {
        int gcd = __gcd(a.x,a.y);
        a.x /= gcd;
        a.y /= gcd;
        if(a.y < 0) a.y = -a.y, a.x = -a.x;
    }
    return a;
} 

int main()
{
   while(scanf("%d%d",&n,&q) != EOF)
   {
       for(int i = 0; i < n; ++ i) ans[i] = 0;
        for(int i = 0; i < n; ++ i)
        {
           int l, r;
           scanf("%d%d",&l,&r);
           point[i] = {l,r};
        }

        for(int i = 0; i < q; ++ i)
        {
           int l, r;
           scanf("%d%d",&l,&r);
           query[i] = {l,r};
        }
    //假如A是直角点
        for(int i = 0; i < q; ++ i)
        {
            mp.clear();
            for(int j = 0; j < n; ++ j)
            {
                Point tmp = query[i] - point[j];
                mp[Hash(col(tmp))] ++; 
            }
            for(int j = 0; j < n; ++ j)
            {
                Point tmp = query[i] - point[j];
                tmp = (Point){-tmp.y,tmp.x}; 
                tmp = col(tmp);
                ans[i] += mp[Hash(tmp)];
            }
            // cout << ans[i] << endl;
            ans[i] >>= 1;
        }
    //假如A不是直角点
      for(int i = 0; i < n; ++ i)
       {
           mp.clear();
           for(int j = 0; j < n; ++ j)
           {
                if(i == j) continue;
                mp[Hash(col(point[i]-point[j]))] ++;
           }

           for(int j = 0; j < q; ++ j)
           {
               Point tmp = point[i] - query[j];
               tmp = (Point){-tmp.y,tmp.x};
               tmp = col(tmp);
               ans[j] += mp[Hash(tmp)];
           }
       }
       for(int i = 0; i < q; ++ i) printf("%d\n",ans[i]);
   }
   return 0;
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值