Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)

Total Submission(s): 201 Accepted Submission(s): 77

Problem Description

There is a saying: Life is like a line, some people are your parallel lines, while others are destined to meet you.
Maybe have met, maybe just a matter of time, two unparallel lines will always meet in some places, and now a lot of life (i.e. line) are in the same coordinate system, in a given open interval, how many pairs can meet each other?

Input

There are several test cases in the input.
Each test case begin with one integer N (1 ≤ N ≤ 50000), indicating the number of different lines.
Then two floating numbers L, R follow (-10000.00 ≤ L < R ≤ 10000.00), indicating the interval (L, R).
Then N lines follow, each line contains four floating numbers x1, y1, x2, y2 (-10000.00 ≤ x1, y1, x2, y2 ≤ 10000.00), indicating two different points on the line. You can assume no two lines are the same one.
The input terminates by end of file marker.

Output

For each test case, output one integer, indicating pairs of intersected lines in the open interval, i.e. their intersection point’s x-axis is in (l, r).

Sample Input

3
0.0 1.0
0.0 0.0 1.0 1.0
0.0 2.0 1.0 2.0
0.0 2.5 2.5 0.0

Sample Output

1

Author

iSea @ WHU

Source

2010 ACM-ICPC Multi-University Training Contest(3)——Host by WHU

Recommend

zhouzeyong

题意:

给一个开区间(l,r),给n个直线,问这些直线在这段区间里面有多少个交点。‘’

思路:

如果暴力求解的话,时间复杂度为n^2,大概为10^11,肯定会超时。

由于题目将x的坐标限定在了一个区间(l,r)内,考虑如下情况:

设直线1分别与x=l,x=r相交与L1,R1;直线2分别相交于L2,R2。若两直线在此区间内相交,必有

L1<L2&&R1>R2  或者  L1>L2&&R1<R2

(想象若两直线在(l,r)相交,则经过交点后,纵坐标的大小顺序一定会改变)

此即为逆序数对的性质,于是题目转化为了求逆序数对的个数:

还需注意两点:

  • 1.与y轴平行的,只要这样的线在(l,r)范围内,则必定跟别的不平行线相交。所以计算下个数再乘以不与y轴平行的线的个数 
  • 2.就能使在l和r上的交点不计算在内,需要在排序时注意下(当l相同时,按r排序;当r相同时,按l排序)

    另外很重要的一点,数据为实型数,需要对数据进行离散化处理,如此才能应用树状数组

  • 关于离散化:http://blog.csdn.net/gokou_ruri/article/details/7723378

    代码:

    #include<bits/stdc++.h>
    using namespace std; #define lowbit(x) (x&(-x))
    const int N=5e4+10;
    struct node{
    double a,b;
    int num;
    };
    node e[N];
    int c[N];
    double l,r; int cmp1(node x,node y) //left 递增排序
    {
    if(x.a==y.a)
    return x.b<y.b;
    return x.a<y.a;
    } int cmp2(node x,node y) //right 递减排序
    {
    if(x.b==y.b)
    return x.a>y.a;
    return x.b>y.b;
    } void add(int x,int val)
    {
    while(x<N)
    {
    c[x]+=val;
    x+=lowbit(x);
    }
    } int sum(int x)
    {
    int ans=0;
    while(x>0)
    {
    ans+=c[x];
    x-=lowbit(x);
    }
    return ans;
    } int main()
    {
    int n;
    int t,tt,i,j,ans;
    double x1,y1,x2,y2,k,b; while(scanf("%d",&n)!=EOF)
    {
    t=tt=0;
    ans=0;
    scanf("%lf%lf",&l,&r);
    for(i=0;i<n;i++)
    {
    scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);//两个点的横纵坐标
    if(x1==x2)//横坐标相等 表示竖直的线 平行与y轴
    {
    if(l<x1&&x1<r)
    tt++;
    continue;
    }
    k=(y2-y1)/(x2-x1);//y=kx+b
    b=y1-k*x1;
    e[t].a=l*k+b;
    e[t++].b=r*k+b;
    }
    //******************//
    //离散化,由于想树状数组中add的是left,所以只对left离散化即可
    sort(e,e+t,cmp1);
    for(i=0;i<t;i++)//编号
    e[i].num=i+1;//树状数组无下标0
    //*******************//
    sort(e,e+t,cmp2);//递减排序 3412
    memset(c,0,sizeof(c));
    for(i=0;i<t;i++)//统计
    {
    add(e[i].num,1);
    ans+=sum(e[i].num-1);
    }
    printf("%d\n",ans+tt*t);//加上平行y轴的直线所产生的交点
    }
    }
  • HDU3465--Life is a Line(树状数组求逆序数,离散化)的更多相关文章

    1. poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

      题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

    2. poj 2299 树状数组求逆序数+离散化

      http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...

    3. hdu 5147 Sequence II (树状数组 求逆序数)

      题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

    4. poj 2299 Ultra-QuickSort(树状数组求逆序数)

      链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...

    5. SGU180 Inversions(树状数组求逆序数)

      题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...

    6. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

      题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

    7. Codeforces645B【树状数组求逆序数】

      题意: 给你1-n的序列,然后有k次机会的操作,每一次你可以选择两个数交换. 求一个最大的逆序数. 思路: 感觉就是最后一个和第一个交换,然后往中间逼近,到最终的序列,用树状数组求一下逆序数. #in ...

    8. hdu5792 World is Exploding(多校第五场)树状数组求逆序对 离散化

      题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5792 题目描述:给你n个值,每个值用A[i]表示,然后问你能否找到多少组(a,b,c,d)四个编号,四 ...

    9. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

      Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

    随机推荐

    1. 洛谷 P1801 黑匣子 题解

      题面 离线处理: 大体思路就是将数组排序,然后对于第k次询问把不可行的数打上标记,然后从头开始寻找第k个没打标记的点的值(排序后的数组保证了它是第k小的). 实现方法:首先离散化原始数组,得到数组fi ...

    2. CF 1140B Good String

      Description You have a string ss of length nn consisting of only characters > and <. You may d ...

    3. gcc数据对齐之: howto 2.

      原文链接:http://www.catb.org/esr/structure-packing/ 谁应阅读本文 本文探讨如何通过手工重新打包C结构体声明,来减小内存空间占用.你需要掌握基本的C语言知识, ...

    4. Android 调用摄像头功能【拍照与视频】

      版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/ma_hoking/article/details/28292973 应用场景: 在Android开发 ...

    5. 十一、LaTex中的矩阵

    6. 2019-11-29-Roslyn-通过-Nuget-管理公司配置

      title author date CreateTime categories Roslyn 通过 Nuget 管理公司配置 lindexi 2019-11-29 08:58:52 +0800 201 ...

    7. GitHub源码攻击事件

      黑客擦除了微软多达392个代码存储库,并提出勒索要求.此前,黑客攻击了包含微软在内的大批受害者的Git存储库,删除了所有源代码和最近提交的内容,并留下了支持比特币支付的赎金票据. 勒索信息如下: “要 ...

    8. Eclipse集成开发环境搭建

      gdbserver安装: 安装gdb-server的环境变量要放在arm-linux-gcc的环境的前面,因为arm-linux-gcc的安装包里面也有gdb,linux系统在找指令时从/root/. ...

    9. tomcat+redis接上篇

      数据库允许远程连接 把数据库中的mysql数据库中的user表里的host项为localhost的改为 % update user set host = '%' where user = 'root' ...

    10. python_实现选课系统

      校园管理系统 角色: 学校.学员.课程.讲师 要求: 1. 创建北京.上海 2 所学校 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开 3 ...