题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1828

分析:与面积不同的地方是还要记录竖的边有几个(num记录),并且当边界重合的时候需要合并(用lbd和rbd表示边界来辅助)

线段树操作:update:区间增减 query:直接取根节点的值

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 10007
#define inf 0x3f3f3f3f
#define N 20015
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
struct line
{
int l,r,h,d;
line(){}
line(int l,int r,int h,int d):l(l),r(r),h(h),d(d){}
bool operator<(const line &a)const
{
return h<a.h;
}
}s[N];
int sum[N<<],col[N<<],num[N<<];
int lbd[N<<],rbd[N<<];
void Pushup(int l,int r,int rt)
{
if(col[rt])
{
lbd[rt]=;rbd[rt]=;
num[rt]=;
sum[rt]=r-l+;
}
else if(l==r)
{
num[rt]=;sum[rt]=;
lbd[rt]=;rbd[rt]=;
}
else
{
lbd[rt]=lbd[rt<<];rbd[rt]=rbd[rt<<|];
sum[rt]=sum[rt<<]+sum[rt<<|];
num[rt]=num[rt<<]+num[rt<<|];
if(rbd[rt<<]&&lbd[rt<<|])num[rt]-=; }
}
void update(int L,int R,int d,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
col[rt]+=d;
Pushup(l,r,rt);
return;
}
int m=(l+r)>>;
if(L<=m)update(L,R,d,lson);
if(m<R)update(L,R,d,rson);
Pushup(l,r,rt);
}
int main()
{
int L,R,n;
int x1,x2,y1,y2;
while(scanf("%d",&n)>)
{
int m=;
L=inf;R=-inf;
for(int i=;i<=n;i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
L=min(L,x1);R=max(R,x2);
s[m++]=line(x1,x2,y1,);
s[m++]=line(x1,x2,y2,-);
}
sort(s,s+m);
int ans=,last=;
for(int i=;i<m;i++)
{
update(s[i].l,s[i].r-,s[i].d,L,R-,);
ans+=num[]*(s[i+].h-s[i].h);
ans+=abs(sum[]-last);
last=sum[];
}
printf("%d\n",ans);
}
}

hdu1828(线段树——矩形周长并)的更多相关文章

  1. HDU 1828:Picture(扫描线+线段树 矩形周长并)

    题目链接 题意 给出n个矩形,求周长并. 思路 学了区间并,比较容易想到周长并. 我是对x方向和y方向分别做两次扫描线.应该记录一个pre变量,记录上一次扫描的时候的长度,对于每次遇到扫描线统计答案的 ...

  2. hdu 1828 线段树扫描线(周长)

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

  3. hdu1828 线段树扫描线求矩形面积的周长

    题意:       给你n个矩形,问你这n个矩形所围成的图形的周长是多少. 思路:       线段树的扫描线简单应用,这个题目我用的方法比较笨,就是扫描两次,上下扫描,求出多边形的上下边长和,然后同 ...

  4. hdu1828(线段树+扫描线)

    又知道了线段树的一种用法,除了单点更新,区间更新,还有这种在一段线段上标号但不往下推. 真是神奇 hdu1828 #include <iostream> #include <stdi ...

  5. HDU 1542:Atlantis(扫描线+线段树 矩形面积并)***

    题目链接 题意 给出n个矩形,求面积并. 思路 使用扫描线,我这里离散化y轴,按照x坐标从左往右扫过去.离散化后的y轴可以用线段树维护整个y上面的线段总长度,当碰到扫描线的时候,就可以统计面积.这里要 ...

  6. hdu1542(线段树——矩形面积并)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 分析:离散化+扫描线+线段树 #pragma comment(linker,"/STA ...

  7. 51nod 1206 && hdu 1828 Picture (扫描线+离散化+线段树 矩阵周长并)

    1206 Picture  题目来源: IOI 1998 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 给出平面上的N个矩形(矩形的边平行于X轴 ...

  8. hdu1828 线段树+离散化+扫描线

    添加lb[],rb[]数组,来标记竖边.添加num,来计算竖边的个数,因为计算周长的时候,未覆盖的竖边都要加. #include<stdio.h> #include<stdlib.h ...

  9. poj 1151 Atlantis (离散化 + 扫描线 + 线段树 矩形面积并)

    题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 映射到y轴,并且记录下每个的y坐标,并对y坐标进行离散. 然后按照x从左向右扫描. #include <io ...

随机推荐

  1. Linux 安装ibus极点五笔输入法备忘录

    Linux 安装 ibus 五笔输入法备忘录 useful?: https://github.com/definite/ibus-table-chinese 一. yum install ibus* ...

  2. 【Demo 0009】Android 组件(BroadcastReceiver)

    本章学习要点:        1.  了解Broadcast的作用;        2.  掌握自定义广播和系统广播的接收:        3.  掌握广播的发送:

  3. G - Self Numbers(2.2.1)

    G - Self Numbers(2.2.1) Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & % ...

  4. Kendo UI开发教程(21): Kendo MVVM 数据绑定(十) Source

    Source绑定可以把ViewModel的值和由Kendo模板定义的目标元素绑定,如果ViewModel的值发生变化,被绑定的目标元素也随之发生变化.模板由属性data-template指定,它的值为 ...

  5. 核心游记之 page_address_init

    lock_kernel()仅仅虚晃一枪就过去了. 紧接着来的是page_address_init include/linux/mm.h   #if defined(CONFIG_HIGHMEM) &a ...

  6. p类型最大可定义范围

    t7(16) TYPE  p DECIMALS 14,

  7. Hacker News网站的文章排名算法工作原理

    In this post I'll try to explain how Hacker News ranking algorithm works and how you can reuse it in ...

  8. Delphi代码中嵌入ASM代码(简单明了)

    前言 Delphi作为一个快速高效的开发平台,使用的人越来越多,但熟悉在Delphi代码中嵌入ASM代码的程序员我想不多,因为这方面的资料太少了,另一方面,它还需要有基本的汇编语言知识,关於汇编语言的 ...

  9. (原创)(C#随笔)IEnumerable< ICollection < IList区别

    public interface IEnumerable { IEnumerator GetEnumerator(); } 再看ICollection<T> public interfac ...

  10. 怎么用CIFilter给图片加上各种各样的滤镜_1

    近期在做对图像处理的app,而要对图像进行处理,CoreImage是离不开的框架. 上网Google了一下,大部分的博客都是仅仅讲了怎么给图像加上怀旧(CISepiaTone).模糊(CIBloom) ...