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;
    }
  • 相关阅读:
    如何在页面加载完成后再去做某事?什么方法可以判断当前页面加载已完成?
    让MySQL支持Emoji表情
    mysql utf8mb4与emoji表情
    移动前端手机输入法自带emoji表情字符处理
    统计网站的每日访问量
    yii中登录后跳转回登录前请求的页面
    YII相关资料(干货)
    [2014-08-24]为 Xamarin Studio 创建的 Asp.Net Mvc 项目配置 gitignore
    [2014-08-28]Mac系统上的几个命令解释器(控制台)
    [2014-08-18]初尝 AspNet vNext On Mac
  • 原文地址:https://www.cnblogs.com/BobHuang/p/9804172.html
Copyright © 2011-2022 走看看