HDU 1828 Picture (线段树:扫描线周长)
依然是扫描线,只不过是求所有矩形覆盖之后形成的图形的周长。
容易发现,扫描线中的某一条横边对答案的贡献。
其实就是 加上/去掉这条边之前的答案 和 加上/去掉这条边之后的答案 之差的绝对值
然后横着竖着都做一遍就行了
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 10010
#define ll long long
using namespace std; int n,sz;
int a[N],cnt[N<<],sum[N<<];
struct SQU{
int a1,b1,a2,b2;
}q[N];
struct node{
int l,r,la,ra,h,f;
}sc[N];
int cmp1(node s1,node s2){
if(s1.h!=s2.h) return s1.h<s2.h;
else return s1.f>s2.f;
}
void pushup(int l,int r,int rt)
{
if(cnt[rt]>) sum[rt]=a[r+]-a[l];
else if(l==r) sum[rt]=;
else sum[rt]=sum[rt<<]+sum[rt<<|];
}
void update(int L,int R,int l,int r,int rt,int w)
{
if(L<=l&&r<=R)
{
cnt[rt]+=w;
pushup(l,r,rt);
return;
}
int mid=(l+r)>>;
if(L<=mid) update(L,R,l,mid,rt<<,w);
if(R>mid) update(L,R,mid+,r,rt<<|,w);
pushup(l,r,rt);
}
void clr()
{
memset(a,,sizeof(a));
memset(sc,,sizeof(sc));
memset(cnt,,sizeof(cnt));
memset(sum,,sizeof(sum));
}
int solvex()
{
int ans=,lst=;
for(int i=;i<=n;i++)
{
a[*i-]=q[i].a1,a[*i]=q[i].a2;
sc[*i-].l=q[i].a1,sc[*i].l=q[i].a1;
sc[*i-].r=q[i].a2,sc[*i].r=q[i].a2;
sc[*i-].h=q[i].b1,sc[*i].h=q[i].b2;
sc[*i-].f=,sc[*i].f=-;
}
sort(a+,a+*n+);
sz=unique(a+,a+*n+)-(a+);
sort(sc+,sc+*n+,cmp1);
for(int i=;i<=*n;i++)
{
int la=lower_bound(a+,a+sz+,sc[i].l)-a;
int ra=lower_bound(a+,a+sz+,sc[i].r)-a;
lst=sum[];
update(la,ra-,,sz,,sc[i].f);
ans+=abs(sum[]-lst);
}
return ans;
}
int solvey()
{
int ans=,lst=;
for(int i=;i<=n;i++)
{
a[*i-]=q[i].b1,a[*i]=q[i].b2;
sc[*i-].l=q[i].b1,sc[*i].l=q[i].b1;
sc[*i-].r=q[i].b2,sc[*i].r=q[i].b2;
sc[*i-].h=q[i].a1,sc[*i].h=q[i].a2;
sc[*i-].f=,sc[*i].f=-;
}
sort(a+,a+*n+);
sz=unique(a+,a+*n+)-(a+);
sort(sc+,sc+*n+,cmp1);
for(int i=;i<=*n;i++)
{
int la=lower_bound(a+,a+sz+,sc[i].l)-a;
int ra=lower_bound(a+,a+sz+,sc[i].r)-a;
lst=sum[];
update(la,ra-,,sz,,sc[i].f);
ans+=abs(sum[]-lst);
}
return ans;
} int main()
{
//freopen("data.in","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d%d%d",&q[i].a1,&q[i].b1,&q[i].a2,&q[i].b2);
int ret=;
ret+=solvex();
clr();
ret+=solvey();
printf("%d\n",ret);
return ;
}
HDU 1828 Picture (线段树:扫描线周长)的更多相关文章
- HDU 1828 Picture (线段树+扫描线)(周长并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 给你n个矩形,让你求出总的周长. 类似面积并,面积并是扫描一次,周长并是扫描了两次,x轴一次,y ...
- POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算
求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...
- hdu 1828 Picture(线段树 || 普通hash标记)
http://acm.hdu.edu.cn/showproblem.php?pid=1828 Picture Time Limit: 6000/2000 MS (Java/Others) Mem ...
- hdu 1828 Picture(线段树轮廓线)
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hdu 1828 线段树扫描线(周长)
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hdu1828 Picture(线段树+扫描线+矩形周长)
看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积)) 解法一·:两次扫描线 如图我们可以 ...
- HDU 1828 Picture(长方形的周长和)
HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4 ...
- HDU 1542 - Atlantis - [线段树+扫描线]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- 覆盖的面积 HDU - 1255 (线段树-扫描线)模板提
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1& ...
随机推荐
- XPath语法简明介绍
简介: XPath 是一门在 XML 文档中查找信息的语言.XPath 用于在 XML 文档中通过元素和属性进行导航. XPath 路径表达式: XPath 使用路径表达式来选取 XML 文档中的节点 ...
- Tensorboard服务激活
首先确定Tensorflow的具体位置(在Dos环境下,也就是cmd) cd .. cd scripts conda env list activate tensorflow tensorboard ...
- SQL-Oracle-创建Dblink
create database link DBLINK_IMARK_RAC connect to imark identified by imarkDB12345 using '(DESCRIPTIO ...
- A server is already running. Check tmp/pids/server.pid.
A server is already running. Check tmp/pids/server.pid. 把server.pid删除: 学习了: http://stackoverflow.co ...
- Leetcode--easy系列2
#14 Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- 石子合并(区间dp)
石子合并(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程仅仅能每次将相邻 ...
- angularjs1-7,供应商
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- ES内存持续上升问题定位
https://discuss.elastic.co/t/memory-usage-of-the-machine-with-es-is-continuously-increasing/23537/ ...
- tflearn中一些CNN RNN的例子
lstm.py # -*- coding: utf-8 -*- """ Simple example using LSTM recurrent neural networ ...
- Linux uname 命令 打印系统信息
转自:https://www.jb51.net/LINUXjishu/417626.html 1.概述 打印系统信息 2.命令格式 uname [OPTION]... 3.常用命令参数 打印一些系统信 ...