POJ Area of Simple Polygons 扫描线
这个题lba等神犇说可以不用离散化,但是我就是要用。
题干:
Description There are N, <= N <= , rectangles in the -D xy-plane. The four sides of a rectangle are horizontal or vertical line segments. Rectangles are defined by their lower-left and upper-right corner points. Each corner point is a pair of two nonnegative integers in the range of through , indicating its x and y coordinates. Assume that the contour of their union is defi ned by a set S of segments. We can use a subset of S to construct simple polygon(s). Please report the total area of the polygon(s) constructed by the subset of S. The area should be as large as possible. In a -D xy-plane, a polygon is defined by a finite set of segments such that every segment extreme (or endpoint) is shared by exactly two edges and no subsets of edges has the same property. The segments are edges and their extremes are the vertices of the polygon. A polygon is simple if there is no pair of nonconsecutive edges sharing a point. Example: Consider the following three rectangles: rectangle : < (, ) (, ) >, rectangle : < (, ) (, ) >, rectangle : < (, ) (, ) >. The total area of all simple polygons constructed by these rectangles is .
Input The input consists of multiple test cases. A line of -'s separates each test case. An extra line of 4 -1's marks the end of the input. In each test case, the rectangles are given one by one in a line. In each line for a rectangle, non-negative integers are given. The first two are the x and y coordinates of the lower-left corner. The next two are the x and y coordinates of the upper-right corner.
Output
For each test case, output the total area of all simple polygons in a line.
Sample Input - - - - - - - -
- - - -
Sample Output
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = << ;
const int mod = ;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
{x = x * + c - '';if(x > mod) x %= mod;}
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
struct node
{
int s,e,h,f;
}p[];
int n,m,ans;
int tree[],x[];
int c[];
void init()
{
n = m = ans = ;
clean(tree);
clean(p);
clean(c);
x[] = -;
}
bool cmp(node a,node b)
{
return a.h < b.h;
} void push_up(int o,int l,int r)
{
// printf("@%d %d\n",l,r);
if(c[o] != ) tree[o] = x[r+] - x[l];
else if(l == r) tree[o] = ;
else tree[o] = tree[o << ] + tree[o << | ];
} void update(int o,int l,int r,int x,int y,int w)
{
if(l == x && r == y) c[o] += w;
else
{
int mid = (l + r) >> ;
if(y <= mid) update(o << ,l,mid,x,y,w);
else if(x > mid) update(o << | ,mid + ,r,x,y,w);
else update(o << ,l,mid,x,mid,w),update(o << | ,mid + ,r,mid + ,y,w);
}
push_up(o,l,r);
}
int main()
{
int a,b,c,d;
init();
while(scanf("%d%d%d%d",&a,&b,&c,&d) != EOF)
{
if(a < )
break;
while(a >= )
{
p[++n].s = a;p[n].e = c;p[n].h = b;p[n].f = ;
x[n] = a;
p[++n].s = a;p[n].e = c;p[n].h = d;p[n].f = -;
x[n] = c;
read(a);read(b);read(c);read(d);
}
sort(x + ,x + n + );
m = unique(x + ,x + n + ) - x - ;
sort(p + ,p + n + ,cmp);
duke(i,,n-)
{
int l = lower_bound(x,x + m,p[i].s) - x;
int r = lower_bound(x,x + m,p[i].e) - x - ;
update(,,m,l,r,p[i].f);
ans += tree[] * (p[i + ].h - p[i].h);
}
printf("%d\n",ans);
init();
}
return ;
}
POJ Area of Simple Polygons 扫描线的更多相关文章
- POJ 1389 Area of Simple Polygons 扫描线+线段树面积并
---恢复内容开始--- LINK 题意:同POJ1151 思路: /** @Date : 2017-07-19 13:24:45 * @FileName: POJ 1389 线段树+扫描线+面积并 ...
- POJ 1389 Area of Simple Polygons | 扫描线
请戳此处 #include<cstdio> #include<algorithm> #include<cstring> #define N 1010 #define ...
- POJ1389:Area of Simple Polygons——扫描线线段树题解+全套代码注释
http://poj.org/problem?id=1389 题面描述在二维xy平面中有N,1 <= N <= 1,000个矩形.矩形的四边是水平或垂直线段.矩形由左下角和右上角的点定义. ...
- 【POJ 1389】Area of Simple Polygons(线段树+扫描线,矩形并面积)
离散化后,[1,10]=[1,3]+[6,10]就丢了[4,5]这一段了. 因为更新[3,6]时,它只更新到[3,3],[6,6]. 要么在相差大于1的两点间加入一个值,要么就让左右端点为l,r的线段 ...
- [poj] 1389 Area of Simple Polygons
原题 线段树+扫描线 对于这样一个不规则图形,我们要求他的面积有两种方法,割和补. 补显然不行,因为补完你需要求补上去的内部分不规则图形面积-- 那么怎么割呢? 像这样: 我们就转化成了无数个矩形的和 ...
- Area of Simple Polygons
poj1389:http://poj.org/problem?id=1389 题意:求矩形面积的并题解:扫描线加线段树 同poj1389 #include<iostream> #inclu ...
- POJ1389 Area of Simple Polygons 线段树
POJ1389 给定n个整数点矩形,求面积并. 显然ans必然是整数. 记录若干个事件,每个矩形的左边的竖边记为开始,右边的竖边记为结束. 进行坐标离散化后用线段树维护每个竖的区间, 就可以快速积分了 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)
POJ 3468.A Simple Problem with Integers 这个题就是成段的增减以及区间查询求和操作. 代码: #include<iostream> #include& ...
随机推荐
- 17Oracle Database 维护
Oracle Database 维护 备份 还原
- 手机通过Charles用线上域名访问PC本地项目
最近调试微信公众号,由于微信授权需要线上正式环境的域名,笔者想把手机公众号网页重定向到PC本地localhost调试. 该方法通过Charles代理转发,适用所有需要域名重定向的场景,例如手机通过在线 ...
- ubutun 创建左面快捷方式
#http://blog.csdn.net/jizi7618937/article/details/51012552
- LINUX-YUM 软件包升级器 - (Fedora, RedHat及类似系统)
yum install package_name 下载并安装一个rpm包 yum localinstall package_name.rpm 将安装一个rpm包,使用你自己的软件仓库为你解决所有依赖关 ...
- Linux命令介绍
资料链接:(Linux基本命令介绍)http://note.youdao.com/share/?id=36c07917f8d3e6437c1e764c3516a3f2&type=note#/ ...
- Python基础之生成器、迭代器
一.字符串格式化进阶 Python的字符串格式化有两种方式: 百分号方式.format方式,由于百分号的方式相对来说比较老,在社区里讨论format方式有望取代百分号方式,下面我们分别介绍一下这两种方 ...
- Educational Codeforces Round 60 (Rated for Div. 2) 即Codeforces Round 1117 C题 Magic Ship
time limit per test 2 second memory limit per test 256 megabytes input standard inputoutput standard ...
- 这个函数有搞头,要调试通过就差不多啦--ImpersonateActiveUserAndRun
//Function to run a process as active user from windows service void ImpersonateActiveUserAndRun() { ...
- HDU——4738 Caocao's Bridges
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Spring Boot中使用Swagger2生成RESTful API文档(转)
效果如下图所示: 添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!-- https://mvnrepository.com/artifact/io.springfox ...