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;
    }
  • 相关阅读:
    自定义控件省市区:仿苹果级联菜单
    【数据存储】SQLite数据库存储(10) 操作通讯记录的ContentProvider
    js跨域的理解与实现
    HTML5学习笔记
    [置顶] 高效前端优化工具Fiddler入门教程
    iis7.0 cpu 限制
    centos下安装apache + subversion(转)
    PHP页面控制访问
    [置顶] 高性能建站之前端优化篇
    [置顶] 构架高性能WEB网站的几点知识
  • 原文地址:https://www.cnblogs.com/BobHuang/p/9804172.html
Copyright © 2011-2022 走看看