Counting Intersections HDU - 5862(离散化+树状数组)

本文介绍了一种高效计算平面内平行于坐标轴线段交点数量的方法。通过将问题转化为利用树状数组进行离散化处理,文章详细阐述了算法实现过程,包括输入输出格式、核心数据结构定义及关键步骤。

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

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

这个问题在于如何高效的去枚举了,我们去枚举所有的竖线,并把所有的横线拆成两个点,如果是左端点,标记为0,如果是右端点,标记为1。说不清楚,看代码

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
#define N 100005
using namespace std;
int n;
int c[N*2];
int total;
int lowBit(int x)
{
    return x&-x;
}
int sum(int x)
{
    int ans=0;
    while(x>0)
    {
        ans+=c[x];
        x-=lowBit(x);
    }
    return ans;
}
void change(int x,int p)
{
    while(x<=total)
    {
        c[x]+=p;
        x+=lowBit(x);
    }
}//树状数组
struct node
{
    int x,y1,y2;
}seg[N];//竖线结构体
int cmp1(node s1,node s2)
{
    return s1.x<s2.x;
}
int k1;
struct P
{
    int x,y;
    int type;
}point[N*2];//点结构体
int cmp2(P l1,P l2)
{
    if(l1.x==l2.x)
        return l1.type<l2.type;
    return l1.x<l2.x;
}
int k2;
int num[N*2];
int k3;
int getInd(int y)//由实际值得到离散值
{
    return lower_bound(num,num+total,y)-num+1;
}
int main()
{
    int t;
    int x1,x2,y1,y2;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(c,0,sizeof(c));
        k1=k2=k3=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            if(x1==x2)//竖线
            {
                num[k3++]=y1;
                num[k3++]=y2;//只离散y值
                if(y1>y2)
                {
                    int temp=y1;
                    y1=y2;
                    y2=temp;
                }
                seg[k1].x=x1;
                seg[k1].y1=y1;
                seg[k1].y2=y2;
                k1++;
            }
            else
            {
                num[k3++]=y1;
                if(x1>x2)
                {
                    int temp=x1;
                    x1=x2;
                    x2=temp;
                }
                point[k2].x=x1;
                point[k2].y=y1;
                point[k2].type=0;//把横线给拆掉
                k2++;
                point[k2].x=x2;
                point[k2].y=y1;
                point[k2].type=1;
                k2++;
            }
        }
        sort(seg,seg+k1,cmp1);
        sort(point,point+k2,cmp2);
        sort(num,num+k3);//各自的排序
        total=unique(num,num+k3)-num;
        long long ans=0;
        for(int i=0,j=0;i<k1;i++)//枚举竖线
        {
            while(j<k2&&(point[j].x<seg[i].x||(point[j].x==seg[i].x&&point[j].type==0)))//把所有横坐标小于竖线的横坐标的点的y值更新到树状数组
            {
                if(point[j].type==0)
                    change(getInd(point[j].y),1);
                else
                    change(getInd(point[j].y),-1);
                j++;
            }
            ans+=sum(getInd(seg[i].y2))-sum(getInd(seg[i].y1)-1);//统计答案
        }
        printf("%lld\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值