Codeforces 610D Vika and Segments 线段树扫描线(离散化的严重问题,临点之间不是零)

本文介绍了一种在二维坐标系中计算多个平行于坐标轴的线段所覆盖的单元格总数的方法。通过离散化、扫描线算法及线段树等技术实现精确计算,即使线段间存在重叠或完全一致的情况也能准确处理。

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

D. Vika and Segments(离散+扫描+线段数)

                time limit per test2 seconds
            memory limit per test256 megabytes
                    inputstandard input
                    outputstandard output
  • 1
  • 2
  • 3
  • 4
  • 5

Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew n black horizontal and vertical segments parallel to the coordinate axes. All segments have width equal to 1 square, that means every segment occupy some set of neighbouring squares situated in one row or one column.

Your task is to calculate the number of painted cells. If a cell was painted more than once, it should be calculated exactly once.

Input 
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of segments drawn by Vika.

Each of the next n lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1, y1, x2, y2 ≤ 109) — the coordinates of the endpoints of the segments drawn by Vika. It is guaranteed that all the segments are parallel to coordinate axes. Segments may touch, overlap and even completely coincide.

Output 
Print the number of cells painted by Vika. If a cell was painted more than once, it should be calculated exactly once in the answer.

Sample test(s) 
input 

0 1 2 1 
1 4 1 2 
0 3 2 3 
output 

input 

-2 -1 2 -1 
2 1 -2 1 
-1 -2 -1 2 
1 2 1 -2 
output 
16 
Note 
In the first sample Vika will paint squares (0, 1), (1, 1), (2, 1), (1, 2), (1, 3), (1, 4), (0, 3) and (2, 3). 
题目大意:给出一定数量的线段,且这些线段的宽度为1(即为矩形),求这些线段包
括的总点数(即这些矩形的总面积)。 
解题思路: 
1、修改每条线段最小的x坐标和y坐标,将其减一,变成求这些矩形的总面积。 
2、每个矩形可以由两条水平线段表示,上水平线和下水平线进行存储(并标记1为下水平线,-1为上水平线)。 
3、将这些水平线用其所处的高度进行从底端到顶端排序。 
4、并用集合set记录这些水平线左右两个端点的坐标,进行排序。 
5、用线段树表示每两个x坐标之间存在点的个数,并用cover数组记录是否两个x坐标之间被覆盖的次数进行更新。 
6、开始对水平线从底端到顶端进行扫描,维持线段树。 
7、当为下水平线时,就将其覆盖的两个x坐标之间的数组覆盖次数加一,并对其点数进行更新。当为上水平线时,就将其覆盖的两个x坐标之间的数组覆盖次数减一,并对其点数进行更新。算出每层高度之间的总面积并进行相加求出总点数。 
总结: 
用线段树存储的好处在于每层高度之间记录总的水平线长度总是最大的,并不受当前水平线影响,之前覆盖过的水平线段仍能发挥其作用直到其对应的上水平线出现才失去作用,就算有水平线重叠也只计算一次。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>

using namespace std;


struct node
{
    int x1,y1;
    int x2,y2;
    int f;

    node(){}
    node(int a,int b,int c,int d,int e)
    {
        x1=a;y1=b;
        x2=c;y2=d;
        f=e;
    }


}seg[3000005];

bool cmp(node x,node y)
{
    return x.x1<y.x1;
}

int a[3000005];
int cnt;

int cntt;

struct node1
{
    int l,r;
    int cov;
    double sum;
}tr[8*1000005];


int fd(int x)
{
    int ans=lower_bound(a,a+cntt,x)-a;

    return (ans+1);
}

void build(int i,int l,int r)
{
    tr[i].l=l;
    tr[i].r=r;
    tr[i].cov=0;
    tr[i].sum=0;

    if((l+1)==r)
        return ;

    int mid=(l+r)/2;
    build(i*2,l,mid);
    build(i*2+1,mid,r);

}

node1 up(node1 cur, node1 x,node1 y)
{
    node1 ntree;
    ntree.cov=cur.cov;
    ntree.l=cur.l;
    ntree.r=cur.r;

    if(cur.cov>0)
    {
        ntree.sum=(double)(a[cur.r-1]-a[cur.l-1]);
    }
    else
    {
        ntree.sum=x.sum+y.sum;
    }

    return ntree;
}

void update(int i,int l,int r,int k)
{
    if(tr[i].l==l && tr[i].r==r)
    {
        tr[i].cov+=k;

        if((l+1)!=r)
        tr[i]=up(tr[i],tr[i*2],tr[i*2+1]);
        else
        {
            if(tr[i].cov>0)
                tr[i].sum=a[tr[i].r-1]-a[tr[i].l-1];
            else
                tr[i].sum=0;
        }

        return ;
    }

    int mid=(tr[i].l+tr[i].r)/2;
    if(r<=mid)
    {
        update(i*2,l,r,k);
    }
    else if(l>=mid)
    {
        update(i*2+1,l,r,k);
    }
    else
    {
        update(i*2,l,mid,k);
        update(i*2+1,mid,r,k);   //attention
    }

    tr[i]=up(tr[i],tr[i*2],tr[i*2+1]);

}


int main()
{
    int n;

    while(~scanf("%d",&n))
    {
        cnt=0;
        for(int i=0;i<n;i++)
        {
            int x1,y1,x2,y2;

            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            a[cnt++]=x1;
            a[cnt++]=y1;
            a[cnt++]=x2;
            a[cnt++]=y2;

            a[cnt++]=x1+1;
            a[cnt++]=y1+1;
            a[cnt++]=x2+1;
            a[cnt++]=y2+1;

            if(x1==x2)
            {
                if(y1>y2)
                    swap(y1,y2);
                seg[i*2]=node(x1,y1,x2,y2+1,1);
                seg[i*2+1]=node(x1+1,y1,x2+1,y2+1,-1);
            }
            else
            {
                if(x1>x2)
                    swap(x1,x2);
                seg[i*2]=node(x1,y1,x1,y1+1,1);
                seg[i*2+1]=node(x2+1,y1,x2+1,y1+1,-1);
            }
        }
        sort(a,a+cnt);
        cntt=unique(a,a+cnt)-a;

        sort(seg,seg+2*n,cmp);
        double ans=0;

   // cout<<cntt<<endl;

    //for(int i=0;i<cntt;i++)
       // cout<<a[i]<<" ";
    //cout<<endl;
   // for(int i=0;i<2*n;i++)
   // {
       // cout<<seg[i].x1<<" :"<<seg[i].y1<<" "<<seg[i].y2<<" "<<seg[i].f<<endl;
  //  }
       build(1,1,cntt);
       int id=0;
       //long long px=seg[0].x1;
       update(1,fd(seg[0].y1),fd(seg[0].y2),seg[0].f);

       for(int i=1;i<2*n;i++)
       {

          {
           double tans=seg[i].x1-seg[i-1].x1;

           tans*=(tr[1].sum);

           ans+=tans;

           update(1,fd(seg[i].y1),fd(seg[i].y2),seg[i].f);
           }
       }

       printf("%.0f\n",ans);

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值