hdu 3265 Posters (线段树,离散化+扫描线)

119 篇文章 1 订阅
113 篇文章 1 订阅

题目链接:传送门

Posters

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6379    Accepted Submission(s): 1537


Problem Description
Ted has a new house with a huge window. In this big summer, Ted decides to decorate the window with some posters to prevent the glare outside. All things that Ted can find are rectangle posters. 

However, Ted is such a picky guy that in every poster he finds something ugly. So before he pastes a poster on the window, he cuts a rectangular hole on that poster to remove the ugly part. Ted is also a careless guy so that some of the pasted posters may overlap when he pastes them on the window. 

Ted wants to know the total area of the window covered by posters. Now it is your job to figure it out.

To make your job easier, we assume that the window is a rectangle located in a rectangular coordinate system. The window’s bottom-left corner is at position (0, 0) and top-right corner is at position (50000, 50000). The edges of the window, the edges of the posters and the edges of the holes on the posters are all parallel with the coordinate axes. 


Input
The input contains several test cases. For each test case, the first line contains a single integer N (0<N<=50000), representing the total number of posters. Each of the following N lines contains 8 integers x1, y1, x2, y2, x3, y3, x4, y4, showing details about one poster. (x1, y1) is the coordinates of the poster’s bottom-left corner, and (x2, y2) is the coordinates of the poster’s top-right corner. (x3, y3) is the coordinates of the hole’s bottom-left corner, while (x4, y4) is the coordinates of the hole’s top-right corner. It is guaranteed that 0<=xi, yi<=50000(i=14) and x1<=x3<x4<=x2, y1<=y3<y4<=y2.

The input ends with a line of single zero.


Output
For each test case, output a single line with the total area of window covered by posters.


Sample Input
2
0 0 10 10 1 1 9 9
2 2 8 8 3 3 7 7
0


Sample Output
56

遇到新的做法要仔细分析,不要为了做题而做题,多思考,这是如何想出的,过程要仔细分析,遇到相似的题,就有思路了。

大意:有n张海报(形状是矩形),每张海报上都有一个被挖去的小矩形,问:贴完所有的海报后,问海报的面积;

这一题有两种思路(不过都是离散化,扫描线,线段树);

第一种:
思路:最容易想到的一种,将海报分成4个小矩形(如下图所示),这样就成了求4*n个海报,贴在墙上的面积。
这里写图片描述

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<map>
#define N 50009

struct node
{
    int l,r;
    int chang;
    int inq;
} tree[N*10];

struct nodee
{
    int h1,h2;
    int x;
    int inq;
    void get_date(int a,int b,int c,int d)
    {
        h1=a,h2=b,x=c,inq=d;
    }
    bool operator<(const nodee &p)
    {
        return x<p.x;
    }
} dis[N*10];

map<int,int>q;
int a[N*10];

void build(int num,int l,int r)//建树
{
    tree[num].chang=0;
    tree[num].inq=0;
    tree[num].l=l;
    tree[num].r=r;
    if(l==r) return ;
    int mid=(r+l)>>1;
    build(num<<1,l,mid);
    build(num<<1|1,mid+1,r);
}
void up(int num)
{
    if(tree[num].inq>0)
        tree[num].chang=a[tree[num].r+1]-a[tree[num].l];
    else if(tree[num].l==tree[num].r)
        tree[num].chang=0;
    else tree[num].chang=tree[num<<1].chang+tree[num<<1|1].chang;
}
void update(int num,int x,int y,int p)//更新
{
    int l=tree[num].l,r=tree[num].r,mid=(r+l)>>1;
    if(x<=l&&r<=y)
    {
        tree[num].inq+=p;
        up(num);
        return ;
    }
    if(x<=mid) update(num<<1,x,y,p);
    if(y>mid) update(num<<1|1,x,y,p);
    up(num);
}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        q.clear();
        memset(a,0,sizeof(a));
        int x1, y1, x2, y2, x3, y3, x4, y4;
        int m=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
            a[m]=x1;
            dis[m++].get_date(x1,x2,y1,1);
            a[m]=x2;
            dis[m++].get_date(x1,x2,y3,-1);
            a[m]=x1;
            dis[m++].get_date(x1,x3,y3,1);
            a[m]=x3;
            dis[m++].get_date(x1,x3,y4,-1);
            a[m]=x4;
            dis[m++].get_date(x4,x2,y3,1);
            a[m]=x2;
            dis[m++].get_date(x4,x2,y4,-1);
            a[m]=x1;
            dis[m++].get_date(x1,x2,y4,1);
            a[m]=x2;
            dis[m++].get_date(x1,x2,y2,-1);
        }
        sort(a,a+m);
        sort(dis,dis+m);
        int mm=unique(a,a+m)-a;//离散化,去重
        for(int i=0; i<mm; i++)
            q[a[i]]=i;
        build(1,0,mm-1);
        long long sum=0;
        for(int i=0; i<m-1; i++)
        {
            if(q[dis[i].h1]<=q[dis[i].h2]-1)//避免出现越界情况
                update(1,q[dis[i].h1],q[dis[i].h2]-1,dis[i].inq);
            sum+=(long long)(dis[i+1].x-dis[i].x)*tree[1].chang;
        }
        printf("%lld\n",sum);
    }
    return 0;
}

第二种:
这里写图片描述
思路:如上图所示,x1到x2范围后面是有阴影的(即是有海报的),所以在记录时,标记为1,而x3到x4范围后面是没有阴影的(即是没有海报的),所以在记录时,标记为-1,按照这种方式,使用扫描线就可以了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<map>
#define N 50009

struct node
{
    int l,r;
    int inq;
} tree[N*100];

struct nodee
{
    int h1,h2;
    int x;
    int inq;
    void get_date(int a,int b,int c,int d)
    {
        h1=a,h2=b,x=c,inq=d;
    }
    bool operator<(const nodee &q) const
    {
        return x<q.x;
    }
} dis[N*100];

map<int,int>q;
int a[N*100];

void build(int num,int l,int r)
{
    tree[num].inq=0;
    tree[num].l=l;
    tree[num].r=r;
    if(l==r) return ;
    int mid=(r+l)>>1;
    build(num<<1,l,mid);
    build(num<<1|1,mid+1,r);
}

void doo(int num)
{
    if(tree[num].inq)
    {
        int h=tree[num].inq;
        tree[num<<1].inq+=h;
        tree[num<<1|1].inq+=h;
        tree[num].inq=0;
    }
}
void he(int num)
{
    int q1=tree[num<<1].inq,q2=tree[num<<1|1].inq;
    if(q1&&q2)
    {
        q1=min(q1,q2);//取覆盖次数最少的
        tree[num].inq=q1;
        tree[num<<1].inq-=q1;
        tree[num<<1|1].inq-=q1;
    }
}

void update(int num,int x,int y,int p)
{
    int l=tree[num].l,r=tree[num].r,mid=(l+r)>>1;
    if(x<=l&&r<=y)
    {
        tree[num].inq+=p;
        return ;
    }
    doo(num);//如果该区间被覆盖,说明左右子区间被覆盖
    if(x<=mid) update(num<<1,x,y,p);
    if(y>mid) update(num<<1|1,x,y,p);
    he(num);//如果左右子区间,都被覆盖过,说明该区间被覆盖
}

long long query(int num)//查找区域
{
    int l=tree[num].l,r=tree[num].r,mid=(l+r)>>1;
    if(tree[num].inq)//区域被覆盖过
        return (long long)a[r+1]-a[l];
    if(l==r) return 0;//查找到头,没被覆盖过
    doo(num);
    return query(num<<1)+query(num<<1|1);
}

int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        q.clear();
        memset(a,0,sizeof(a));
        int x1, y1, x2, y2, x3, y3, x4, y4;
        int m=0;
        int M=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
            dis[m++].get_date(x1,x2,y1,1);//纵坐标为y1的[x1,x2]区域后,有面积的
            dis[m++].get_date(x3,x4,y3,-1);//纵坐标为y3的[x3,x4]区域后,没有面积的
            dis[m++].get_date(x3,x4,y4,1);//纵坐标为y4的[x3,x4]区域后,有面积的
            dis[m++].get_date(x1,x2,y2,-1);//纵坐标为y2的[x1,x2]区域后,没有面积的
            a[M++]=x1;
            a[M++]=x2;
            a[M++]=x3;
            a[M++]=x4;
        }
        sort(a,a+M);
        sort(dis,dis+m);//按照纵坐标(x),从小到大排
        M=unique(a,a+M)-a;//离散化,去重
        for(int i=0; i<M; i++)
            q[a[i]]=i;//记录离散化后的数值
        build(1,0,M-1);
        long long sum=0;
        for(int i=0; i<m-1; i++)
        {
            update(1,q[dis[i].h1],q[dis[i].h2]-1,dis[i].inq);
            long long f=query(1);//查询范围
            long long h=(long long)(dis[i+1].x-dis[i].x)*f;
            sum+=h;
        }
        printf("%lld\n",sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值