hdu 4052 线段树扫描线、奇特处理

Adding New Machine

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1428    Accepted Submission(s): 298


Problem Description
Incredible Crazily Progressing Company (ICPC) suffered a lot with the low speed of procedure. After investigation, they found that the bottleneck was at Absolutely Crowded Manufactory (ACM). In oder to accelerate the procedure, they bought a new machine for ACM. But a new problem comes, how to place the new machine into ACM? 

ACM is a rectangular factor and can be divided into W * H cells. There are N retangular old machines in ACM and the new machine can not occupy any cell where there is old machines. The new machine needs M consecutive cells. Consecutive cells means some adjacent cells in a line. You are asked to calculate the number of ways to choose the place for the new machine. 
 

Input
There are multiple test cases (no more than 50). The first line of each test case contains 4 integers W, H, N, M (1 ≤ W, H ≤ 107, 0 ≤ N ≤ 50000, 1 ≤ M ≤ 1000), indicating the width and the length of the room, the number of old machines and the size of the new machine. Then N lines follow, each of which contains 4 integers Xi1, Yi1, Xi2 and Yi2 (1 ≤ Xi1 ≤ Xi2 ≤ W, 1 ≤ Yi1 ≤ Yi2 ≤ H), indicating the coordinates of the i-th old machine. It is guarantees that no cell is occupied by two old machines. 
 

Output
Output the number of ways to choose the cells to place the new machine in one line. 
 
Sample Input
3 3 1 2
2 2 2 2
3 3 1 3
2 2 2 2
2 3 2 2
1 1 1 1
2 3 2 3
 Sample Output
8
4
3

/*
hdu 4052 线段树扫描线、奇特处理

给你W*H大小的矩形,其中有N个地区不能使用(给出了这个地区的两个顶点的坐标即(x1,y1)
和(x2,y2)),问能下多少个1*M的矩形。

但是看见题目有想到了扫描线,但是一直不知道应该怎么处理后来偶然看见别人提示可以转换
成求面积,大致就有了思路

假设1*n的矩阵中放入1*m的矩阵,能有多少种?    n-m+1
我们扫描每一列,两个相邻为n的旧机器中就能放下n-m+1个新机器,于是原先的旧机器矩形
就变成了(x1,y1,x2+ma-1,y2)(从下往上扫描)
        (x1,y1,x2,y2+ma-1)(从左往右扫描)
而剩下的为被占据的位置就是方案数了
因为我是在每个旧机器往右边添加的,所以还要解决这一列没有从1开始的情况,所以在最左边
加上(1,1,ma,h+1)的矩阵
而且ma=1时,横着放和竖着放是一样的,所以除以2

但是第一个版本写出来一直 RuntimeError
后来实在没法又换了个,把离散化用vec处理终于出现了WR(TAT)
主要是 ma == 1 情况,因为我会在1添加一个矩阵,但是当ma==1时这个矩阵也被建立了就导致
(1,1,1,h+1) 由于是按边建树l=x1,r=x2-1 -> r<l (- -!好气)  //应该多测几次的

然后进行了特判第一个也过了

hhh-2016-03-30 22:26:25
*/


//Second
#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <functional>
#include <map>
#include <algorithm>
#include <queue>
#include <vector>
#define lson (i<<1)
#define rson ((i<<1)|1)
typedef long long ll;
using namespace std;

const int maxn = 1000005;
vector<int> vec;
int w,h;
int x[maxn],y[maxn],tx[maxn],ty[maxn];
map<int,int > mp;
int n,ma;
struct node
{
    int l,r;
    int sum;
    ll len;
    int mid()
    {
        return (l+r)>>1;
    }
} tree[maxn<<2];

void push_up(int i)
{
    if(tree[i].sum)
        tree[i].len = vec[tree[i].r+1]-vec[tree[i].l];
    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 = tree[i].len = 0;
    if(l == r)
        return;
    build(lson,l,tree[i].mid());
    build(rson,tree[i].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);
    return ;
}

struct edge
{
    int l,r;
    int high;
    int va;
};
edge Line[maxn<<2];
int m;
bool cmp(edge a,edge b)
{
    if(a.high != b.high)
        return a.high < b.high;
    else
        return a.va > b.va;
}

int tox;
ll ans;
void solve(int cur,int hi,int wi)
{
    vec.clear();
    if(cur)
    {
        for(int i =1; i <= n; i++)
            swap(x[i],y[i]),swap(tx[i],ty[i]);
    }
    tox = 0;
    for(int i = 1; i <= n; i++)
    {
        int t = min(wi+1,tx[i]+ma-1);
        Line[tox].l = x[i],Line[tox].r =t,Line[tox].high = y[i],Line[tox++].va = 1;
        Line[tox].l = x[i],Line[tox].r =t,Line[tox].high = ty[i],Line[tox++].va = -1;
        vec.push_back(x[i]);
        vec.push_back(t);
    }
    if(ma != 1)
    {
        Line[tox].l = 1,Line[tox].r = ma,Line[tox].high=1,Line[tox++].va=1;
        Line[tox].l = 1,Line[tox].r = ma,Line[tox].high=hi+1,Line[tox++].va=-1;
        vec.push_back(1),vec.push_back(ma);
    }
    sort(Line,Line+tox,cmp);
    sort(vec.begin(),vec.end());
    vec.erase(unique(vec.begin(),vec.end()),vec.end());
    int m = vec.size();
    for(int i = 0; i < m; i++)
        mp[vec[i]] = i;
    build(1,0,m);
    int l,r;
    for(int i = 0; i < tox-1; i++)
    {
        l = mp[Line[i].l];
        r = mp[Line[i].r]-1;
        if(r < l)
            continue;
        Insert(1,l,r,Line[i].va);
        ans -= (ll)tree[1].len*(Line[i+1].high-Line[i].high);
    }
    //cout << tans <<endl;
}

int main()
{
    while(scanf("%d%d%d%d",&w,&h,&n,&ma) != EOF)
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d%d%d",&x[i],&y[i],&tx[i],&ty[i]);
            tx[i]++,ty[i]++;
        }

        ans =(ll)w*h*2;
        solve(0,h,w);
        solve(1,w,h);
        if(ma == 1)
            ans /= 2;
        printf("%I64d\n",ans);
    }
    return 0;
}


/*
First:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <functional>
#include <map>
#include <algorithm>
#include <queue>

#define lson (i<<1)
#define rson ((i<<1)|1)
typedef long long ll;
using namespace std;

const int maxn = 1000005;

ll w,h;
int n,ma;
int now;
struct node
{
    int l,r;
    int sum;
    ll len;
    int mid()
    {
        return (l+r)>>1;
    }
} tree[maxn<<2];
ll hs[2][maxn];

void push_up(int i)
{
    if(tree[i].sum)
        tree[i].len = hs[now][tree[i].r+1]-hs[now][tree[i].l];
    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 = tree[i].len = 0;
    if(l == r)
        return;
    build(lson,l,tree[i].mid());
    build(rson,tree[i].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);
    return ;
}

struct edge
{
    ll l,r;
    ll high;
    int va;
};
edge tx[maxn<<2];
edge ty[maxn<<2];
int m;
bool cmp(edge a,edge b)
{
    if(a.high != b.high)
        return a.high < b.high;
    else
        return a.va > b.va;
}
int bin(int cur,ll x)
{
    int l = 0,r = m-1;
    while(l <= r)
    {
        int mid = (l+r)>>1;
        if(hs[cur][mid] == x)
            return mid;
        else if(hs[cur][mid] < x)
            l = mid+1;
        else
            r = mid-1;
    }
}
int tox,toy;
ll solve(int cur)
{
    now = cur;
    int len = (cur == 0 ? tox:toy);
    m = 1;
    for(int i = 1; i < len; i++) //ШЅжи
    {
        if(hs[cur][i] != hs[cur][i-1])
            hs[cur][m++] = hs[cur][i];
    }
//    for(int i = 0;i < m;i++)
//        printf("%d ",hs[cur][i]);
//    cout <<endl;
    build(1,0,m);
    ll tans = 0;
    int l,r;
    for(int i = 0; i < len-1; i++)
    {
        if(cur == 0)
        {
            l = bin(cur,tx[i].l);
            r = bin(cur,tx[i].r)-1;
            Insert(1,l,r,tx[i].va);
            tans += (ll)tree[1].len*(tx[i+1].high-tx[i].high);
        }
        else
        {
            l = bin(cur,ty[i].l);
            r = bin(cur,ty[i].r)-1;
            if(r < l )continue;
            Insert(1,l,r,ty[i].va);
            tans += (ll)tree[1].len*(ty[i+1].high-ty[i].high);
        }

        //cout << tree[i].len << endl;
        //cout << tans <<endl;

    }
    //cout << tans <<endl;
    return tans;
}

int main()
{
    while(scanf("%I64d%I64d%d%d",&w,&h,&n,&ma) != EOF)
    {
        tox = 0,toy = 0;
        ll x1,y1,x2,y2;
        for(int i = 1; i <= n; i++)
        {
            scanf("%I64d%I64d%I64d%I64d",&x1,&y1,&x2,&y2);
            x2++,y2++;
            ll t1 = (x2+ma-1)>w+1? w+1:x2+ma-1;
            tx[tox].l = x1,tx[tox].r = t1,tx[tox].high = y1,tx[tox].va = 1;
            hs[0][tox++] = x1;
            tx[tox].l = x1,tx[tox].r = t1,tx[tox].high = y2,tx[tox].va = -1;
            hs[0][tox++] = t1;

            t1 = (y2+ma-1)>h+1? h+1:y2+ma-1;
            ty[toy].l = y1,ty[toy].r = t1,ty[toy].high = x1,ty[toy].va = 1;
            hs[1][toy++] = y1;
            ty[toy].l = y1,ty[toy].r = t1,ty[toy].high = x2,ty[toy].va = -1;
            hs[1][toy++] = t1;
        }
        if(ma != 1){
        tx[tox].l = 1,tx[tox].r = ma,ty[toy].l=1,ty[toy].r = ma;
        tx[tox].high=1,tx[tox].va=1,ty[toy].high=1,ty[toy].va=1;
        hs[0][tox++] = 1,hs[1][toy++]=1;

        tx[tox].l = 1,tx[tox].r = ma,ty[toy].l=1,ty[toy].r = ma;
        tx[tox].high=h+1,tx[tox].va=-1,ty[toy].high=w+1,ty[toy].va=-1;
        hs[0][tox++] = ma,hs[1][toy++] = ma;
        }
        sort(hs[0],hs[0]+tox);
        sort(hs[1],hs[1]+toy);
        sort(tx,tx+tox,cmp);
        sort(ty,ty+toy,cmp);
        ll ans = w*h*2;

        ans -= solve(0);
        //printf("%I64d\n",ans);
        ans -= solve(1);
        if(ma == 1)
            ans /= 2;
        printf("%I64d\n",ans);
    }
    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、付费专栏及课程。

余额充值