Life is a Line

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1927    Accepted Submission(s): 471

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
题意:
给出n条线段两个端点的坐标以及x轴的一个范围l,r,问在这个范围内直线有几个交点。
代码:
 /*
两直线在l,r之内有交点当且仅当(a1-a1)*(b1-b2)<0,a1,b1是其中一条直线与l,r的两个交点,这样按照a排一次序
只要b有交错就会有交点,然后就转化成求逆序数,先按照a从小到大排,给他们编一个号,在按照b从小到大排序
这样只要求编号的逆序数即可。特殊情况是直线平行y轴,此时他与其他的不平行于y轴的直线都相交。
求逆序数是当前的数的位置减去前面比他小的数的个数也就是正序数个数。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int A[];
struct Lu
{
double a,b;
int num;
}L[];
bool cmp1(Lu x,Lu y)
{
if(x.a==y.a)
return x.b<y.b;
return x.a<y.a;
}
bool cmp2(Lu x,Lu y)
{
if(x.b==y.b)
return x.a<y.a;
return x.b<y.b;
}
int lowbit(int id)
{
return id&(-id);
}
void add(int id,int v)
{
while(id<=){
A[id]+=v;
id+=lowbit(id);
}
}
int qury(int id)
{
int s=;
while(id>){
s+=A[id];
id-=lowbit(id);
}
return s;
}
int main()
{
int n;
double l,r,x1,x2,y1,y2;
while(scanf("%d",&n)!=EOF){
scanf("%lf%lf",&l,&r);
int ans=,tem=,cnt=;
for(int i=;i<n;i++){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
if(x1==x2){ //在l,r以外就去掉,在l,r以内要计算,就是这里wa到吐血。
if(x1>l&&x1<r) tem++;
continue;
}
L[cnt].a=y2+(y1-y2)*(l-x2)/(x1-x2);
L[cnt++].b=y2+(y1-y2)*(r-x2)/(x1-x2);
}
sort(L,L+cnt,cmp1);
for(int i=;i<cnt;i++){
L[i].num=i+;
}
sort(L,L+cnt,cmp2);
memset(A,,sizeof(A));
for(int i=;i<cnt;i++){
add(L[i].num,);
ans+=(i-qury(L[i].num-)); //求逆序数
}
printf("%d\n",ans+tem*cnt);
}
return ;
}

HDU3465 树状数组逆序数的更多相关文章

  1. HDU 4911 (树状数组+逆序数)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...

  2. hdu2838Cow Sorting(树状数组+逆序数)

    题目链接:点击打开链接 题意描写叙述:给定一个长度为100000的数组,每一个元素范围在1~100000,且互不同样,交换当中的随意两个数须要花费的代价为两个数之和. 问怎样交换使数组有序.花费的代价 ...

  3. HDU5196--DZY Loves Inversions 树状数组 逆序数

    题意查询给定[L, R]区间内 逆序对数 ==k的子区间的个数. 我们只需要求出 子区间小于等于k的个数和小于等于k-1的个数,然后相减就得出答案了. 对于i(1≤i≤n),我们计算ri表示[i,ri ...

  4. [树状数组+逆序对][NOIP2013]火柴排队

    火柴排队 题目描述 涵涵有两盒火柴,每盒装有n根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑ (ai-bi)2,i=1,2,3,. ...

  5. hdu 5497 Inversion 树状数组 逆序对,单点修改

    Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...

  6. Codevs 3286 火柴排队 2013年NOIP全国联赛提高组 树状数组,逆序对

    题目:http://codevs.cn/problem/3286/ 3286 火柴排队  2013年NOIP全国联赛提高组  时间限制: 1 s   空间限制: 128000 KB   题目等级 : ...

  7. Bzoj 2789: [Poi2012]Letters 树状数组,逆序对

    2789: [Poi2012]Letters Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 278  Solved: 185[Submit][Stat ...

  8. Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2886  Solved: 924[Submit][Stat ...

  9. Bzoj 3289: Mato的文件管理 莫队,树状数组,逆序对,离散化,分块

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1539  Solved: 665[Submit][Status][Di ...

随机推荐

  1. code first提示已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭解决方法

    使用codefirst查询当然是必不可少的,但有时不小心可能很简单的查询也会导致异常. 下面用codefirst做个示例简单演示下异常发生的场景: var attendlist = db.Databa ...

  2. Shell 脚本实现随机抽取班级学生

    #/bin/bash function rand(){ min=$ max=$(($-$min+)) num=$(date +%s%N) echo $(($num%$max+$min)) } rnd= ...

  3. linux系统的初化始配置

    一.网络的初始化 1.ip地址的修改(临时生效) 使用ifconfig命令 ifconfig 网卡名 ip地址 子网掩码 [root@localhost /]# ifconfig eno1677773 ...

  4. php单点登录之模拟淘宝天猫同步登录

    说到单点登录大家都很了解,一个站点登录其他域会自动登录. 单点登录SSO(Single Sign On)的方法有很多,比如:p3p.共享session.共享cookice.第三方OAuth认证. 这里 ...

  5. 在Linux下配置多线路ADSL的方法

    经过一段时间的观察,证明运行良好,现把设置过程及方法总结一下,欢迎指正.此文档可以说明双ADSL及多ADSL增加线路的配置过程. 实验环境: 操作系统: RedHat7.3 两条ADSL,长期观察线路 ...

  6. 从ord()中对Unicode编码的理解

    刚开始学习编程的时候,老对字符串编码的理解模模糊糊.也一直看这方便的资料,今天在看Dive in python时,突然有了新的理解(不知道是否正确). Python有个built-in函数ord(), ...

  7. Launching web on MyEclipse Tomcat 问题

    错误提示: Launching web on MyEclipse Tomcat has encountered a problemAn internal error occurred during: ...

  8. Programming Language A 学习笔记(二)

    1. 语法糖--元组的"名称引用"与"位置引用": (e1,...,en) <=> {1=e1,...,n=en} 类型:t1 * - * tn & ...

  9. iOS pod install update 慢!!!

    在终端输入: pod install --verbose --no-repo-update pod update --verbose --no-repo-update

  10. su root认证失败的解决方法

    sudo passwd 输入安装密码. 输入新密码. 输入 su 即获得root权限.