codeforces 610D D. Vika and Segments(离散化+线段树+扫描线算法)

本文介绍了一种算法,通过将线段转化为宽度为1的矩形,并运用扫描线算法来计算二维坐标系中由这些矩形覆盖的点数。该算法能够处理大量平行于坐标轴的线段,确保即使某些点被多次覆盖也只计算一次。

题目链接:

D. Vika and Segments

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

 

Examples
input
3
0 1 2 1
1 4 1 2
0 3 2 3
output
8
input
4
-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的矩形,然后用扫描线算法求面积;

 

AC代码:

 

 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+4;
int n,x1,x2,y3,y2,rec[2*N],num;
struct no
{
    int l,r,h,flag;
};
no line[8*N];
struct nod
{
    int l,r,cover;
    ll sum;
};
nod tree[8*N];
int cmp(no x,no y)
{
    return x.h<y.h;
}
void build(int node,int L,int R)
{
    tree[node].l=L,tree[node].r=R;
    tree[node].cover=tree[node].sum=0;
    if(L>=R)return ;
    int mid=(L+R)>>1;
    build(2*node,L,mid);
    build(2*node+1,mid+1,R);
}
void Pushup(int node)
{
    if(tree[node].cover)
    {
        tree[node].sum=rec[tree[node].r+1]-rec[tree[node].l];
    }
    else
    {
        if(tree[node].l==tree[node].r)tree[node].sum=0;
        else tree[node].sum=tree[2*node].sum+tree[2*node+1].sum;
    }
}
void update(int node,int L,int R,int x)
{
    if(L<=tree[node].l&&R>=tree[node].r)
    {
        tree[node].cover+=x;
        Pushup(node);
        return ;
    }
    int mid=(tree[node].l+tree[node].r)>>1;
    if(L>mid) update(2*node+1,L,R,x);
    else if(R<=mid)update(2*node,L,R,x);
    else
    {
        update(2*node,L,mid,x);
        update(2*node+1,mid+1,R,x);
    }
    Pushup(node);
}
int bi(int x)
{
    int L=1,R=num-1,mid;
    while(L<=R)
    {
        mid=(L+R)>>1;
        if(rec[mid]==x)return mid;
        else if(rec[mid]>x)R=mid-1;
        else L=mid+1;
    }
    return -1;
}
int main()
{
    scanf("%d",&n);
    int cnt=1;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d%d",&x1,&y3,&x2,&y2);
            if(x1>x2)swap(x1,x2);
            if(y3>y2)swap(y3,y2);
            rec[cnt] = line[cnt].l = x1;
            line[cnt].r = x2+1;
            line[cnt].h = y3;
            line[cnt++].flag = 1;
            line[cnt].l = x1;
            rec[cnt] = line[cnt].r = x2+1;
            line[cnt].h = y2+1;
            line[cnt++].flag = -1;
    }
    sort(line+1,line+cnt,cmp);
    sort(rec+1,rec+cnt);
    num = 2;
    for(int i = 2;i < cnt;i++)
    {
        if(rec[i]!=rec[i-1])rec[num++]=rec[i];
    }
    build(1,1,num-1);
    ll ans=0;
    for(int i = 1;i < cnt-1;i++)
    {
        int fx = bi(line[i].l);
        int fy = bi(line[i].r)-1;
        if(fx <= fy)
        {
            update(1,fx,fy,line[i].flag);
        }
        ans+=tree[1].sum*(ll)(line[i+1].h-line[i].h);
    }
    cout<<ans<<"\n";
    return 0;
}

 

  

 

 

 

转载于:https://www.cnblogs.com/zhangchengc919/p/5363831.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值