hdu 3465 Life is a Line(树状数组求逆序对)

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
题目分析:
两条斜率存在的直线在(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 );
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值