hdu 1828 线段树扫描线(周长)

本文介绍了一种使用线段树和扫描线算法解决矩形拼合后图形周长计算的方法。通过处理矩形的边界,利用线段树进行区间更新与查询,实现了高效的周长计算。

Picture

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3897    Accepted Submission(s): 1978


Problem Description
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter. 

Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1. 



The corresponding boundary is the whole set of line segments drawn in Figure 2. 



The vertices of all rectangles have integer coordinates.
 

Input
Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate. 

0 <= number of rectangles < 5000 
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.

Please process to the end of file.
 

Output
Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.


Sample Input

7
-15 0 5 10
-5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
30 10 36 20
34 0 40 16


Sample Output

228

First

/*
hdu 1828 线段树扫描线(周长)

给你一些矩阵,有部分or全部重叠.求最终图形的周长

对于x轴的边,矩阵的下边标为1,矩阵的上边标为-1.每次插入后用当前覆盖面积
减去上一次的覆盖面积(即新增长度),扫描过一个矩阵的上边后便能消除下边的
影响。
对x,y轴分别扫描一次即可

最开始没考虑重边的问题,结果C++ AC(G++ WR)
然后更改后G++才过 - -

2
0 0 1 1
1 0 2 1

2
0 0 1 1
0 1 1 2

hhh-2016-03-26 16:35:21
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
using namespace std;
#define lson  (i<<1)
#define rson  ((i<<1)|1)
typedef long long ll;
const int maxn = 20050;
int a[maxn];
struct node
{
    int l,r;
    int sum,len;
    int mid()
    {
        return (l+r)>>1;
    }
} tree[maxn*5];

void push_up(int i)
{
    if(tree[i].sum)
        tree[i].len = (tree[i].r-tree[i].l+1);
    else if(tree[i].l == tree[i].r)
        tree[i].len = 0;
    else
        tree[i].len = tree[lson].len+tree[rson].len;
}

void build(int i,int l,int r)
{
    tree[i].l = l;
    tree[i].r = r;
    tree[i].sum = 0;
    tree[i].len = 0;
    if(l == r)
        return ;

    int mid = tree[i].mid();
    build(lson,l,mid);
    build(rson,mid+1,r);
    push_up(i);
}

void push_down(int i)
{
//    if(tree[i].sum)
//    {
//        tree[lson].sum += tree[i].sum;
//        tree[rson].sum += tree[i].sum;
//        tree[lson].len = tree[lson].r-tree[lson].l;
//        tree[rson].len = tree[rson].r-tree[rson].l;
//    }
}

void Insert(int i,int l,int r,int val)
{
    if(tree[i].l >= l && tree[i].r <=r )
    {
        tree[i].sum += val;
        push_up(i);
        return ;
    }
    int mid = tree[i].mid();
    push_down(i);
    if(l <= mid)
        Insert(lson,l,r,val);
    if(r > mid)
        Insert(rson,l,r,val);
    push_up(i);
}

struct edge
{
    int l,r;
    int va,high;
};
edge tx[maxn*2];
edge ty[maxn*2];

bool cmp(edge a,edge b)
{
    if(a.high != b.high)
        return a.high < b.high;
    else   //如果有重边则要先插入在删除
        return a.va > b.va;
}

int main()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        int x1,x2,y1,y2;
        int tox = 0,lx=0x3f3f3f3f,rx=0;
        int toy = 0,ly=0x3f3f3f3f,ry=0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            lx = min(x1,lx),rx = max(rx,x2);
            ly = min(y1,ly),ry = max(ry,y2);
            tx[tox].l = x1,tx[tox].r = x2,tx[tox].high=y1,tx[tox++].va=1;
            tx[tox].l = x1,tx[tox].r = x2,tx[tox].high=y2,tx[tox++].va=-1;
            ty[toy].l = y1,ty[toy].r = y2,ty[toy].high=x1,ty[toy++].va=1;
            ty[toy].l = y1,ty[toy].r = y2,ty[toy].high=x2,ty[toy++].va=-1;
        }
        sort(tx,tx+tox,cmp);
        sort(ty,ty+toy,cmp);
        int ans=0,prelen = 0;
        build(1,lx,rx-1);
        for(int i = 0; i < tox; i++)
        {
            Insert(1,tx[i].l,tx[i].r-1,tx[i].va);
            ans += abs(tree[1].len-prelen);
            prelen = tree[1].len;
        }
        //cout << ans <<endl;
        build(1,ly,ry-1);
        prelen = 0;
        for(int i = 0; i < toy; i++)
        {
            Insert(1,ty[i].l,ty[i].r-1,ty[i].va);
            ans += abs(tree[1].len-prelen);
            prelen = tree[1].len;
        }
        cout << ans <<endl;
    }
    return 0;
}


Second

/*
hdu 1828 线段树扫描(周长)2

在向上扫描的过程中我们可以计算出平行于x轴的长度
然后在两条线之间我们只需要计算出有多少条竖线便能得带这两条线之间平行于
y轴的长度。
用ls和rs来表示当前节点左右端点是否被覆盖
在处理竖线数量时,注意合并带来的影响,假设lson.rs和rson.ls都存在值的话
说明存在重叠的部分

hhh-2016-03-26 17:58:50
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
using namespace std;
#define lson  (i<<1)
#define rson  ((i<<1)|1)
typedef long long ll;
const int maxn = 20050;
int a[maxn];
struct node
{
    int l,r;
    int sum,len;
    int num,ls,rs;
    int mid()
    {
        return (l+r)>>1;
    }
} tree[maxn*5];

void push_up(int i)
{
    if(tree[i].sum)
    {
        tree[i].len = (tree[i].r-tree[i].l+1);
        tree[i].ls = tree[i].rs = 1;
        tree[i].num = 1;
    }
    else if(tree[i].l == tree[i].r)
    {
        tree[i].len= 0;
        tree[i].num=tree[i].rs=tree[i].ls=0;
    }
    else
    {
        tree[i].len = tree[lson].len+tree[rson].len;
        tree[i].ls = tree[lson].ls;
        tree[i].rs = tree[rson].rs;
        tree[i].num = tree[lson].num+tree[rson].num;
        tree[i].num -= (tree[lson].rs&tree[rson].ls);
        //减去重叠的部分
    }
}

void build(int i,int l,int r)
{
    tree[i].l = l;
    tree[i].r = r;
    tree[i].sum = tree[i].len = 0;
    tree[i].ls = tree[i].rs = tree[i].num = 0;
    if(l == r)
        return ;

    int mid = tree[i].mid();
    build(lson,l,mid);
    build(rson,mid+1,r);
    push_up(i);
}

void push_down(int i)
{

}

void Insert(int i,int l,int r,int val)
{
    if(tree[i].l >= l && tree[i].r <=r )
    {
        tree[i].sum += val;
        push_up(i);
        return ;
    }
    int mid = tree[i].mid();
    push_down(i);
    if(l <= mid)
        Insert(lson,l,r,val);
    if(r > mid)
        Insert(rson,l,r,val);
    push_up(i);
}

struct edge
{
    int l,r;
    int va,high,id;
};
edge tx[maxn*2];

bool cmp(edge a,edge b)
{
    if(a.high != b.high)
        return a.high < b.high;
    else
        return a.va > b.va;
}

int main()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        int x1,x2,y1,y2;
        int tox = 0,lx=0x3f3f3f3f,rx=0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            lx = min(x1,lx),rx = max(rx,x2);
            tx[tox].l = x1,tx[tox].r = x2,tx[tox].high=y1,tx[tox++].va=1;
            tx[tox].l = x1,tx[tox].r = x2,tx[tox].high=y2,tx[tox++].va=-1;
        }
        sort(tx,tx+tox,cmp);
        int ans=0,prelen = 0;
        build(1,lx,rx-1);
        //tx[tox] = tx[tox+1];
        for(int i = 0; i < tox; i++)
        {
            Insert(1,tx[i].l,tx[i].r-1,tx[i].va);
            ans += abs(tree[1].len-prelen);
            if(i != tox-1)
            ans += (tx[i+1].high-tx[i].high)*tree[1].num*2;
            prelen = tree[1].len;
        }
        cout << ans <<endl;
    }
    return 0;
}


<think> 嗯...用户这次问的是扫描线算法中线段树是否动态开点。看来用户正在学习计算几何的高级数据结构应用,可能是个OI选手或者算法爱好者。 从用户引用的两篇文献来看,第一篇详细描述了扫描线配合线段树求矩形面积并的标准流程,第二篇则涉及矩形覆盖最大点数的变种问题。用户能提出"动态开点"这样的专业术语,说明已经对线段树的常见优化方式有所了解。 关于动态开点问题,需要分场景讨论:在标准的离散化扫描线中,由于横坐标已离散化为固定区间,线段树可以预先建树无需动态开点;但在处理浮点数或值域过大的情况时,动态开点就成为必要选择。这个区别很关键,用户可能正在处理特殊场景才会这么问。 考虑到用户之前关注过线段树维护区间覆盖的技术细节,后续可以补充离散化与动态开点的性能对比。不过当前问题需要先明确回答核心疑问——常规实现不用动态开点,特殊情况才需要。 </think> 在扫描线算法中,线段树**通常不需要动态开点**,但具体实现取决于问题需求和坐标系特征: ### 📍 标准实现(非动态开点) 1. **离散化处理** 扫描线算法的关键步骤是将所有坐标点在 $x$$-轴上离散化(如图1所示): $$ \text{步骤:收集坐标} \rightarrow \text{排序} \rightarrow \text{去重} \rightarrow \text{建立映射} $$ 离散化后,线段树管理的区间数量被压缩为 $O(n)$ 级别($n$ 为坐标点数),因此可直接**预建满二叉树**,无需动态开点[^1]。 2. **空间复杂度优化** 预建树的空间复杂度为 $O(n)$,例如: - 原始坐标范围:$[1, 10^9]$ - 离散化后区间数:$2000$(若 $n=1000$) 此时只需开 $4 \times 2000 = 8000$ 的节点数组,完全可行[^1]。 3. **维护信息** - 节点存储 **区间覆盖次数** 和 **有效长度** - 更新操作:区间加减(矩形入边 `+1`,出边 `-1`) - 查询操作:直接取根节点有效长度 ### ⚡ 需动态开点的情况 | **场景** | **原因** | **示例** | |-----------------------|--------------------------------------------------------------------------|---------------------------| | **值域极大且无法离散化** | 浮点数坐标或值域超限(如 $[1, 10^{18}]$) | 地理信息系统(GIS)中的浮点计算 | | **强制在线查询** | 矩形边界实时插入,无法预离散化 | 流式数据处理场景 | | **内存严格受限** | $n$ 极大(如 $>10^6$),离散化后区间仍过多,需节省空间 | 嵌入式设备上的算法部署 | ### 🌰 实例对比 1. **标准问题(矩形面积并)** ```python # 离散化坐标 xs = sorted(set(x_coords)) tree = SegmentTree([0]*4*len(xs)) # 预建树 ``` 2. **动态开点问题(浮点数覆盖)** ```python class Node: def __init__(self): self.cover = 0 self.len = 0.0 self.lson = None # 动态创建子节点 self.rson = None ``` ### ✅ 结论 - **大多数场景**(如引用[1][2]的矩形问题):离散化 + 静态线段树 ✅ - **特殊场景**(浮点数/在线查询):动态开点线段树 ✅ 若处理整数坐标系且可离散化,推荐静态实现,效率更高且编码简单[^1][^2]。 --- ### ❓相关问题 1. 扫描线算法中如何处理浮点数坐标的精度问题? 2. 离散化过程如何避免哈希冲突导致的查询错误? 3. 动态开点线段树扫描线中的更新操作时间复杂度如何保证? 4. 矩形覆盖最大值问题(如HDU 5091)如何转化为扫描线模型?[^2] [^1]: 线段树应用——扫描线,离散化坐标与静态建树 [^2]: HDU 5091 矩形覆盖问题中的扫描线转化技巧
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值