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& ...
随机推荐
- Linux下查看CPU信息、机器型号等硬件信息命令
Linux下查看CPU信息.机器型号等硬件信息命令 编写一个bash脚本: vim info.sh #!/bin/bash cat /etc/issue echo "____________ ...
- react native 从头开始
1.react-native run-android 报错SDK location not found. Define location with sdk.dir in the local.prope ...
- Linux kernel memory-faq.txt
## Linux kernel memory-faq.txt What is some existing documentation on Linux memory management? Ulric ...
- Linux之 sed用法
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为: sed ...
- subprocess操作命令
import subprocess 一. run()方法 --->括号里面传参数,主要有cmd, stdout, shell, encoding, check 1.直接传命令 2.命令带参数要以 ...
- 通过response对象的sendRedirect方法重定向网页
通过response对象的sendRedirect方法重定向网页 制作人:全心全意 使用response对象提供的sendRedirect()方法可以将网页重定向到另一个页面.重定向操作支持将地址重定 ...
- 第十五节:Web爬虫之selenium动态渲染爬取
selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firef ...
- Codeforces 938C - Constructing Tests
传送门:http://codeforces.com/contest/938/problem/C 给定两个正整数n,m(m≤n),对于一个n阶0-1方阵,其任意m阶子方阵中至少有一个元素“0”,则可以求 ...
- SqlServer2008必须开启哪些服务
SQL Server 2008 大概有下面这些服务 SQL Active Directory Helper 服务支持与 Active Directory 的集成SQL Full-text Filter ...
- 设置NODE_ENV=test环境变量
之前开发时因为有内网测试环境和外网测试环境,再部署打包时总是切换两个域名,比较麻烦,所以最好能设置一个环境变量,来控制两个域名,于是做了如下配置: "scripts": { &qu ...