学习链接:http://blog.csdn.net/lwt36/article/details/48908031

学习扫描线主要学习的是一种扫描的思想,后期可以求解很多问题。

扫描线求矩形周长并

hdu 1928

Picture

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

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
 
Source
 
Recommend
linle
 
 

 
 
 
 //#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#define clr(x) memset(x,0,sizeof(x))
#define MAXN 50010
using namespace std;
struct edgx
{
int l,u,x;
int d;
}edgex[MAXN];
struct edgy
{
int l,r,y;
int d;
}edgey[MAXN];
struct seg
{
int l,r,cov,len;
}segt[MAXN<<];
int cntx,cnty;
int x[MAXN],y[MAXN],vec[MAXN];
bool cmpy(edgy a,edgy b)
{
if(a.y==b.y) return a.d>b.d;
return a.y<b.y;
}
bool cmpx(edgx a,edgx b)
{
if(a.x==b.x) return a.d>b.d;
return a.x<b.x;
}
void init(int i,int l,int r)
{
segt[i]=(seg){l,r,,};
if(l==r)
return ;
int mid=(l+r)>>;
init(i<<,l,mid);
init(i<<|,mid+,r);
return ;
}
void pushup(int i)
{
if(segt[i].cov)
{
segt[i].len=vec[segt[i].r+]-vec[segt[i].l];
}
else if(segt[i].l==segt[i].r)
{
segt[i].len=;
}
else
{
segt[i].len=segt[i<<].len+segt[i<<|].len;
}
return ;
}
void update(int i,int l,int r,int value)
{
if(segt[i].l>=l && segt[i].r<=r)
{
segt[i].cov+=value;
pushup(i);
return ;
}
int mid=(segt[i].l+segt[i].r)>>;
if(mid>=r)
{
update(i<<,l,r,value);
}
else if(mid<l)
{
update(i<<|,l,r,value);
}
else
{
update(i<<,l,r,value);
update(i<<|,l,r,value);
}
pushup(i);
return ;
}
int main()
{
int x1,x2,y1,y2,n,m,T,ans,l,r,k;
while(scanf("%d",&n)!=EOF)
{
cntx=;
cnty=;
for(int i=;i<=n;i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
edgex[++cntx]=(edgx){y1,y2,x1,};
x[cntx]=x1;
edgex[++cntx]=(edgx){y1,y2,x2,-};
x[cntx]=x2;
edgey[++cnty]=(edgy){x1,x2,y1,};
y[cnty]=y1;
edgey[++cnty]=(edgy){x1,x2,y2,-};
y[cnty]=y2;
}
n<<=;
ans=;
memcpy(vec,x,sizeof(x));
sort(vec+,vec+n+);
m=unique(vec+,vec+n+)-vec-;
sort(edgey+,edgey+n+,cmpy);
init(,,m);
for(int i=;i<=n;i++)
if(edgey[i].l<edgey[i].r)
{
k=segt[].len;
l=lower_bound(vec+,vec+m+,edgey[i].l)-vec;
r=lower_bound(vec+,vec+m+,edgey[i].r)-vec;
update(,l,r-,edgey[i].d);
ans+=abs(segt[].len-k);
}
memcpy(vec,y,sizeof(y));
sort(vec+,vec+n+);
m=unique(vec+,vec+n+)-vec-;
sort(edgex+,edgex+n+,cmpx);
init(,,m);
for(int i=;i<=n;i++)
if(edgex[i].l<edgex[i].u)
{
k=segt[].len;
l=lower_bound(vec+,vec+m+,edgex[i].l)-vec;
r=lower_bound(vec+,vec+m+,edgex[i].u)-vec;
update(,l,r-,edgex[i].d);
ans+=abs(segt[].len-k);
}
printf("%d\n",ans);
}
return ;
}

hdu 1255 矩阵面积交

覆盖的面积

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5718    Accepted Submission(s): 2854

Problem Description
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.

 
Input
输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.

注意:本题的输入数据较多,推荐使用scanf读入数据.

 
Output
对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.
 
Sample Input
2
5
1 1 4 2
1 3 3 7
2 1.5 5 4.5
3.5 1.25 7.5 4
6 3 10 7
3
0 0 1 1
1 0 2 1
2 0 3 1
 
Sample Output
7.63
0.00
 
Author
Ignatius.L & weigang Lee
 

 //#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#define clr(x) memset(x,0,sizeof(x))
#define MAXN 10010
using namespace std;
struct edg
{
double l,r,y;
int d;
}edge[MAXN];
struct seg
{
int l,r,cov;
double len1,len2;
}segt[MAXN<<];
int cnt;
double x[MAXN];
bool cmp(edg a,edg b)
{
if(a.y==b.y) return a.d>b.d;
return a.y<b.y;
}
double max(double a,double b)
{
return a>b?a:b;
}
void init(int i,int l,int r)
{
segt[i]=(seg){l,r,,,};
if(l==r)
return ;
int mid=(l+r)>>;
init(i<<,l,mid);
init(i<<|,mid+,r);
return ;
}
void pushup(int i)
{
if(segt[i].cov>=)
{
segt[i].len2=segt[i].len1=x[segt[i].r+]-x[segt[i].l];
}
else if(segt[i].cov==)
{
segt[i].len1=x[segt[i].r+]-x[segt[i].l];
if(segt[i].l==segt[i].r)
segt[i].len2=;
else
segt[i].len2=max(segt[i<<].len1,segt[i<<].len2)+max(segt[i<<|].len1,segt[i<<|].len2);
}
else
{
if(segt[i].l==segt[i].r)
{
segt[i].len1=segt[i].len2=;
}
else
{
segt[i].len2=segt[i<<].len2+segt[i<<|].len2;
segt[i].len1=segt[i<<].len1+segt[i<<|].len1;
}
}
return ;
}
void update(int i,int l,int r,int value)
{
if(segt[i].l>=l && segt[i].r<=r)
{
segt[i].cov+=value;
pushup(i);
return ;
}
int mid=(segt[i].l+segt[i].r)>>;
if(mid>=r)
{
update(i<<,l,r,value);
}
else if(mid<l)
{
update(i<<|,l,r,value);
}
else
{
update(i<<,l,r,value);
update(i<<|,l,r,value);
}
pushup(i);
return ;
}
int main()
{
int T,n,m,k,u,v;
double x1,x2,y1,y2,ans,l,r;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
cnt=;
ans=;
for(int i=;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
edge[++cnt]=(edg){x1,x2,y1,};
x[cnt]=x1;
edge[++cnt]=(edg){x1,x2,y2,-};
x[cnt]=x2;
}
n<<=;
sort(x+,x+n+);
m=unique(x+,x+n+)-x-;
sort(edge+,edge+n+,cmp);
init(,,m);
for(int i=;i<n;i++)
if(edge[i].r>edge[i].l)
{
l=lower_bound(x+,x+m+,edge[i].l)-x;
r=lower_bound(x+,x+m+,edge[i].r)-x;
update(,l,r-,edge[i].d);
ans+=segt[].len2*(edge[i+].y-edge[i].y);
}
printf("%0.2lf\n",ans);
}
return ;
}

hdu 1542 [POJ 1151] 区间面积并

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12537    Accepted Submission(s): 5257

Problem Description
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.
 
Input
The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.

The input file is terminated by a line containing a single 0. Don’t process it.

 
Output
For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.

Output a blank line after each test case.

 
Sample Input
2
10 10 20 20
15 15 25 25.5
0
 
Sample Output
Test case #1
Total explored area: 180.00
 
Source
 

卡格式,不说了,都是泪。
 //#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#define clr(x) memset(x,0,sizeof(x))
#define MAXN 10010
using namespace std;
struct edg
{
double l,r,y;
int d;
}edge[MAXN];
struct seg
{
int l,r,cov;
double len;
}segt[MAXN<<];
int cnt;
double x[MAXN];
bool cmp(edg a,edg b)
{
if(a.y==b.y) return a.d>b.d;
return a.y<b.y;
}
double max(double a,double b)
{
return a>b?a:b;
}
void init(int i,int l,int r)
{
segt[i]=(seg){l,r,,};
if(l==r)
return ;
int mid=(l+r)>>;
init(i<<,l,mid);
init(i<<|,mid+,r);
return ;
}
void pushup(int i)
{
if(segt[i].cov)
{
segt[i].len=x[segt[i].r+]-x[segt[i].l];
}
else if(segt[i].l==segt[i].r)
{
segt[i].len=;
}
else
{
segt[i].len=segt[i<<].len+segt[i<<|].len;
}
return ;
}
void update(int i,int l,int r,int value)
{
if(segt[i].l>=l && segt[i].r<=r)
{
segt[i].cov+=value;
pushup(i);
return ;
}
int mid=(segt[i].l+segt[i].r)>>;
if(mid>=r)
{
update(i<<,l,r,value);
}
else if(mid<l)
{
update(i<<|,l,r,value);
}
else
{
update(i<<,l,r,value);
update(i<<|,l,r,value);
}
pushup(i);
return ;
}
int main()
{
int T,n,m,k,u,v;
double x1,x2,y1,y2,ans,l,r;
int kase=;
while(scanf("%d",&n) && n!=)
{
printf("Test case #%d\n",++kase);
cnt=;
ans=;
for(int i=;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
edge[++cnt]=(edg){x1,x2,y1,};
x[cnt]=x1;
edge[++cnt]=(edg){x1,x2,y2,-};
x[cnt]=x2;
}
n<<=;
sort(x+,x+n+);
m=unique(x+,x+n+)-x-;
sort(edge+,edge+n+,cmp);
init(,,m);
for(int i=;i<n;i++)
if(edge[i].r>edge[i].l)
{
l=lower_bound(x+,x+m+,edge[i].l)-x;
r=lower_bound(x+,x+m+,edge[i].r)-x;
update(,l,r-,edge[i].d);
ans+=segt[].len*(edge[i+].y-edge[i].y);
}
printf("Total explored area: %0.2lf\n",ans);
printf("\n");
}
return ;
}

扫描线三巨头 hdu1928&&hdu 1255 && hdu 1542 [POJ 1151]的更多相关文章

  1. hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  2. hdu 1542 & & poj 1151

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  3. HDU 1542/POJ 1151 Atlantis (scaning line + segment tree)

    A template of discretization + scaning line + segment tree. It's easy to understand, but a little di ...

  4. 线段树扫描线(一、Atlantis HDU - 1542(覆盖面积) 二、覆盖的面积 HDU - 1255(重叠两次的面积))

    扫描线求周长: hdu1828 Picture(线段树+扫描线+矩形周长) 参考链接:https://blog.csdn.net/konghhhhh/java/article/details/7823 ...

  5. HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)

    链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...

  6. HDU 1255 覆盖的面积(线段树+扫描线)

    题目地址:HDU 1255 这题跟面积并的方法非常像,仅仅只是须要再加一个变量. 刚開始我以为直接用那个变量即可,仅仅只是推断是否大于0改成推断是否大于1.可是后来发现了个问题,由于这个没有下放,没延 ...

  7. hdu 1255 覆盖的面积(线段树 面积 交) (待整理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.   In ...

  8. hdu 1255 覆盖的面积(求覆盖至少两次以上的面积)

    了校赛,还有什么途径可以申请加入ACM校队?  覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  9. “三巨头”有变化,BAT还能走多久?

    在腾讯市值超越阿里巴巴后,市场分析多数认为,当年的BAT“三巨头”时代已经彻底结束,进入了“双寡头”时代了 从对外投资来看,BAT不同的投资逻辑可以推测其战略方向 撰文/梁云风 时评员,关注财经与互联 ...

随机推荐

  1. John's trip(POJ1041+欧拉回路+打印路径)

    题目链接:http://poj.org/problem?id=1041 题目: 题意:给你n条街道,m个路口,每次输入以0 0结束,给你的u v t分别表示路口u和v由t这条街道连接,要输出从起点出发 ...

  2. [bzoj1002]轮状病毒-矩阵树定理

    Brief Description 求外圈有\(n\)个点的, 形态如图所示的无向图的生成树个数. Algorithm Design \[f(n) = (3*f(n-1)-f(n-2)+2)\] Co ...

  3. 大聊Python----协程

    协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来 ...

  4. 换行符 \r \n \r\n 在不同系统下的区别

    '\r'是回车,前者使光标到行首,(carriage return)'\n'是换行,后者使光标下移一格,(line feed)\r 是回车,return\n 是换行,newline对于换行这个动作,u ...

  5. windows下 nginx安装 使用

    介绍 Nginx (engine x) 是一个高性能的HTTP和反向代理服务器. 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络 ...

  6. gpio子系统和pinctrl子系统(下)

    情景分析 打算从两个角度来情景分析,先从bsp驱动工程师的角度,然后是驱动工程师的角度,下面以三星s3c6410 Pinctrl-samsung.c为例看看pinctrl输入参数的初始化过程(最开始的 ...

  7. 64_c2

    coin-or-Bcp-1.4.3-3.fc26.i686.rpm 22-May-2017 21:07 250866 coin-or-Bcp-1.4.3-3.fc26.x86_64.rpm 22-Ma ...

  8. C#利用WinRAR实现压缩和解压缩

    using System; using Microsoft.Win32; using System.Diagnostics; using System.IO; namespace MSCL { /// ...

  9. 【UOJ#169】元旦老人与数列

    论文题. 考虑到这题的维护和区间操作是反向的,也就是说无法像V那题快速的合并标记. 我们知道,一个区间的最小值和其他值是可以分开来维护的,因为如果一个区间被整体覆盖,那么最小值始终是最小值. 对于被覆 ...

  10. C语言比较巧妙的字符串分割程序

    在解析字符串时,能够解析的给出每个字符串的长度.内容.以及每个字符串的第一个字符的地址. short i; ; //切割之后的字符串的个数 ,ItemLen[],Idx[], ThCommandLen ...