HDU5862-Counting Intersections

本文介绍了一种解决计算平面内平行于坐标轴线段交点数量问题的方法。利用离散化和扫描线技术,通过排序和遍历,有效地统计了任意两条线段相交的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Counting Intersections

                                                                  Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                            Total Submission(s): 1290    Accepted Submission(s): 402


Problem Description
Given some segments which are paralleled to the coordinate axis. You need to count the number of their intersection.

The input data guarantee that no two segments share the same endpoint, no covered segments, and no segments with length 0.
 

Input
The first line contains an integer T, indicates the number of test case.

The first line of each test case contains a number n(1<=n<=100000), the number of segments. Next n lines, each with for integers, x1, y1, x2, y2, means the two endpoints of a segment. The absolute value of the coordinate is no larger than 1e9.
 

Output
For each test case, output one line, the number of intersection.
 

Sample Input
  
2 4 1 0 1 3 2 0 2 3 0 1 3 1 0 2 3 2 4 0 0 2 0 3 0 3 2 3 3 1 3 0 3 0 2
 

Sample Output
  
4 0
 

Author
BUPT
 

Source
 

Recommend
wange2014
 


题意:给你n条平行于x轴或平行于y轴的线段,求有多少个交点

解题思路:离散化+扫描线,将所有竖线只保存点坐标,每条竖线下面的点val为1,上面的点val为-1,对所有点和横线按照y轴进行排序,然后从下往上扫即可


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

struct node
{
    int x1,y,x2;
    friend bool operator<(node a,node b)
    {
        return a.y<b.y;
    }
}a[100009];

struct point
{
    int x,y,val;
    friend bool operator<(point a,point b)
    {
        if(a.y!=b.y) return a.y<b.y;
        else return a.val>b.val;
    }
}p[200009];

int n,cnt,res1,res2;
int x[200009],sum[200009];

int lowbit(int k) {return k&-k;}
void update(int k,int val) {for(;k<cnt;k+=lowbit(k)) sum[k]+=val;}
int getsum(int k) {int ans=0;for(;k;k-=lowbit(k)) ans+=sum[k];return ans;}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        cnt=res1=res2=1;
        for(int i=1;i<=n;i++)
        {
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            x[cnt++]=x1,x[cnt++]=x2;
            if(x1==x2)
            {
                if(y1>y2) swap(y1,y2);
                p[res1].x=x1,p[res1].y=y1,p[res1++].val=1;
                p[res1].x=x1,p[res1].y=y2,p[res1++].val=-1;
            }
            else
            {
                if(x1>x2) swap(x1,x2);
                a[res2].y=y1,a[res2].x1=x1,a[res2++].x2=x2;
            }
        }
        sort(x+1,x+cnt);
        cnt=unique(x+1,x+cnt)-x;
        sort(p+1,p+res1);
        sort(a+1,a+res2);
        memset(sum,0,sizeof sum);
        LL ans=0;
        int j=1;
        for(int i=1;i<res2;i++)
        {
            while(j<res1&&(p[j].y<a[i].y||(p[j].y==a[i].y&&p[j].val==1)))
            {
                int k=lower_bound(x+1,x+cnt,p[j].x)-x;
                update(k,p[j].val);
                j++;
            }
            int k=lower_bound(x+1,x+cnt,a[i].x2)-x;
            int kk=lower_bound(x+1,x+cnt,a[i].x1)-x-1;
            ans=ans+1LL*getsum(k)-1LL*getsum(kk);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值