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 ...
随机推荐
- HTML5地理定位-Geolocation API
HTML5提供了一组Geolocation API,来自navigator定位对象的子对象,获取用户的地理位置信息Geolocation API使用方法:1.判断是否支持 navigator.geol ...
- Python实现图片切割
import os from PIL import Image def splitimage(src, rownum, colnum, dstpath): img = Image.open(src) ...
- 【03】emmet系列之CSS语法
[01]emmet系列之基础介绍 [02]emmet系列之HTML语法 [03]emmet系列之CSS语法 [04]emmet系列之编辑器 [05]emmet系列之各种缩写 单位: 有几个常用值别 ...
- STM32F407 按键输入实验 库函数版 个人笔记
读取IO口输入的函数: STM32F4 的 IO口做输入使用的时候,是通过调用函数 GPIO_ReadInputDataBit()来读取 IO 口的状态的.了解了这点,就可以开始我们的代码编写了. 按 ...
- BNUOJ 5629 胜利大逃亡(续)
胜利大逃亡(续) Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 1 ...
- Happy Three Friends
Happy Three Friends Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- hdu 4771好题
#include<stdio.h> #include<string.h>//通过只记录每一步此时点的状态.题非常好 #include<queue> using na ...
- node框架express里面静态文件中间件express.static,根据路径名查找文件
- 是express框架下的一个方法,可以根据请求路径名查找某个文件下文件名字和路径名相同的文件 - 3.X里面有20多个中间件,但是在4.X里面 只保留了express.static - 语法 ex ...
- apple网址
https://developer.apple.com/downloads/index.action# 开发工具下载
- Count Color POJ - 2777 线段树
Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds ...