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.

 
  题目就是求矩形的并的面积,典型的扫描线的题目,对于中间有个洞的矩形,可以拆分成四个来做。
  
  网上还有一种办法,就是对扫描线的线段树进行改进,在HDU这个题的Discuss里面有。
  
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> #define lc po*2
#define rc po*2+1
#define lson L,M,lc
#define rson M+1,R,rc using namespace std; struct BIAN
{
int x,y1,y2;
short state;
}; const int maxn=; BIAN bian[*];
int BIT[maxn*];
int COL[maxn*]; void pushUP(int L,int R,int po)
{
if(COL[po])
BIT[po]=R+-L;
else if(L==R)
BIT[po]=;
else
BIT[po]=BIT[lc]+BIT[rc];
} void update(int ul,int ur,int ut,int L,int R,int po)
{
if(ul<=L&&ur>=R)
{
COL[po]+=ut;
pushUP(L,R,po); return;
} int M=(L+R)/; if(ul<=M)
update(ul,ur,ut,lson);
if(ur>M)
update(ul,ur,ut,rson); pushUP(L,R,po);
} bool cmp(BIAN a,BIAN b)
{
return a.x<b.x;
} int main()
{
int N;
long long ans;
int x1,x2,x3,x4,y1,y2,y3,y4; while(~scanf("%d",&N))
{
if(!N)
break; memset(COL,,sizeof(COL));
memset(BIT,,sizeof(BIT));
ans=; for(int i=;i<=N;++i)
{
scanf("%d %d %d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4); bian[i*-].x=x1;
bian[i*-].y1=y1;
bian[i*-].y2=y2;
bian[i*-].state=; bian[i*-].x=x3;
bian[i*-].y1=y1;
bian[i*-].y2=y2;
bian[i*-].state=-; bian[i*-].x=x4;
bian[i*-].y1=y1;
bian[i*-].y2=y2;
bian[i*-].state=; bian[i*-].x=x2;
bian[i*-].y1=y1;
bian[i*-].y2=y2;
bian[i*-].state=-; bian[i*-].x=x3;
bian[i*-].y1=y4;
bian[i*-].y2=y2;
bian[i*-].state=; bian[i*].x=x4;
bian[i*].y1=y4;
bian[i*].y2=y2;
bian[i*].state=-; bian[i*-].x=x3;
bian[i*-].y1=y1;
bian[i*-].y2=y3;
bian[i*-].state=; bian[i*-].x=x4;
bian[i*-].y1=y1;
bian[i*-].y2=y3;
bian[i*-].state=-;
} sort(bian+,bian+*N+,cmp); for(int i=;i<=*N;++i)
{
ans+=(long long)BIT[]*(bian[i].x-bian[i-].x); if(bian[i].y2>bian[i].y1)
update(bian[i].y1,bian[i].y2-,bian[i].state,,,);
} cout<<ans<<endl;
} return ;
}

  下面是学习了新的方法后做的,就是对线段树进行修改,用上pushDown,保证线段树的每一个节点的值都不会小于0。

  这样直接分成两个矩形,中间那个的左边的state为-1,右边的为1.

  PS:这个题在杭电上有讨论long long会wa但是unsigned int就可以的问题,其实是因为没有加强制转换导致的,long long可以ac。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm> #define lc po*2
#define rc po*2+1
#define lson L,M,lc
#define rson M+1,R,rc
#define ji1 i*4-3
#define ji2 i*4-2
#define ji3 i*4-1
#define ji4 i*4 using namespace std; struct BIAN
{
int x,y1,y2;
short state;
}; const int maxn=; BIAN bian[maxn*];
long long BIT[maxn*];
int COL[maxn*]; void callUP(int L,int R,int po)
{
if(COL[po]>=)
BIT[po]=R+-L;
else if(L==R)
BIT[po]=;
else
BIT[po]=BIT[lc]+BIT[rc];
} void pushUP(int L,int R,int po)
{
if(L!=R) //如果不判断会越界。
{
int temp=min(COL[lc],COL[rc]); COL[po]+=temp;
COL[lc]-=temp;
COL[rc]-=temp; callUP(L,(L+R)/,lc);
callUP((L+R)/+,R,rc);
} callUP(L,R,po);
} void pushDown(int L,int R,int po)
{
if(COL[po])
{
COL[lc]+=COL[po];
COL[rc]+=COL[po]; callUP(L,(L+R)/,lc);
callUP((L+R)/+,R,rc); COL[po]=;
callUP(L,R,po);
}
} void update(int ul,int ur,int ut,int L,int R,int po)
{
if(ul<=L&&ur>=R)
{
COL[po]+=ut;
pushUP(L,R,po); return;
} pushDown(L,R,po); int M=(L+R)/; if(ul<=M)
update(ul,ur,ut,lson);
if(ur>M)
update(ul,ur,ut,rson); pushUP(L,R,po); } bool cmp(BIAN a,BIAN b)
{
return a.x<b.x;
} int main()
{
int N;
int cas=;
long long ans;
int x1,x2,y1,y2,x3,x4,y3,y4; ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(); for(cin>>N;N;cin>>N)
{
memset(BIT,,sizeof(BIT));
memset(COL,,sizeof(COL)); for(int i=;i<=N;++i)
{
cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4; bian[ji1].state=;
bian[ji2].state=-;
bian[ji3].state=-;
bian[ji4].state=; bian[ji1].x=x1;
bian[ji2].x=x2;
bian[ji3].x=x3;
bian[ji4].x=x4; bian[ji1].y1=bian[ji2].y1=y1;
bian[ji1].y2=bian[ji2].y2=y2;
bian[ji3].y1=bian[ji4].y1=y3;
bian[ji3].y2=bian[ji4].y2=y4;
} sort(bian+,bian+*N+,cmp); ans=; for(int i=;i<=*N;++i)
{
ans+=(long long)BIT[]*(bian[i].x-bian[i-].x); //要加强制转换。 update(bian[i].y1,bian[i].y2-,bian[i].state,,,);
} cout<<ans<<endl;
} return ;
}

(中等) HDU 3265 Posters , 扫描线。的更多相关文章

  1. HDU 3265 Posters(线段树)

    HDU 3265 Posters pid=3265" target="_blank" style="">题目链接 题意:给定一些矩形海报.中间有 ...

  2. HDU 3265 Posters (线段树+扫描线)(面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 给你n个中间被挖空了一个矩形的中空矩形,让你求他们的面积并. 其实一个中空矩形可以分成4个小的矩 ...

  3. hdu 3265 Posters(线段树+扫描线+面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 题意:给你一张挖了洞的墙纸贴在墙上,问你总面积有多少. 挖了洞后其实就是多了几个矩形墙纸,一张墙 ...

  4. HDU 3265 Posters ——(线段树+扫描线)

    第一次做扫描线,然后使我对线段树的理解发生了动摇= =..这个pushup写的有点神奇.代码如下: #include <stdio.h> #include <algorithm> ...

  5. HDU 3265 Posters

    矩形面积并,一个拆成四个 #include<cstdio> #include<cstring> #include<cmath> #include<map> ...

  6. HDU 3511 圆扫描线

    找最深的圆,输出层数 类似POJ 2932的做法 圆扫描线即可.这里要记录各个圆的层数,所以多加一个维护编号的就行了. /** @Date : 2017-10-18 18:16:52 * @FileN ...

  7. HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)

    Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...

  8. HDU 3265 扫描线(矩形面积并变形)

    Posters Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  9. (中等) HDU 1828 Picture,扫描线。

    Problem Description A number of rectangular posters, photographs and other pictures of the same shap ...

随机推荐

  1. php 10.2总

    注意事项 获取表单信息 <?php if($_POST["submit"]=="登录"){ echo "您输入的用户名为:".$_PO ...

  2. Ubuntu下修改DNS重启也能用的方法

    1.通过修改:/etc/resolvconf/resolv.conf.d/base(这个文件默认是空的)实现 内容填上需要修改的nameserver

  3. python 中调用shell命令

    subprocess模块 根据Python官方文档说明,subprocess模块用于取代上面这些模块.有一个用Python实现的并行ssh工具—mssh,代码很简短,不过很有意思,它在线程中调用sub ...

  4. 前端知识复习一(css)

    1.清楚浮动 父盒子高度为0,子盒子全部定位.浮动.子盒子不会撑开父盒子,下面的盒子会顶上来 清楚方法: clear:both: overflow:hidden: 加空标签 单/双 //双标签 .cl ...

  5. POJ - 3061 Subsequence(连续子序列和>=s的最短子序列长度)

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  6. Html基础详解之(jquery)

    jquery选择器: #id 根据给定的ID匹配一个元素,如果选择器中包含特殊字符,可以用两个斜杠转义.(注:查找 ID 为"myDiv"的元素.) <!DOCTYPE ht ...

  7. android - 自定义(组合)控件 + 自定义控件外观

    转载:http://www.cnblogs.com/bill-joy/archive/2012/04/26/2471831.html android - 自定义(组合)控件 + 自定义控件外观   A ...

  8. myeclipse连接hadoop集群编程及问题解决

    原以为搭建一个本地编程测试hadoop程序的环境很简单,没想到还是做得焦头烂额,在此分享步骤和遇到的问题,希望大家顺利. 一.要实现连接hadoop集群并能够编码的目的需要做如下准备: 1.远程had ...

  9. Diamond Collector

    Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining ...

  10. SQL模擬死結產生

    引用自:http://jengting.blogspot.tw/2012/06/sql.html 根據 MSDN 將死結數量降至最低 裡的圖型模擬死結產生 ~~ 在 SSMS 內開啟兩個 T-SQL ...