Area of Simple Polygons
poj1389:http://poj.org/problem?id=1389
题意:求矩形面积的并
题解:扫描线加线段树 同poj1389
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
int num;
struct Node{
int l;
int r;
int tp;
int y;
bool operator <(Node a) const{
return y<a.y;
}
}line[*maxn];
int arr[*maxn];
struct Edge{
int left;
int right;
int flag;
int sum;
}node1[maxn*];
void build(int l,int r,int idx){
node1[idx].left=l;
node1[idx].right=r;
if(l==r){
node1[idx].flag=;
node1[idx].sum=;
return;
}
int mid=(l+r)/;
build(l,mid,idx<<);
build(mid+,r,idx<<|);
node1[idx].sum=node1[idx<<].sum+node1[idx<<|].sum;
}
void update(int l,int r,int f,int idx){
if(node1[idx].left==node1[idx].right){
node1[idx].flag+=f;
if(node1[idx].flag)node1[idx].sum=arr[node1[idx].right+]-arr[node1[idx].left];
if(!node1[idx].flag)node1[idx].sum=;
return ;
}
int mid=(node1[idx].left+node1[idx].right)/;
if(mid>=r)update(l,r,f,idx<<);
else if(mid<l)update(l,r,f,idx<<|);
else{
update(l,mid,f,idx<<);
update(mid+,r,f,idx<<|);
}
node1[idx].sum=node1[idx<<].sum+node1[idx<<|].sum;
}
int binaryserach(int x){
int l,r,mid;
l=,r=num+;
while (r-l>){
mid=(l+r)>>;
if (arr[mid]<=x) l=mid;
else r=mid;
}
return l;
}
int main(){
int n;int x1,y1,x2,y2;
long long ans;
while(~scanf("%d%d%d%d",&x1,&y1,&x2,&y2)){
if(x1==-&&x2==-&&y1==-&&y2==-)break;
num=;int cnt=;
line[].l=x1;line[].r=x2;line[].y=y1;line[].tp=;
line[].l=x1;line[].r=x2;line[].y=y2;line[].tp=-;
arr[++num]=x1;arr[++num]=x2;
while(~scanf("%d%d%d%d",&x1,&y1,&x2,&y2)){
if(x1==-&&x2==-&&y1==-&&y2==-)break; line[++cnt].l=x1;line[cnt].r=x2;line[cnt].y=y1;line[cnt].tp=;
line[++cnt].l=x1;line[cnt].r=x2;line[cnt].y=y2;line[cnt].tp=-;
arr[++num]=x1;arr[++num]=x2;
} sort(arr+,arr+num+);
ans=;
sort(line+,line+cnt+);
build(,cnt,);
for(int i=;i<=cnt;i++){
ans+=node1[].sum*(line[i].y-line[i-].y);
int l=binaryserach(line[i].l);
int r=binaryserach(line[i].r)-;
update(l,r,line[i].tp,);
} printf("%I64d\n",ans);
}
}
Area of Simple Polygons的更多相关文章
- POJ Area of Simple Polygons 扫描线
这个题lba等神犇说可以不用离散化,但是我就是要用. 题干: Description There are N, <= N <= , rectangles -D xy-plane. The ...
- POJ1389 Area of Simple Polygons 线段树
POJ1389 给定n个整数点矩形,求面积并. 显然ans必然是整数. 记录若干个事件,每个矩形的左边的竖边记为开始,右边的竖边记为结束. 进行坐标离散化后用线段树维护每个竖的区间, 就可以快速积分了 ...
- 【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 扫描线+线段树面积并
---恢复内容开始--- LINK 题意:同POJ1151 思路: /** @Date : 2017-07-19 13:24:45 * @FileName: POJ 1389 线段树+扫描线+面积并 ...
- POJ1389:Area of Simple Polygons——扫描线线段树题解+全套代码注释
http://poj.org/problem?id=1389 题面描述在二维xy平面中有N,1 <= N <= 1,000个矩形.矩形的四边是水平或垂直线段.矩形由左下角和右上角的点定义. ...
- [poj] 1389 Area of Simple Polygons
原题 线段树+扫描线 对于这样一个不规则图形,我们要求他的面积有两种方法,割和补. 补显然不行,因为补完你需要求补上去的内部分不规则图形面积-- 那么怎么割呢? 像这样: 我们就转化成了无数个矩形的和 ...
- POJ 1389 Area of Simple Polygons | 扫描线
请戳此处 #include<cstdio> #include<algorithm> #include<cstring> #define N 1010 #define ...
- [转] POJ计算几何
转自:http://blog.csdn.net/tyger/article/details/4480029 计算几何题的特点与做题要领:1.大部分不会很难,少部分题目思路很巧妙2.做计算几何题目,模板 ...
- [POJ 1151] Atlantis
一样的题:HDU 1542 Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18148 Accepted ...
随机推荐
- JAVA Web学习篇--Servlet
Servlet由来 做过BS项目的人都知道,浏览器可以依据HTML静态标记语言来显示各式各样的网页.可是假设我们须要在网页上完毕一些业务逻辑:比方登陆验证.或者说网页显示的内容在server的数据库中 ...
- Delphi 记事本 TMemo
Windows记事本记事本 描述: 用Delphi模仿的Windows记事本 界面和功能都和Windows的记事本一样,是用Memo实现的而不是RichEdit 可以执行以下功能 文件 ...
- hive运行query语句时提示错误:org.apache.hadoop.ipc.RemoteException: java.io.IOException: java.io.IOException:
hive> select product_id, track_time from trackinfo limit 5; Total MapReduce jobs = 1 Launching Jo ...
- JAVA基于AE调用GP实现泰森多边形
调用GP实现数据处理是较快捷.较易入手的方法. 使用JAVA语言基于AE调用GP实现泰森多边形的代码例如以下: public void CreatVoronoi(){ try { GeoProcess ...
- StructureMap Exception Code: 207 Internal exception while creating Instance '06fc8bd7-76db-47c1-8d71-31090a074f5e' of PluginType QIMS.Repository.IComStaffRepository. Check the inner exception for more
标题翻译: StructureMap异常代码:207内部异常,同时创造PluginType QIMS.Repository.IComStaffRepository的实例“06fc8bd7-76db-4 ...
- 关于ajaxfileupload.js一些问题和上传图片就立即显示图片功能
ajaxfileupload.js是上传文件的一个插件,最近碰到的一个问题是在谷歌浏览器上传文件之后,原文本框中的文件名称消失,网上搜了好长时间也没有十分满意的答案.无刷新上传文件我想到的只有ajax ...
- node基础再现--module.exports 和exports
实际上,最最基础的方法,最最原始的方法是module.exports,至于exports,是为了方便书写才出来的,应该说,module.exports 包含exports,所工作的范围更加的广泛! m ...
- 点击其它地方隐藏div/事件冒泡/sweet-alert阻止冒泡
点击document时把div隐藏,但点击div时阻止点击事件冒泡到document,从而实现“点击文档其它地方隐藏div,点击div本身不隐藏”.js代码如下:$("#div") ...
- 使用java注解的例子有没有
使用java注解的例子 参考文档:http://www.cnblogs.com/pepcod/archive/2013/02/20/2918719.html http://www.shaoqun.co ...
- shell脚本学习之if..else用法
一 简介 1 字符串判断 str1 = str2 当两个串有相同内容.长度时为真 str1 != str2 当串str1和str2不等时为真 -n str1 当串的长度大于0时为真(串非空) - ...