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 ...
随机推荐
- [转帖]56核Xeon Platinum 9200现身 - 英特尔有史以来最大的CPU封装
56核Xeon Platinum 9200现身 - 英特尔有史以来最大的CPU封装 https://www.cnbeta.com/articles/tech/835271.htm 当英特尔宣布上周正式 ...
- html中的锚点设置
html中的锚点 一.页面内跳转的锚点设置 页面内的跳转需要两步: 方法一: ①:设置一个锚点链接去找喵星人:(注意:href属性的属性值最前面要加#) ②:在页面中需要的位置设置锚点<a na ...
- Python进阶编程 反射
1.7反射 python面向对象中的反射:通过字符串的形式操作对象相关的属性.python中的一切事物都是对象(都可以使用反射) class Foo: f = '类的静态变量' def __init_ ...
- python3的一些文件操作的脚手架
用python把原来的脚本重构了一下,其中写了文件操作的一些函数,如下: import os import shutil import hashlib import stat #查找文件夹中的某个文件 ...
- java向word中插入Excel附件
1.word中插入对象的原理 编辑word,向word中插入图片.EXCEL.WORD等附件,再将word保存为xml格式,通过XML查看工具打开xml格式的word的源码,通过对比源码, 可以发现平 ...
- kafka复习(1)
一:flume复习 0.JMS(java message service )java消息服务 ----------------------------------------------------- ...
- 苹果浏览器和ios中,时间字符串转换问题
背景:在开发PC端项目和小程序时,遇到过一个时间字符串转化问题,在苹果浏览器和ios微信客户端里,"2018-10-15 18:20" 以 字符"-"拼接的时间 ...
- 记录-- vue+element树节点的标注
html(背景:状态标注3钟颜色红黄绿对应0,1,2,) <el-tree @check="slclasscheck" v-if="treeShow" : ...
- Win7安装Visual Studio 2019闪退问题
最近在Win7 系统上安装最新版的VS2019发现 每次在这个画面之后就闪退了,即便换了台电脑也是一样的情况,于是我意识到,这应该是系统本身的问题 经过调查发现是只需要安装两个更新就可以了 这两个更新 ...
- ipcs - 提供基于 ipc (Inter-process communication)结构的信息
总览 ipcrm [ shm | msg | sem ] id 描述 ipcrm 将删除指定的资源--指定 id.