zoukankan      html  css  js  c++  java
  • HDU5862 Counting Intersections

    Given some segments which are paralleled to the coordinate axis. You need to count the number of their intersection. 

    The input data guarantee that no two segments share the same endpoint, no covered segments, and no segments with length 0. 

    InputThe first line contains an integer T, indicates the number of test case. 

    The first line of each test case contains a number n(1<=n<=100000), the number of segments. Next n lines, each with for integers, x1, y1, x2, y2, means the two endpoints of a segment. The absolute value of the coordinate is no larger than 1e9. 
    OutputFor each test case, output one line, the number of intersection.Sample Input

    2
    4
    1 0 1 3
    2 0 2 3
    0 1 3 1
    0 2 3 2
    4
    0 0 2 0
    3 0 3 2
    3 3 1 3
    0 3 0 2

    Sample Output

    4
    0

    求当前线段与坐标轴平行的直线的交点

    可以用扫描线的,扫描线or离散化都是一种很神奇的存在方式

    #include<bits/stdc++.h>
    using namespace std;
    const int N=2e5+5;
    struct Node
    {
        int f,x,y,y1;
        bool operator <(const Node &R)const
        {
            return (x==R.x?f<R.f:x<R.x);
        }
    } a[N];
    int Maxn;
    int yy[N],c[N];
    void add(int x,int n)
    {
        for(int i=x; i<=Maxn; i+=i&-i)c[i]+=n;
    }
    int sum(int x)
    {
        int ans=0;
        for(int i=x; i>0; i-=i&-i)ans+=c[i];
        return ans;
    }
    unordered_map<int,int>M;
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            M.clear(),memset(c,0,sizeof c);
            int n,ctot=0,tot=0;
            scanf("%d",&n);
            for(int i=0,x1,x2,y1,y2; i<n; i++)
            {
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                if(x1==x2)
                {
                    if(y1>y2)swap(y1,y2);
                    a[++ctot]= {1,x1,y1,y2};
                    yy[++tot]=y1;
                    yy[++tot]=y2;
                }
                else
                {
                    if(x1>x2)swap(x1,x2);
                    a[++ctot]= {0,x1,y1,1};
                    a[++ctot]= {0,x2+1,y2,-1};
                    yy[++tot]=y1;
                }
            }
            sort(yy+1,yy+tot+1);
            Maxn=0;
            for(int i=1; i<=tot; i++)if(!M[yy[i]])M[yy[i]]=++Maxn;
            sort(a+1,a+ctot+1);
            long long ans=0;
            for(int i=1; i<=ctot; i++)
            {
                if(a[i].f)ans+=(sum(M[a[i].y1])-sum(M[a[i].y]-1));
                else add(M[a[i].y],a[i].y1);
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    dedecmsV5.7和discuz!X3.4整合之后免激活登陆
    dedecms织梦文章微信分享带缩略图与简介
    关于PHP的mkdir函数
    关于discuz的fap.php 漏洞问题
    discuzX3.4安装之后,没有任何样式怎么办?
    阿里云 RDS for MySQL支持什么引擎
    PHP随机生成要求位数个字符(大小写字母+数字)
    PHP json_decode为什么将json字符串转成数组是对象格式?
    PHP实用的功能函数
    css实现三角形图标
  • 原文地址:https://www.cnblogs.com/BobHuang/p/9804172.html
Copyright © 2011-2022 走看看