POJ1389 Area of Simple Polygons 线段树
POJ1389 给定n个整数点矩形,求面积并。
显然ans必然是整数。
记录若干个事件,每个矩形的左边的竖边记为开始,右边的竖边记为结束。
进行坐标离散化后用线段树维护每个竖的区间, 就可以快速积分了。
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std; const int maxn=100005;
struct Rectangle
{
int x1,y1,x2,y2;
}; struct Event
{
int x,y1,y2,add;
}; struct IntervalTreeNode
{
int count,total;
}; int n; Rectangle rect[maxn];
Event evt[maxn<<1];
IntervalTreeNode tree[(maxn+10)<<2];
int id[maxn<<1];
bool cmp(const Event &a,const Event &b)
{
if(a.x<b.x)return true;
return false;
} void up(int i,int lb,int rb)
{
tree[i].total=tree[i<<1].total+tree[(i<<1)+1].total;
if(tree[i].count)tree[i].total=id[rb]-id[lb];
} void ins(int i,int lb,int rb,int a,int b,int k)
{
if(a==lb&&b==rb){
tree[i].count+=k;
up(i,lb,rb);
return ;
}
int med=(lb+rb)>>1;
if(b<=med) ins(i<<1,lb,med,a,b,k);
else if(a>=med)ins((i<<1)+1,med,rb,a,b,k);
else{
ins(i<<1,lb,med,a,med,k);
ins((i<<1)+1,med,rb,med,b,k);
}
up(i,lb,rb);
} long long area()
{
for(int i=0;i<n;i++){
id[i]=rect[i].y1;
id[i+n]=rect[i].y2;
}
sort(id,id+2*n);
for(int i=0;i<2*n;i++){
rect[i].y1=lower_bound(id,id+2*n,rect[i].y1)-id;
rect[i].y2=lower_bound(id,id+2*n,rect[i].y2)-id;//坐标离散化
}
for(int i=0;i<n;i++){
evt[i].add=1;
evt[i+n].add=-1;
evt[i].x=rect[i].x1;
evt[i+n].x=rect[i].x2;
evt[i].y1=evt[i+n].y1=rect[i].y1;
evt[i].y2=evt[i+n].y2=rect[i].y2;
}
sort(evt,evt+n*2,cmp);
long long int ans=0;
for(int i=0;i<2*n;i++){
if(i>0&&evt[i].x>evt[i-1].x){
ans+=(long long )(evt[i].x-evt[i-1].x)*tree[1].total;
}
ins(1,0,2*n-1,evt[i].y1,evt[i].y2,evt[i].add);
}
return ans;
} int main()
{freopen("t.txt","r",stdin);
while(true)
{
n=0;
scanf("%d%d%d%d",&rect[0].x1,&rect[0].y1,&rect[0].x2,&rect[0].y2);
if(rect[0].x1==-1)return -1;
while(++n)
{
scanf("%d%d%d%d",&rect[n].x1,&rect[n].y1,&rect[n].x2,&rect[n].y2);
if(rect[n].x1==-1)break;
}
printf("%I64d\n",area());
}
return 0;
}
POJ1389 Area of Simple Polygons 线段树的更多相关文章
- 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 扫描线+线段树面积并
---恢复内容开始--- LINK 题意:同POJ1151 思路: /** @Date : 2017-07-19 13:24:45 * @FileName: POJ 1389 线段树+扫描线+面积并 ...
- Area of Simple Polygons
poj1389:http://poj.org/problem?id=1389 题意:求矩形面积的并题解:扫描线加线段树 同poj1389 #include<iostream> #inclu ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树
E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树+计数排序
题目链接: http://codeforces.com/problemset/problem/558/E E. A Simple Task time limit per test5 secondsme ...
- CodeForces 588E A Simple Task(线段树)
This task is very simple. Given a string S of length n and q queries each query is on the format i j ...
- POJ Area of Simple Polygons 扫描线
这个题lba等神犇说可以不用离散化,但是我就是要用. 题干: Description There are N, <= N <= , rectangles -D xy-plane. The ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树 延时标记
E. A Simple Task time limit per test5 seconds memory limit per test512 megabytes inputstandard input ...
随机推荐
- POJ 1995 (快速幂) 求(A1B1+A2B2+ ... +AHBH)mod M
Description People are different. Some secretly read magazines full of interesting girls' pictures, ...
- Spring AOP配置简单记录(注解及xml配置方式)
在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...
- sed和awk的常用实例
一.文本间隔 1.在每一行后面增加一空行 sed G guo.sh awk '{printf("%s\n\n",$0 ) }' 2.将文件中原来的空行删掉,并在在每一行后边增加一空 ...
- 关于iphone 微信浏览器编码问题
这个问题最终没有完美的解决,给出的一个解决方法是返回一个html文档.
- noip模拟赛 Nephren Ruq Insania
题目背景 大样例下发链接: https://pan.baidu.com/s/1nuVpRS1 密码: sfxg 注意:本题大样例4的输出文件修改为 https://pan.baidu.com/s/1b ...
- jQuery的观察者模式详解 转载
jQuery的观察者模式详解 投稿:hebedich 本文主要是介绍了jQuery中on方法及trigger方法,以及围绕这个方法来体验的观察者模式,是篇非常不错的文章,对我们理解观察者模式很有帮助. ...
- js判断对象是否为空对象的几种方法
1.将json对象转化为json字符串,再判断该字符串是否为"{}" var data = {}; var b = (JSON.stringify(data) == "{ ...
- VIM使用技巧15
在vim的插入模式下,有时需要插入寄存器中的文本: 1.使用<C-r>{register} 2.使用<C-r><C-p>{register} 3.使用<C-r ...
- js数组转换成json串 (JSON.stringify)
例如: var giftlist[1490011777] = []; giftlist[1490011777]['id'] = 1490011777; giftlist[1490011777]['na ...
- [bzoj2091][Poi2010]The Minima Game_动态规划
The Minima Game bzoj-2091 Poi-2010 题目大意:给出N个正整数,AB两个人轮流取数,A先取.每次可以取任意多个数,直到N个数都被取走.每次获得的得分为取的数中的最小值, ...