这个题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 扫描线的更多相关文章

  1. POJ 1389 Area of Simple Polygons 扫描线+线段树面积并

    ---恢复内容开始--- LINK 题意:同POJ1151 思路: /** @Date : 2017-07-19 13:24:45 * @FileName: POJ 1389 线段树+扫描线+面积并 ...

  2. POJ 1389 Area of Simple Polygons | 扫描线

    请戳此处 #include<cstdio> #include<algorithm> #include<cstring> #define N 1010 #define ...

  3. POJ1389:Area of Simple Polygons——扫描线线段树题解+全套代码注释

    http://poj.org/problem?id=1389 题面描述在二维xy平面中有N,1 <= N <= 1,000个矩形.矩形的四边是水平或垂直线段.矩形由左下角和右上角的点定义. ...

  4. 【POJ 1389】Area of Simple Polygons(线段树+扫描线,矩形并面积)

    离散化后,[1,10]=[1,3]+[6,10]就丢了[4,5]这一段了. 因为更新[3,6]时,它只更新到[3,3],[6,6]. 要么在相差大于1的两点间加入一个值,要么就让左右端点为l,r的线段 ...

  5. [poj] 1389 Area of Simple Polygons

    原题 线段树+扫描线 对于这样一个不规则图形,我们要求他的面积有两种方法,割和补. 补显然不行,因为补完你需要求补上去的内部分不规则图形面积-- 那么怎么割呢? 像这样: 我们就转化成了无数个矩形的和 ...

  6. Area of Simple Polygons

    poj1389:http://poj.org/problem?id=1389 题意:求矩形面积的并题解:扫描线加线段树 同poj1389 #include<iostream> #inclu ...

  7. POJ1389 Area of Simple Polygons 线段树

    POJ1389 给定n个整数点矩形,求面积并. 显然ans必然是整数. 记录若干个事件,每个矩形的左边的竖边记为开始,右边的竖边记为结束. 进行坐标离散化后用线段树维护每个竖的区间, 就可以快速积分了 ...

  8. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  9. POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)

    POJ 3468.A Simple Problem with Integers 这个题就是成段的增减以及区间查询求和操作. 代码: #include<iostream> #include& ...

随机推荐

  1. CAD得到所有实体1

    主要用到函数说明: IMxDrawSelectionSet::AllSelect 得到当前空间的所有实体.详细说明如下: 参数 说明 [in,defaultvalue(NULL)] IMxDrawRe ...

  2. c++通过CMake实现debug开关

    刚学cmake,很多东西还不是很懂,不过今天刚刚实现了通过CMake控制debug的开关,兴奋之余记录一下. 背景介绍: 最近参与到了一个大的C++项目,很多代码已经非常成熟,我来添加一些辅助功能,但 ...

  3. C++11 Thread多线程的学习心得与问题

    C++11 ,封装了thread的多线程的类,这样对多线程的使用更加方便. 多线程的原理我不加赘述,可以参看操作系统等参考书. 多线程代码可以最大化利用计算机性能资源,提高代码的运行效率,是常用优化方 ...

  4. TestNG分组测试

    分为方法的分组和类的分组: GroupsOnMethod类: package com.janson.groups; import org.testng.annotations.Test; public ...

  5. 05 Python运算符

    Python运算符: 此图来源于菜鸟教程,更详细参考 http://www.runoob.com/python3/python3-basic-operators.html 说明: 同一优先级的通常从左 ...

  6. 恶补---bell数

    定义 bell数即一个集合划分的数目 示例 前几项的bell数列为 1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975 ,... 求值方法 1.bell ...

  7. [转] 探讨JS合并两个数组的方法

    我们在项目过程中,有时候会遇到需要将两个数组合并成为一个的情况. 比如: 1 2 var a = [1,2,3]; var b = [4,5,6]; 有两个数组a.b,需求是将两个数组合并成一个.方法 ...

  8. Leetcode 90.子集

    子集 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1], [1,2,2], ...

  9. 学习MongoDB--(5-2):索引(查看索引的使用,管理索引)

    前一篇简单介绍了索引,并给出了基本的索引使用,这一次,我们进一步说一下MongoDB中的索引,包括如何查看查询是否走索引,如何管理索引和地理空间索引等. [使用explain和hint] 前面讲高级查 ...

  10. noip模拟赛 水题

    题目描述 LYK出了道水题. 这个水题是这样的:有两副牌,每副牌都有n张. 对于第一副牌的每张牌长和宽分别是xi和yi.对于第二副牌的每张牌长和宽分别是aj和bj.第一副牌的第i张牌能覆盖第二副牌的第 ...