• HUST 1376 Random intersection


    神题。同学指教。1秒AC。。。http://blog.csdn.net/jtjy568805874/article/details/50724656

    #include<cstdio>
    #include<cstring>
    #include<ctime>
    #include<algorithm>
    using namespace std;
    
    const int maxn = 100000 + 10;
    struct point
    {
        double x;
        double y;
        int id;
    }p[2 * maxn];
    struct Line
    {
        int a;
        int b;
    }line[maxn];
    int T, n, tot;
    int flag[maxn];
    
    bool cmp(const point&a, const point&b)
    {
        if (a.x == b.x) return a.y < b.y;
        return a.x < b.x;
    }
    
    const double eps = 1e-8;
    #define zero(x)(((x)>0?(x):(-x))<eps)
    
    double xmult(point p1, point p2, point p0)
    {
        return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
    }
    
    int dots_inline(point p1, point p2, point p3)
    {
        return zero(xmult(p1, p2, p3));
    }
    
    int same_side(point p1, point p2, point l1, point l2)
    {
        return xmult(l1, p1, l2)*xmult(l1, p2, l2)>eps;
    }
    
    int dot_online_in(point p, point l1, point l2)
    {
        return zero(xmult(p, l1, l2)) && (l1.x - p.x)*(l2.x - p.x)<eps && (l1.y - p.y)*(l2.y - p.y)<eps;
    }
    
    int intersect_in(point u1, point u2, point v1, point v2)
    {
        if (!dots_inline(u1, u2, v1) || !dots_inline(u1, u2, v2)) return !same_side(u1, u2, v1, v2) && !same_side(v1, v2, u1, u2);
        return dot_online_in(u1, v1, v2) || dot_online_in(u2, v1, v2) || dot_online_in(v1, u1, u2) || dot_online_in(v2, u1, u2);
    }
    
    int main()
    {
        scanf("%d", &T);
        while (T--)
        {
            long long ans = 0;
            scanf("%d", &n); tot = 0; memset(flag, 0, sizeof flag);
            for (int i = 1; i <= n; i++)
            {
                scanf("%lf%lf", &p[tot].x, &p[tot].y); p[tot].id = i; tot++;
                scanf("%lf%lf", &p[tot].x, &p[tot].y); p[tot].id = i; tot++;
            }
            sort(p, p + tot, cmp);
            for (int i = 0; i < tot; i++)
            {
                if (!flag[p[i].id])
                {
                    flag[p[i].id] = 1;
                    line[p[i].id].a = i;
                }
                else
                {
                    line[p[i].id].b = i;
                    p[i].id = -p[i].id;
                }
            }
    
            for (int i = 0; i < tot; i++)
            {
                if (p[i].id>0)
                {
                    int j;
                    for (j = i + 1; p[j].id != -p[i].id; j++)
                    {
                        if (p[j].id > 0)
                        {
                            if (intersect_in(p[line[p[i].id].a], p[line[p[i].id].b], p[line[p[j].id].a], p[line[p[j].id].b])) ans++;
                        }
                    }
                    
                    for (;; j++)
                    {
                        if (j+1<tot&&p[j].x == p[j + 1].x&&p[j].y == p[j + 1].y)
                        {
                            if (p[j+1].id>0)
                            if (intersect_in(p[line[p[i].id].a], p[line[p[i].id].b], p[line[p[j+1].id].a], p[line[p[j+1].id].b])) ans++;
                        }
                        else break;
                    }
                }
    
            }
            printf("%lld
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    Django 之DRF删改查的API接口
    Django 之DRF原理操作
    通过密码、username、时间戳进行md5加密简单脚本
    Java 之GC常用命令
    ORM 之常用查询、字段类型、约束
    ORM 之单表查询案例
    Django session使用
    K8S 之yaml文件参数介绍1
    K8S 之使用Capabilities与限制网络带宽
    K8S 之容器生命周期钩子
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5211771.html
Copyright © 2020-2023  润新知