2016暑假多校联合---Counting Intersections

原题链接

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

题意:输入n条线段(x1,y1)-(x2,y2)  这n条线段平行于坐标轴,不存在重叠部分且不存在两条线段共断点,求交叉点数;

思路:输入数据n<=100000,而坐标x,y<1e9,所以首先可以将线段进行离散化,使得所有的坐标点在1~1e5之间,把平行于y轴的线段按x坐标排序,把平行于x轴的线段化作起点与结束点,即用一个结构体struct Node{int x;int y;}node[200005];  保存平行于x轴的线段的信息,例如一条平行于x轴的线段为(x1,y1)-(x2,y2)且y1==y2  则

node[i].x=x1,node[i].y=y1;  node[i+1].x=x2+1,node[i+1].y=0-y1;   这样可以把一段区间上的操作化作点的操作,减小复杂度。

区间上的操作化作点的操作,什么意思呢?  其实就是在这段区间开始的时候加上这个数,那么在区间中间不必再考虑这条线(这段区间),因为开始已经加上了,最后在区间结束的下一位减去就行。

最后,在1到2*1e5的循环,用一个一维的树状数组,当循环到i 时,先把关于node[].x==i 的 操作处理完,然后计算在x=i 直线处相交的点数,即为求出在平行于y轴且x==i的区间上的点数,用树状数组可以方便快速实现;

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
typedef long long LL;
LL ax[300005],ay[300005];
LL c[200005];
LL Lowbit(LL t)
{
    return t&(t^(t-1));
}
LL Sum(LL x)
{
    LL sum = 0;
    while(x > 0)
    {
        sum += c[x];
        x -= Lowbit(x);
    }
    return sum;
}
void add(LL li,LL d)
{
    while(li<200005)
    {
        c[li]+=d;
        li=li+Lowbit(li);
    }
}

struct Nodex
{
    LL x1,x2;
    LL y;
}nodex[100005];

struct Nodey
{
    LL y1,y2;
    LL x;
}nodey[100005];

struct Node
{
    LL x;
    LL y;
}node[200005];

bool cmp1(const Nodey s1,const Nodey s2)
{
    return s1.x<s2.x;
}

bool cmp2(const Node s1,const Node s2)
{
    return s1.x<s2.x;
}
map<LL,LL>q1;
map<LL,LL>q2;

int main()
{
    LL T,n;
    scanf("%I64d",&T);
    while(T--)
    {
        q1.clear();
        q2.clear();
        scanf("%I64d",&n);
        LL tot1=0,tot2=0;
        for(LL i=0;i<n;i++)
        {
            LL x1,x2,y1,y2;
            scanf("%I64d%I64d%I64d%I64d",&x1,&y1,&x2,&y2);
            if(x1==x2)
            {
                nodey[tot2].y1=min(y1,y2);
                nodey[tot2].y2=max(y1,y2);
                nodey[tot2++].x=x1;
            }
            else
            {
                nodex[tot1].x1=min(x1,x2);
                nodex[tot1].x2=max(x1,x2);
                nodex[tot1++].y=y1;
            }
        }

        LL num1,num2;
        for(LL i=0;i<tot1;i++)
        {
            ax[i*2]=nodex[i].x1;
            ax[i*2+1]=nodex[i].x2;
        }
        num1=2*tot1;
        for(LL i=0;i<tot2;i++)
        {
            ax[num1++]=nodey[i].x;
        }
        sort(ax,ax+num1);
        LL tot=0,pre=-1;
        for(LL i=0;i<num1;i++)
        {
            if(ax[i]!=pre)
            {
                pre=ax[i];
                q1[pre]=++tot;
            }
        }

        for(LL i=0;i<tot2;i++)
        {
            ay[i*2]=nodey[i].y1;
            ay[i*2+1]=nodey[i].y2;
        }
        num2=2*tot2;
        for(LL i=0;i<tot1;i++)
            ay[num2++]=nodex[i].y;
        sort(ay,ay+num2);
        tot=0,pre=-1;
        for(LL i=0;i<num2;i++)
        {
            if(ay[i]!=pre)
            {
                pre=ay[i];
                q2[pre]=++tot;
            }
        }

        for(LL i=0;i<tot1;i++)
        {
            nodex[i].x1=q1[nodex[i].x1];
            nodex[i].x2=q1[nodex[i].x2];
            nodex[i].y=q2[nodex[i].y];
        }
        for(LL i=0;i<tot2;i++)
        {
            nodey[i].y1=q2[nodey[i].y1];
            nodey[i].y2=q2[nodey[i].y2];
            nodey[i].x=q1[nodey[i].x];
        }

        //for(LL i=0;i<tot1;i++)
            //cout<<"x: "<<nodex[i].x1<<" "<<nodex[i].x2<<" "<<nodex[i].y<<endl;
        //for(LL i=0;i<tot1;i++)
            //cout<<"y: "<<nodey[i].y1<<" "<<nodey[i].y2<<" "<<nodey[i].x<<endl;
        sort(nodey,nodey+tot2,cmp1);
        tot=0;
        for(LL i=0;i<tot1;i++)
        {
            node[tot].x=nodex[i].x1;
            node[tot++].y=nodex[i].y;
            node[tot].x=nodex[i].x2+1;
            node[tot++].y=0-nodex[i].y;
        }
        sort(node,node+tot,cmp2);
        LL j=0,k=0,sum=0;
        memset(c,0,sizeof(c));
        for(LL i=1;i<=200001;i++)
        {
            while(node[j].x==i&&j<tot)
            {
                if(node[j].y>0)
                add(node[j].y,1);
                else
                add(0-node[j].y,-1);
                j++;
            }
            while(nodey[k].x==i&&k<tot2)
            {
                sum+=Sum(nodey[k].y2)-Sum(nodey[k].y1-1);
                k++;
            }
            if(j>=tot)  break;
            if(k>=tot2) break;
        }
        printf("%I64d\n",sum);
    }
    return 0;
}
时间: 2024-12-27 09:43:04

2016暑假多校联合---Counting Intersections的相关文章

2016暑假多校联合---Windows 10

2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on the top of a mountain. Recently, our old monk found the operating system of his computer was updating to windows 10 automatically and he even can't j

2016暑假多校联合---Substring(后缀数组)

2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a string, he has to calculate the total number of its distinct substrings. But ?? thinks that is too easy, he wants to make this problem more interesti

2016暑假多校联合---Rikka with Sequence (线段树)

2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has an array A with n numbers. Then he make

2016暑假多校联合---Another Meaning

2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two meanings. Such as “hehe”, which not only means “hehe”, but also means “excuse me”. Today, ?? is chating with MeiZi online, MeiZi sends a sentence A to

2016暑假多校联合---Death Sequence(递推、前向星)

原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian living in 1st century. He and his 40 comrade soldiers were trapped in a cave, the exit of which was blocked by Romans. They chose suicide over captu

2016暑假多校联合---Joint Stacks (STL)

HDU  5818 Problem Description A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of the stack. The last entry which is inserted is the first one that will be removed. In another wor

2016暑假多校训练参赛感想

参赛感想 这是第一次参加暑假多校训练,应该也会是人生中最后一次,我真的很庆幸能参加这个训练,和全国几乎所有高校的ACMer一起在一个平台上做题!昨天为止多校已经完全结束,今天看到叉姐的训练感想(叉姐的感想链接),我觉得我也有必要写下自己的训练感想. 人的眼界总是狭窄的,当在自己的学校站在前几名的时候觉得自己还不错,应该会有不错的将来,但是当第一次参加国赛(2015 南阳站)的时候我便被别人实力所震撼,我突然觉得自己在别人的眼里简直就是小学生,菜到不行.别人在5个小时可以AK,而我连最水的题也要想

2015暑假多校联合---CRB and His Birthday(01背包)

题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son.She went to the nearest shop with M Won(currency unit).At the shop, there are N kinds of pr

2015暑假多校联合---Expression(区间DP)

题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5396 Problem Description Teacher Mai has n numbers a1,a2,?,anand n−1 operators("+", "-" or "*")op1,op2,?,opn−1, which are arranged in the form a1 op1 a2 op2 a3 ? an. He wan