hdu 3465 Life is a Line(树状数组求逆序对)
2015-04-23 08:58
465 查看
Life is a Line
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 1712 Accepted Submission(s): 391
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
题目分析:
两条斜率存在的直线在(l,r)相交的条件就是设ly,ry为两条直线交在x=l和=y两条直线上的交点的y坐标,当(ly1-ly2)*(ry1-ry2)<0时,存在交点,而斜率不存在且在范围内的直线和任意存在斜率的直线相交, 根据相交的特性,可以引申到求逆序对,所以先对点进行离散化,注意对坐标相同的点的处理,防止出现在l,r处相交的点。
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #define MAX 50007 using namespace std; int n,cnt,num; double ll,r; struct Line { double ly,ry; int id; }l[MAX]; double x1,y1,x2,y2; bool cmp1 ( Line l1 , Line l2 ) { if ( l1.ly == l2.ly ) return l1.ry < l2.ry; return l1.ly < l2.ly; } bool cmp2 ( Line l1 , Line l2 ) { if ( l1.ry == l2.ry ) return l1.ly > l2.ly; return l1.ry > l2.ry; } int c[MAX]; int lowbit ( int x ) { return x&-x; } void add ( int x ) { while ( x <= n ) { c[x]++; x += lowbit ( x ); } } int sum ( int x ) { int ret = 0; while ( x ) { ret += c[x]; x -= lowbit ( x ); } return ret; } int main ( ) { while ( ~scanf ( "%d" , &n ) ) { num = cnt = 0; memset ( c , 0 , sizeof ( c ) ); scanf ( "%lf%lf" , &ll , &r ); for ( int i = 0 ; i < n ; i++ ) { scanf ( "%lf%lf%lf%lf" , &x1 , &y1 , &x2 , &y2 ); if ( x1 == x2 ) { if ( x1 < r && x1 > ll ) cnt++; } else { double k = (y2-y1)/(x2-x1); double b = y1 - x1*k; l[num].ly = k*ll+b; l[num++].ry = k*r+b; } } sort ( l , l+num , cmp1 ); for ( int i = 0 ; i < num ; i++ ) l[i].id = i+1; sort ( l , l+num , cmp2 ); /*for ( int i = 0 ; i < n ; i++ ) { printf ( "%lf %lf " , l[i].ly , l[i].ry ); printf ( "%d " , l[i].id ); puts ("" ); }*/ long long ans = 0; for ( int i = 0 ; i < num ; i++ ) { ans += sum ( l[i].id ); add ( l[i].id ); } // cout << ans << " " << num << " " << cnt << endl; ans += num * cnt; printf ( "%lld\n" , ans ); } }
相关文章推荐
- HDU 3465 Life is a Line (逆序数 + 数状数组)
- HDOJ 题目3465 Life is a Line(树状数组求逆序对)
- hdu 3465 Life is a Line 树状数组求逆序数
- hdu 3465 Life is a Line 树状数组求逆序数
- HDU 3465 Life is a Line 树状数组求逆序数
- HDU3465--Life is a Line(树状数组求逆序数,离散化)
- HDOJ 题目3465 Life is a Line(树状数组求逆序对,第二种写法)
- HDU 3465 Life is a Line 逆序数
- hdu 3465 Life is a Line
- HDU 5792 World is Exploding (树状数组逆序对)
- HDU - 3465 Life is a Line
- HDU - 3465 Life is a Line
- HDU - 2838 Cow Sorting (树状数组 + 逆序对)
- hdu1394求最小逆序----树状数组解决
- HDU1394用树状数组求逆序数
- HDU3465 Life is a Line 用归并排序求逆序数
- HDU 5792 World is Exploding (树状数组)
- hdu 5497 Inversion 求逆序对 树状数组
- HDU-5792 World is Exploding(树状数组)
- hdu_2838_Cow Sorting(树状数组求逆序对)