【HDOJ1828&&POJ1177】Picture(线段树,扫描线)
题意:给定n个矩形,求他们的并的周长
n<=5e3,abs(x[i])<=1e4
思路:From https://www.cnblogs.com/kuangbin/archive/2013/04/10/3013437.html
真实“线段”树
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
#define N 11000
#define M 7010
#define eps 1e-8
#define pi acos(-1)
#define oo 1e9
#define MOD 10007 struct node
{
int l,r,cnt,num,c;
bool lc,rc;
}t[N<<]; struct Line
{
int x1,x2,y,f;
}line[N]; bool cmp(Line a,Line b)
{
return a.y<b.y;
} int x[N]; void build(int l,int r,int p)
{
t[p].l=x[l];
t[p].r=x[r];
t[p].cnt=t[p].num=t[p].c=;
t[p].lc=t[p].rc=false;
if(l+==r) return;
int mid=(l+r)>>;
build(l,mid,p<<);
build(mid,r,p<<|);
} void calc(int p)
{
if(t[p].c>)
{
t[p].cnt=t[p].r-t[p].l;
t[p].num=;
t[p].lc=t[p].rc=true;
return;
}
if(t[p].l+==t[p].r)
{
t[p].cnt=t[p].num=;
t[p].lc=t[p].rc=false;
}
else
{
t[p].cnt=t[p<<].cnt+t[p<<|].cnt;
t[p].lc=t[p<<].lc;
t[p].rc=t[p<<|].rc;
t[p].num=t[p<<].num+t[p<<|].num;
if(t[p<<].rc&&t[p<<|].lc) t[p].num--;
}
} void update(int l,int r,Line e,int p)
{
if(t[p].l==e.x1&&t[p].r==e.x2)
{
t[p].c+=e.f;
calc(p);
return;
}
int mid=(l+r)>>;
if(e.x2<=t[p<<].r) update(l,mid,e,p<<);
else if(e.x1>=t[p<<|].l) update(mid,r,e,p<<|);
else
{
Line tmp=e;
tmp.x2=t[p<<].r;
update(l,mid,tmp,p<<);
tmp=e;
tmp.x1=t[p<<|].l;
update(mid,r,tmp,p<<|);
}
calc(p);
} int main()
{
//freopen("poj1177.in","r",stdin);
//freopen("poj1177.out","w",stdout);
int n;
while(scanf("%d",&n)!=EOF)
{
int cnt=;
for(int i=;i<=n-;i++)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line[cnt].x1=x1;
line[cnt].x2=x2;
line[cnt].y=y1;
line[cnt].f=;
x[cnt++]=x1;
line[cnt].x1=x1;
line[cnt].x2=x2;
line[cnt].y=y2;
line[cnt].f=-;
x[cnt++]=x2;
}
sort(line,line+cnt,cmp);
sort(x,x+cnt);
int m=unique(x,x+cnt)-x;
build(,m-,); int ans=;
int last=;
for(int i=;i<cnt-;i++)
{
update(,m-,line[i],); ans+=t[].num**(line[i+].y-line[i].y);
ans+=abs(t[].cnt-last);
last=t[].cnt;
}
update(,m-,line[cnt-],);
ans+=abs(t[].cnt-last);
printf("%d\n",ans);
} return ;
}
【HDOJ1828&&POJ1177】Picture(线段树,扫描线)的更多相关文章
- hdu1828 Picture(线段树+扫描线+矩形周长)
看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积)) 解法一·:两次扫描线 如图我们可以 ...
- POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)
题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...
- POJ1177 Picture 线段树+离散化+扫描线
求最终的覆盖图形周长,写这种代码应该短而精确,差的比较远 /* Problem: 1177 User: 96655 Memory: 348K Time: 32MS Language: C++ Resu ...
- poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)
题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ...
- HDU1828 Picture 线段树+扫描线模板题
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 1828 Picture (线段树:扫描线周长)
依然是扫描线,只不过是求所有矩形覆盖之后形成的图形的周长. 容易发现,扫描线中的某一条横边对答案的贡献. 其实就是 加上/去掉这条边之前的答案 和 加上/去掉这条边之后的答案 之差的绝对值 然后横着竖 ...
- Luogu1856 [USACO5.5]矩形周长Picture (线段树扫描线)
对于横轴,加上与上一次扫描的差值:对于竖轴,加上高度差与区间内不相交线段\(*2\)的积: 难点在pushdown,注意维护覆盖关系.再就注意负数 #include <iostream> ...
- poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)
题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...
- HDU 1828 Picture (线段树+扫描线)(周长并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 给你n个矩形,让你求出总的周长. 类似面积并,面积并是扫描一次,周长并是扫描了两次,x轴一次,y ...
- 线段树扫描线(一、Atlantis HDU - 1542(覆盖面积) 二、覆盖的面积 HDU - 1255(重叠两次的面积))
扫描线求周长: hdu1828 Picture(线段树+扫描线+矩形周长) 参考链接:https://blog.csdn.net/konghhhhh/java/article/details/7823 ...
随机推荐
- linux网络编程之断点传输文件
以下载链接"http://www.boa.org/boa-0.94.13.tar.gz"为例: 断点续传实验大概步骤: ===================== 1,使用geth ...
- PHP PDO 使用类
PDO类 <?php class MYPDO { protected static $_instance = null; protected $dbName = ''; protected $d ...
- mui的选项卡js选中指定项
dom结构:在一定条件下想默认选中第二个选项卡 <div id="segmentedControl" class="mui-segmented-control mu ...
- 739. Daily Temperatures
https://leetcode.com/problems/daily-temperatures/description/ class Solution { public: vector<int ...
- 格雷码Gray Code详解
格雷码简介 在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同,则称这种编码为格雷码(Gray Code),另外由于最大数与最小数之间也仅一位数不同,即“首尾相连”,因此又称循环码或反射码.格 ...
- 版本控制之GitHub — — 第一步的理解
GitHub是时下最流行的版本控制的一门“技术”,此之前svn(subversion)也是同样的作用. 至于版本控制:Git是分布式的,而svn是中心式的(或者叫集中式的)版本控制系统,这是两者之间理 ...
- 03015_JSTL技术
1.JSTL概述 (1)JSP(JSP Standard Tap Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能.jstl出现的目的同el一样也是要替代js ...
- java以正确的方式停止线程
java线程停止可以说是非常有讲究的,看起来非常简单,但是也要做好一些防范措施,一般停止一个线程可以使用Thread.stop();来实现,但是最好不要用,因为他是不安全的. 大多数停止线程使用Thr ...
- Dataflow编程模型和spark streaming结合
Dataflow编程模型和spark streaming结合 主要介绍一下Dataflow编程模型的基本思想,后面再简单比较一下Spark streaming的编程模型 == 是什么 == 为用户提 ...
- bash shell命令与监测的那点事(二)
bash shell命令与监测的那点事之top 上次我们说到了ps命令,ps命令虽然在收集运行在系统上的进程信息很有用,但是也有不足之处,ps命令只能显示某个特定时间点的信息,如果你想观察频繁换进换出 ...