题意:给定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(线段树,扫描线)的更多相关文章

  1. hdu1828 Picture(线段树+扫描线+矩形周长)

    看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积))  解法一·:两次扫描线 如图我们可以 ...

  2. POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)

    题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...

  3. POJ1177 Picture 线段树+离散化+扫描线

    求最终的覆盖图形周长,写这种代码应该短而精确,差的比较远 /* Problem: 1177 User: 96655 Memory: 348K Time: 32MS Language: C++ Resu ...

  4. poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)

    题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ...

  5. HDU1828 Picture 线段树+扫描线模板题

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  6. HDU 1828 Picture (线段树:扫描线周长)

    依然是扫描线,只不过是求所有矩形覆盖之后形成的图形的周长. 容易发现,扫描线中的某一条横边对答案的贡献. 其实就是 加上/去掉这条边之前的答案 和 加上/去掉这条边之后的答案 之差的绝对值 然后横着竖 ...

  7. Luogu1856 [USACO5.5]矩形周长Picture (线段树扫描线)

    对于横轴,加上与上一次扫描的差值:对于竖轴,加上高度差与区间内不相交线段\(*2\)的积: 难点在pushdown,注意维护覆盖关系.再就注意负数 #include <iostream> ...

  8. poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)

    题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...

  9. HDU 1828 Picture (线段树+扫描线)(周长并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 给你n个矩形,让你求出总的周长. 类似面积并,面积并是扫描一次,周长并是扫描了两次,x轴一次,y ...

  10. 线段树扫描线(一、Atlantis HDU - 1542(覆盖面积) 二、覆盖的面积 HDU - 1255(重叠两次的面积))

    扫描线求周长: hdu1828 Picture(线段树+扫描线+矩形周长) 参考链接:https://blog.csdn.net/konghhhhh/java/article/details/7823 ...

随机推荐

  1. oracle一些常用的数据类型

    字符数据类型 char数据类型 当需要固定长度时,使用char数据类型,此数据类型长度可以使1-2000字节.若是不指定大小默认占1字节,如果长度有空余时会以空格进行填充,如果大于设定长度 数据库则会 ...

  2. 用PHP和Python生成短链接服务的字符串ID

    假设你想做一个像微博短链接那样的短链接服务,短链接服务生成的URL都非常短例如: http://t.cn/E70Piib, 我们应该都能想到链接中的E70Piib对应的就是存储长链接地址的数据记录的I ...

  3. mui的选项卡js选中指定项

    dom结构:在一定条件下想默认选中第二个选项卡 <div id="segmentedControl" class="mui-segmented-control mu ...

  4. web项目中无法开启或404

    404找不到页面,可能是spring的bean自动注入有了问题,例如org.springframework.beans.factory.BeanCreationException:可以检查配置文件的s ...

  5. LeetCode刷题感想

    断断续续用了半年的时间把LeetCode刷完了,之前复习了数据结构与算法.将刷题与复习数据结构结合起来会更有效果.总之不是为了刷题而刷题,而是为了巩固和补充一部分知识. LeetCode真的是一个很好 ...

  6. Hive UDAF开发详解

    说明 这篇文章是来自Hadoop Hive UDAF Tutorial - Extending Hive with Aggregation Functions:的不严格翻译,因为翻译的文章示例写得比较 ...

  7. Android通过AIDL和反射调用系统拨打电话和挂断电话

    首先在项目中添加ITelephony.aidl文件,我的如下: /* * Copyright (C) 2007 The Android Open Source Project * * Licensed ...

  8. Redis实现之服务器

    命令请求的执行过程 一个命令请求从发送到获得回复的过程中,客户端和服务器需要完成一系列操作.举个栗子,如果我们使用客户端执行以下命令: 127.0.0.1:6379> SET KEY VALUE ...

  9. Careercup - Microsoft面试题 - 5799446021406720

    2014-05-12 07:17 题目链接 原题: Given below is a tree/trie A B c D e F a<b<e<>>c<>d&l ...

  10. IOS开发---菜鸟学习之路--(十二)-利用ASIHTTPRequest进行异步获取数据

    想要实现异步获取的话我这边了解过来有两个非常简单的方式 一个是利用ASIHTTPRequest来实现异步获取数据 另一个则是利用MBProgressHUD来实现异步获取数据 本章就先来讲解如何利用AS ...