HDU5862-Counting Intersections

来源:互联网 发布:iphone隐形监控软件 编辑:程序博客网 时间:2024/06/06 02:21

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
241 0 1 32 0 2 30 1 3 10 2 3 240 0 2 03 0 3 23 3 1 30 3 0 2
 

Sample Output
40
 

Author
BUPT
 

Source
2016 Multi-University Training Contest 10
 

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 longconst 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;}

原创粉丝点击