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;
    }
  • 相关阅读:
    取出某个月有几天
    重建索引时,一些数值
    java代理概念
    java内部类和静态内部类
    Lamdba表达式的代码使用讲解
    java 中Vector的使用详解
    mysql 安装失败 start service执行不下去
    Mysql中ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8怎么转换为sql sever2008的代码
    通过命令行发送邮件
    Servlet开发总结(一)
  • 原文地址:https://www.cnblogs.com/BobHuang/p/9804172.html
Copyright © 2011-2022 走看看