POJ Area of Simple Polygons 扫描线

这个题lba等神犇说可以不用离散化,但是我就是要用。

题干:

DescriptionThere are N, 1 <= N <= 1,000 rectangles in the 2-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 0 through 50,000 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 2-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 1: < (0, 0) (4, 4) >, rectangle 2: < (1, 1) (5, 2) >, rectangle 3: < (1, 1) (2, 5) >. The total area of all simple polygons constructed by these rectangles is 18. 
InputThe input consists of multiple test cases. A line of 4 -1'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, 4 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
0 0 4 4
1 1 5 2
1 1 2 5
-1 -1 -1 -1
0 0 2 2
1 1 3 3
2 2 4 4
-1 -1 -1 -1
-1 -1 -1 -1  
Sample Output
18
10 

代码:

#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 = 1 << 30;
const int mod = 998244353;
typedef double db;
template <class T>
void read(T &x)
{char c;bool op = 0;while(c = getchar(), c < '0' || c > '9')if(c == '-') op = 1;x = c - '0';while(c = getchar(), c >= '0' && c <= '9'){x = x * 10 + c - '0';if(x > mod) x %= mod;}if(op) x = -x;
}
template <class T>
void write(T x)
{if(x < 0) putchar('-'), x = -x;if(x >= 10) write(x / 10);putchar('0' + x % 10);
}
struct node
{int s,e,h,f;
}p[100000];
int n,m,ans;
int tree[500000],x[500000];
int c[500000];
void init()
{n = m = ans = 0;clean(tree);clean(p);clean(c);x[0] = -1;
}
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] != 0) tree[o] = x[r+1] - x[l];else if(l == r) tree[o] = 0;else tree[o] = tree[o << 1] + tree[o << 1 | 1];
}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) >> 1;if(y <= mid) update(o << 1,l,mid,x,y,w);else if(x > mid) update(o << 1 | 1,mid + 1,r,x,y,w);else update(o << 1,l,mid,x,mid,w),update(o << 1 | 1,mid + 1,r,mid + 1,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 < 0)break;while(a >= 0){p[++n].s = a;p[n].e = c;p[n].h = b;p[n].f = 1;x[n] = a;p[++n].s = a;p[n].e = c;p[n].h = d;p[n].f = -1;x[n] = c;read(a);read(b);read(c);read(d);}sort(x + 1,x + n + 1);m = unique(x + 1,x + n + 1) - x - 1;sort(p + 1,p + n + 1,cmp);duke(i,1,n-1){int l = lower_bound(x,x + m,p[i].s) - x;int r = lower_bound(x,x + m,p[i].e) - x - 1;update(1,1,m,l,r,p[i].f);ans += tree[1] * (p[i + 1].h - p[i].h);}printf("%d\n",ans);init();}return 0;
}

 

转载于:https://www.cnblogs.com/DukeLv/p/9799249.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/570301.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

window.print()打印时,如何自定义页眉/页脚、页边距

自定义页眉/页脚、页边距&#xff0c;要用到ActiveX控件&#xff08;在ie的安全设置的启用&#xff09;&#xff0c;会修改注册表中ie的设置&#xff0c;代码如下。 try{ var hkey_root,hkey_path,hkey_key; hkey_root"HKEY_CURRENT_USER"; hkey_path"\\Soft…

java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String

这个问题是&#xff0c;类型转换出错&#xff0c;为什么呢&#xff1f; 因为BigDecimal不能强制转换成 String类型&#xff0c;要用toString()转换。

String转换为int类型

在java中使用 Integer.parseInt 在js中使用 parseInt

Luogu P4205 [NOI2005]智慧珠游戏

P4205 [NOI2005]智慧珠游戏 题意 题目描述 智慧珠游戏拼盘由一个三角形盘件和\(12\)个形态各异的零件组成。拼盘的盘 件如图\(1\)所示 对于由珠子构成的零件&#xff0c;可以放到盘件的任一位置&#xff0c;条件是能有地方放&#xff0c;且尺寸合适&#xff0c;所有的零件都允许…

js中给多个class属性的标签赋值

根据ID给标签赋value值&#xff0c; document.getElementById("id").value"张三"; 当有多个class属性时&#xff0c; for(var i0;i<10;i){document.getElementsByClassName("name")[i].value"李四"; }

Cannot set property 'value' of undefined

一般情况都是js报错引起的&#xff0c;根据实际总结到&#xff1a; 1、js页面初始化时&#xff0c;执行$(document).ready(function(){})方法&#xff0c; 当你要加载的页面内容很多时&#xff0c;你的页面还未加载完&#xff0c;执行初始化函数报错。 2、在初始化函数中&…

Spring Boot + Spring Cloud 构建微服务系统(三):服务消费和负载(Feign)

Spring Cloud Feign Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端。它使得编写Web服务客户端变得更加简单。我们只需要通过创建接口并用注解来配置它既可完成对Web服务接口的绑定。它具备可插拔的注解支持&#xff0c;包括Feign注解、JAX-RS注解。它也…

Scrapy突破反爬虫的限制

7-1 爬虫和反爬的对抗过程以及策略基本概念爬虫&#xff1a;自动获取网站数据的程序&#xff0c;关键是批量的获取反爬虫&#xff1a;使用技术手段防止爬虫程序的方法误伤&#xff1a;反爬技术将普通用户识别为爬虫&#xff0c;如果误伤过高&#xff0c;效果再好也不能用一般ip…

Oracle中“不等于”的使用

在oracle中判断字段id不是“123”时&#xff0c; select * from user where id<> 123; 但是id为空的&#xff0c;却怎么也查询不出来。 这是why&#xff1f;原因是&#xff1a;字段为null的时候&#xff0c;只能通过is null或者is not null来判断。 这样写才是正确的…

wpf控件

控件——载应用程序上与用户进行交互的元素 所有的控件都是继承自System.windows.Control类&#xff0c;该类提供了一些基本的属性 1、 设置控件对齐方式 2、 设置Tab键顺序 3、 支持绘制背景&#xff0c;前景和边框 4、 支持格式化文本内容的尺寸和字体 Background&#xff1a…

The import javax.servlet cannot be resolved

错误的原因是&#xff1a;缺少servlet-api.jar这个包&#xff0c;将这个包导入项目里面就可以了。1. 我们可以到tomcat的lib目录下面找到这个包&#xff0c;然后在eclipse中&#xff0c;右击项目&#xff0c;2. 选择Java Build Path>Libraries>Add ExternalJARS,找到你计…

mybatis中修改了数据,控制台显示成功,数据库没有修改

在mybatis中遇到了修改数据时&#xff0c;控制台显示修改成功&#xff0c;但是去数据库查看并没有修改&#xff0c;这是因为mybatis不时自动提交事务的&#xff0c;所以是不会修改数据库的数据&#xff0c;这是我们加上一句 sqlSession.commit()就可以了。转载于:https://www.c…

The type Resource is not accessible due to restriction on required library

项目属性preferences>java build path>把右侧【libraries中的JRE System Library】删除重新导入.

BZOJ1500 [NOI2005]维修数列(Splay tree)

[Submit][Status][Discuss]Description 请写一个程序&#xff0c;要求维护一个数列&#xff0c;支持以下 6 种操作&#xff1a;请注意&#xff0c;格式栏 中的下划线‘ _ ’表示实际输入文件中的空格Input 输入的第1 行包含两个数N 和M(M ≤20 000)&#xff0c;N 表示初始时数列…

日期格式转换:String 与 date

在java中转换 //String—>Date String time “2018-01-09”&#xff1b; try{SimpleDateFormat sdf new SimpleDateFormat("yyyy-MM-dd");Date date sdf.parse(time); }catch(ParseException e){System.out.println(e.getMessage()); } //Date—>String S…

在java中对null的理解

转载&#xff1a;https://www.cnblogs.com/X-World/p/5686122.html Java中的Null是什么&#xff1f; 1&#xff09;首先&#xff0c;null是关键字&#xff0c;像public、static、final。它是大小写敏感的&#xff0c;你不能将null写成Null或NULL&#xff0c;编译器将不能识别…

obs video-io.c

video_frame_init 讲解 /* messy code alarm video_frame_init 函数用于初始化视频帧。它接受一个指向 struct video_frame 结构体的指针 frame&#xff0c; 视频格式 format&#xff0c;以及宽度 width 和高度 height。该函数根据视频格式的不同&#xff0c;计算出每个视频帧…

如何解决SVN 清理失败

解决方法&#xff1a; 下载 sqlite3.exe 在你的清理失败的路径下查看.svn目录下是否存在一个wc.db文件&#xff0c;把解压好的sqlite3.exe 放在wc.db文件的同一路径下 注意&#xff1a;主要是用sqlite3.exe清理掉wc.db中的相关信息。 通过cmd命令行进入你清理失败的路径&am…

10-Linux与windows文件互传-pscp坑---- 'pscp' 不是内部或外部命令,也不是可运行的程序或批处理文件...

1.下载pscp工具http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html2.拷贝到C:\Windows\System32 如果考到其他文件夹&#xff0c;运行提示 pscp 不是内部或外部命令&#xff0c;也不是可运行的程序或批处理文件。 那么考到这个文件下吧&#xff01;&#xff0…

MongoDB最简单的入门教程之三 使用Java代码往MongoDB里插入数据

前两篇教程我们介绍了如何搭建MongoDB的本地环境&#xff1a; MongoDB最简单的入门教程之一 环境搭建 以及如何用nodejs读取MongoDB里的记录&#xff1a; MongoDB最简单的入门教程之二 使用nodejs访问MongoDB 这篇教程我们会介绍如何使用Java代码来连接MongoDB。 如果您是基于M…