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. spring boot-13.数据访问

      1.spring boot 的自动配置提供的方便快捷的数据库操作服务,只需要进行少量配置即可连接数据库.spring boot 在org.springframework.boot.autoconfig ...

    2. spring+redis实例(二)

      这一篇redis实例是基于序列化储存-(写入对象,读取对象) 在spring+redis(一)中我们介绍了在spring中怎么去操作储存redis,基于string的储存,今天我们介绍一下redis基 ...

    3. springBoot2.0 Yaml值获取

      1. pom.xml添加如下依赖 <dependency> <groupId>org.springframework.boot</groupId> <arti ...

    4. 自己动手实现一个html2canvas

      前言 昨天写了新手引导动画的4种实现方式,里面用到了 html2canvas 于是就顺便了解了一下实现思路. 大概就是 利用 svg 的 foreignObject 标签, 嵌入 dom, 最后再利用 ...

    5. RabbitMQ入门教程(一):安装和常用命令

      原文:RabbitMQ入门教程(一):安装和常用命令 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn ...

    6. 10.css3动画--过渡动画--trasition

      Transition简写属性. Transition-property规定应用过渡的css属性的名称. . Transition-timing-function过渡效果的时间曲线,默认是ease. L ...

    7. $id(id)函数

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

    8. iphone手机软件安装目录

      iPhone系统常用文件夹位置 1.[/Applications] 常用软件的安装目录 2. [/private /var/ mobile/Media /iphone video Recorder] ...

    9. intellij 编译 springmvc+hibernate+spring+maven 找不到hbm.xml映射文件

      1. 错误信息 Invocation of init method failed; nested exception is org.hibernate.MappingNotFoundException ...

    10. Python3零基础入门学习视频+源码+课件+习题-小甲鱼

      目录 1. 介绍 2. 目录 3. 下载地址 1. 介绍 适用人群 完全零基础入门,不需要任何前置知识. 课程概述 本系列教程面向零基础的同学,是一个深入浅出,通俗易懂的Python3视频教程. 前半 ...