HDU3465--Life is a Line(树状数组求逆序数,离散化)
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)相交,则经过交点后,纵坐标的大小顺序一定会改变)
此即为逆序数对的性质,于是题目转化为了求逆序数对的个数:
还需注意两点:
另外很重要的一点,数据为实型数,需要对数据进行离散化处理,如此才能应用树状数组
关于离散化: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(树状数组求逆序数,离散化)的更多相关文章
- poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)
题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...
- poj 2299 树状数组求逆序数+离散化
http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...
- hdu 5147 Sequence II (树状数组 求逆序数)
题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- poj 2299 Ultra-QuickSort(树状数组求逆序数)
链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...
- SGU180 Inversions(树状数组求逆序数)
题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- Codeforces645B【树状数组求逆序数】
题意: 给你1-n的序列,然后有k次机会的操作,每一次你可以选择两个数交换. 求一个最大的逆序数. 思路: 感觉就是最后一个和第一个交换,然后往中间逼近,到最终的序列,用树状数组求一下逆序数. #in ...
- hdu5792 World is Exploding(多校第五场)树状数组求逆序对 离散化
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5792 题目描述:给你n个值,每个值用A[i]表示,然后问你能否找到多少组(a,b,c,d)四个编号,四 ...
- HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
随机推荐
- 基于XML配置Spring的自动装配
一.了解Spring自动装配的方式 采用传统的XML方式配置Bean组件的关键代码如下所示 <bean id="userMapper" class="edu.cn. ...
- html之input
<input>在w3c中解释的是用于搜集用户的信息 它其实就是一个输入框,根据type的不同这个框可以用作不同的功能可以输入一个文本,可以定义一个按钮什么的. 属性type一些值的介绍 1 ...
- window7下安装Elasticseach5.2.2
1. 安装JDK,至少1.8.0_73以上版本 java -version 2. 下载和解压缩Elasticsearch安装包,目录结构 3. 启动Elasticsearch:bin\elastics ...
- ALS部署Spark集群入坑记
[Stage 236:> (0 + 0) / 400]17/12/04 09:45:55 ERROR yarn.ApplicationMaster: User class threw excep ...
- git使用以及对应sourceTree
git上面的几条指令 (1)要想把A合并到B分支上,就需要先切换到B分支上,然后在合并A分支,执行指令: git checkout B // 这是切换到B分支上 git merge A // 这是将A ...
- 用python 获取照片的Exif 信息(获取拍摄设备,时间,地点等信息)
第一步:先安装 pip install exifread 第二部:上代码 import exifread import requests class PhotoExifInfo(): def __in ...
- ES中的查询操作
1.前缀查询 先输入数据: PUT /my_index/address/ { "postcode": "W1 3DG" } PUT /my_index/addr ...
- 响应式前端框架Bootstrap系列(11)分页
分页功能已经封装成一个独立的js文件,也是用bs完成的,名称为bootstrap-paginator.js. 使用前先导入文件 : <script src="../libs/boots ...
- TanksWar(坦克大战三维、二维版以及90版)
本文已迁移至:https://coco56.blog.csdn.net/article/details/103198945
- 裸机开发体验之led快速体验
[root@promote led]# arm-linux-gcc -g -c led.S[root@promote led]# lsled.lds led.o led.S Makefile[root ...