2019CCPC A . Angle Beats(斜率哈希)

题目大意:给定平面内的 n 个点,m次询问,每次询问给出一个点,询问这个点与给定的n个点中的某两个点构成直角三角形的方案数。

n,m<=2000,4000ms

设询问的点为 q 点

分为两种情况,q 点为直角顶点和 q 为锐角顶点。

第一种情况使用一个 map 存下所有其它点与 q 点的斜率,然后在map中找乘积为 -1 的斜率的对数,第二种情况先预处理 n 个点中每个点的每种斜率的点的个数,然后询问的时候遍历一遍算出对应斜率并求查询负倒数斜率的个数即可。

然鹅这两种操作使用map会超时:整体复杂度是O(n^2logn+3nmlogn)

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int, int>
const int maxn = 2010;
pii a[maxn];
map<pii, int> num[maxn];
map<pii, int> g;
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d", &a[i].fi, &a[i].se);
    }
    for (int i = 1; i <= n; i++) //预处理
    {
        for (int j = 1; j <= n; j++)
        {
            if (i == j || a[i] == a[j]) continue;
            int tx = a[i].fi - a[j].fi;
            int ty = a[i].se - a[j].se;
            if (tx && ty)
            {
                int t = __gcd(abs(tx), abs(ty));
                tx /= t, ty /= t; //约分
                //规范斜率使分母非负
                if ((tx > 0 && ty < 0) || (tx < 0 && ty < 0)) tx = -tx, ty = -ty;
            }
            else
            {
                if (tx) tx = 1;
                if (ty) ty = 1;
            }
            num[i][{ tx, ty }]++;
        }
    }
    while (m--)
    {
        g.clear();
        int x, y;
        int ans = 0;
        scanf("%d%d", &x, &y);
        pii p = { x, y };
        for (int i = 1; i <= n; i++)
        {
            if (a[i] == p) continue;
            int tx = a[i].fi - x;
            int ty = a[i].se - y;
            if (tx && ty)
            {
                int t = __gcd(abs(tx), abs(ty));
                tx /= t, ty /= t;
            }
            else
            {
                if (tx) tx = 1;
                if (ty) ty = 1;
            }
            if ((tx > 0 && ty < 0) || (tx < 0 && ty < 0)) tx = -tx, ty = -ty;
            g[{ tx, ty }]++;
        }
        for (auto gg : g)
        {
            if (gg.fi.fi == 0 && gg.fi.se == 1 && g.count({ 1, 0 })) // 特判垂直和水平
            {
                ans += gg.se * g[{ 1, 0 }];
            }
            else if (gg.fi.fi == 1 && gg.fi.se == 0 && g.count({ 0, 1 }))
            {
                ans += gg.se * g[{ 0, 1 }];
            }
            else
            {
                int tx = -gg.fi.se;
                int ty = gg.fi.fi;
                if ((tx > 0 && ty < 0) || (tx < 0 && ty < 0)) tx = -tx, ty = -ty;
                if (g.count({ tx, ty }))
                    ans += gg.se * g[{ tx, ty }];
            }
        }
        ans /= 2;      //每种情况会被计算两次
        for (int i = 1; i <= n; i++)
        {
            if (p == a[i]) continue;
            int tx = a[i].fi - x;
            int ty = a[i].se - y;
            if (tx && ty)
            {
                int t = __gcd(abs(tx), abs(ty));
                tx /= t, ty /= t;
            }
            else
            {
                if (tx) tx = 1;
                if (ty) ty = 1;
            }
            swap(tx, ty);
            if (tx && ty)
            {
                tx = -tx;
                if ((tx > 0 && ty < 0) || (tx < 0 && ty < 0)) tx = -tx, ty = -ty;
            }
            if (num[i].count({ tx, ty }))
                ans += num[i][{ tx, ty }];
        }
        printf("%d\n", ans);
    }
    return 0;
}

使用unordered_map存储斜率的话就能少一个log的复杂度,就可以过了,但是使用存储坐标哈希的方法会莫名的wa掉,然后改成质数的哈希并更改一下遍历的方式就可以过了: 

// Problem: A. Angle Beats
// URL: https://codeforces.com/gym/102361/problem/A
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int, int>
#define ull unsigned long long
const int maxn = 2010;
pii a[maxn];
unordered_map<ull, int> num[maxn];
unordered_map<ull, int> g;
int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}
inline ull has(int x, int y)
{
    return 233ull * x + 2333ull * y + 998244353;
}
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d", &a[i].fi, &a[i].se);
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (i == j || a[i] == a[j]) continue;
            int tx = a[i].fi - a[j].fi;
            int ty = a[i].se - a[j].se;
            if (tx && ty)
            {
                int t = gcd(tx, ty);
                tx /= t, ty /= t;
            }
            else
            {
                if (tx) tx = 1;
                if (ty) ty = 1;
            }
            num[i][has(tx, ty)]++;
        }
    }
    while (m--)
    {
        g.clear();
        int x, y;
        int ans = 0;
        scanf("%d%d", &x, &y);
        pii p = { x, y };
        for (int i = 1; i <= n; i++)
        {
            if (a[i] == p) continue;
            int tx = a[i].fi - x;
            int ty = a[i].se - y;
            if (tx && ty)
            {
                int t = gcd(abs(tx), abs(ty));
                tx /= t, ty /= t;
            }
            else
            {
                if (tx) tx = 1;
                if (ty) ty = 1;
            }
            if ((tx > 0 && ty < 0) || (tx < 0 && ty < 0)) tx = -tx, ty = -ty;
            g[has(tx, ty)]++;
        }
        for (int i = 1; i <= n; i++)
        {
            if (a[i] == p) continue;
            int tx = y - a[i].se;
            int ty = a[i].fi - x;
            if (tx && ty)
            {
                int t = gcd(abs(tx), abs(ty));
                tx /= t, ty /= t;
                ull t1 = has(tx, ty), t2 = has(-tx, -ty);
                if (g.count(t1)) ans += g[t1];
                if (g.count(t2)) ans += g[t2];
            }
            else
            {
                if (tx) tx = 1;
                if (ty) ty = 1;
                ull t = has(tx, ty);
                if (g.count(t)) ans += g[t];
            }
        }
        ans /= 2;
        for (int i = 1; i <= n; i++)
        {
            if (p == a[i]) continue;
            int tx = y - a[i].se;
            int ty = a[i].fi - x;
            if (tx && ty)
            {
                int t = gcd(tx, ty);
                tx /= t, ty /= t;
                ull t1 = has(tx, ty), t2 = has(-tx, -ty);
                if (num[i].count(t1)) ans += num[i][t1];
                if (num[i].count(t2)) ans += num[i][t2];
            }
            else
            {
                if (tx)
                    tx = 1;
                else if (ty)
                    ty = 1;
                ull t = has(tx, ty);
                if (num[i].count(t)) ans += num[i][t];
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

另外官解是极角排序,还有优化排序方式来省掉约分的gcd复杂度的map的版本可以过,但是擦边3.5s过。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值