这个题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. 使用SELECT语句检索数据

    使用SELECT语句检索数据select指令适用于SQL数据库SELECT 语句用于从数据库中选取数据.(指令不分大小写,选择的值除名字和一些有特殊意义的字符可不分大小写,from结束时一定要加;) ...

  2. C++ CEF 浏览器中显示 Tooltip(标签中的 title 属性)

    在 Windows 中将 CEF 集成到 C++ 客户端以后,默认是无法显示 tooltip 的,比如图片标签中的 title 属性. 实现的方式其实很简单,按下面的步骤操作就可以: 创建一个文本文件 ...

  3. The C Programming Language-4.1

    下面是c程序设计语言4.1代码以及我的一些理解 strindex函数,通过嵌套两次循环,在s[ ]和t[ ]两个数组对映元素相等且t[ ]尚未遍历完毕的情况下,不断循环,最终返回正数或-1 代码如下 ...

  4. LINUX-字符设置和文件格式转换

    dos2unix filedos.txt fileunix.txt 将一个文本文件的格式从MSDOS转换成UNIX unix2dos fileunix.txt filedos.txt 将一个文本文件的 ...

  5. loadrunner12 + ie11 无internet, 代码中文乱码

    第一次用lr    录制的时候显示无internet, 在网上找了好久答案, 无非是ie路径设置,还有证书......   都试过了不好用,自己研究一下午 , 最后发现是协议没对应上,http协议  ...

  6. 洛谷 1569 [USACO11FEB]属牛的抗议

    [题解] 非常显然的DP,f[i]表示到第i个位置最多分成几组,f[i]=Max(f[i],f[j]+1) (j<i,sum[j]<=sum[i]) #include<cstdio& ...

  7. 2.6 访问 Shell 脚本的参数

        所谓的位置参数(positional parameters)指的也就是Shell脚本的命令行参数(command-line arguments).在Shell函数里,它们同时也可以是函数的参数 ...

  8. javaHttp请求,接收到的是中文乱码如何处理

    可在service()方法中加日志,看哪种不是乱码 例如,中文乱码的话,中文编码一般有 UTF-8,GBK,ISO-8859-1 加日志为 List<String> list = new ...

  9. 【Codeforces 464A】No to Palindromes!

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 因为原序列没有任何长度超过2的回文串. 所以,我们在改变的时候,只要时刻保证改变位置s[i]和s[i-1]以及s[i-2]都不相同就好. 因为 ...

  10. 【Codeforces 979B】Treasure Hunt

    [链接] 我是链接,点我呀:) [题意] 每次你可以将一个字符变成一个不同于本身的字符. 每个人需要改变n次(且不能不改变) 设每个人的字符串中出现次数最多的字符出现的次数为cnt[0~2] 问你谁的 ...