P1856 矩形周长
哇!这小破题坑了我好久。
扫描线+线段树
这题数据范围小,没离散化。真要离散化我还搞不好呢。
具体的看这个博客吧。
主要是这个坑爹的c,len把我搞了,其他的还好。
代码:
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;
struct Node
{
int len,s;
} node[];
struct Edge
{
int L,R,high,flag;
bool operator < (const Edge &a) const
{
if(this->high!=a.high)return this->high>a.high;
return this->flag<a.flag;
}
};
priority_queue<Edge>x;
priority_queue<Edge>y;
void build(int l,int r,int o)
{
node[o].len=;
node[o].s=;
if(l==r) return;
int mid=(l+r)>>;
build(l,mid,o<<);
build(mid+,r,o<<|);
return;
}
void up(int l,int r,int o)
{
if(node[o].s) node[o].len=r-l+;
else if(l==r) node[o].len=;
else node[o].len=node[o<<].len+node[o<<|].len;
return;
}
void add(int L,int R,int v,int l,int r,int o)
{
//printf("add:%d %d %d %d %d %d\n",L,R,v,l,r,o);
if(L<=l && r<=R)
{
node[o].s+=v;
up(l,r,o);
return;///这里的return一开始忘了打,死循环。
}
if(R<l || r<L) return;
int mid=(l+r)>>;
if(L<=mid) add(L,R,v,l,mid,o<<);
if(mid<R) add(L,R,v,mid+,r,o<<|);
up(l,r,o);
return;
}
void adde(int x1,int y1,int x2,int yyy)
{
Edge xup,xlow,yup,ylow;
xup.L=xlow.L=ylow.high=x1;
xup.R=xlow.R=yup.high=x2;
yup.L=ylow.L=xlow.high=y1;
yup.R=ylow.R=xup.high=yyy;
xlow.flag=ylow.flag=;
xup.flag=yup.flag=-;
//printf("adde:%d %d %d %d\n",xup.L,xup.R,xup.high,xup.flag);
//printf("adde:%d %d %d %d\n",xlow.L,xlow.R,xlow.high,xlow.flag);
x.push(xup);
x.push(xlow);
y.push(yup);
y.push(ylow);
return;
}
int main()
{
int N = ;
build(,N,);
int n,x1,x2,y1,yyy;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&yyy);///这里 的变量名传错了,0分
adde(x1+,y1+,x2+,yyy+);
}
Edge e;int len=;
long long int ans=;
while(!x.empty())
{
//printf("x\n");
e=x.top();
x.pop();
//printf("edge:%d %d %d %d\n",e.L,e.R,e.high,e.flag);
add(e.L,e.R-,e.flag,,N,);
ans+=abs(node[].len-len);
len=node[].len;
//printf("ans=%d\n",ans);
}
build(,N,);len=;
while(!y.empty())
{
//printf("y\n");
e=y.top();
//printf("edge:%d %d %d %d\n",e.L,e.R,e.high,e.flag);
y.pop();
add(e.L,e.R-,e.flag,,N,);
ans+=abs(node[].len-len);
len=node[].len;
//printf("ans=%d\n",ans);
}
printf("%lld",ans);
return ;
}
/**
4
1 1 2 2
3 1 5 3
4 0 6 2
5 -1 7 1 20
*/
洛谷的数据好水
还有更高级的方法我没搞了,晦涩难懂......
P1856 矩形周长的更多相关文章
- P1856 [USACO5.5]矩形周长Picture
P1856 [USACO5.5]矩形周长Picture $len$ $sum$ $num$ $flag\_l$ $flage\_ ...
- P1856 [USACO5.5]矩形周长Picture[扫描线]
题目背景 墙上贴着许多形状相同的海报.照片.它们的边都是水平和垂直的.每个矩形图片可能部分或全部的覆盖了其他图片.所有矩形合并后的边长称为周长. 题目描述 编写一个程序计算周长. 如图1所示7个矩形. ...
- 25.按要求编写一个Java应用程序: (1)编写一个矩形类Rect,包含: 两个属性:矩形的宽width;矩形的高height。 两个构造方法: 1.一个带有两个参数的构造方法,用于将width和height属性初化; 2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。 两个方法: 求矩形面积的方法area() 求矩形周长的方法perimeter() (2)通过继承Rect类编写一个具有
package zhongqiuzuoye; //自己写的方法 public class Rect { public double width; public double height; Rect( ...
- HDU 1828 / POJ 1177 Picture --线段树求矩形周长并
题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 ...
- HDU 1828 扫描线(矩形周长并)
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 6362(求椭圆中矩形周长的期望 数学)
题意是给定一个椭圆标准方程的a,b(椭圆的长半轴长和短半轴长),在[0,b]内取一个数,则过点(0,b)且平行于x轴的直线与椭圆交于两点,再将此两点关于x轴做对称点,顺次连接此四点构成矩形,求出这些矩 ...
- hdu 1828 Picture(线段树扫描线矩形周长并)
线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...
- 51nod 1206 Picture 矩形周长求并 | 线段树 扫描线
51nod 1206 Picture 矩形周长求并 | 线段树 扫描线 #include <cstdio> #include <cmath> #include <cstr ...
- POJ 1177 矩形周长并 模板
Picture 题目链接 http://poj.org/problem?id=1177 Description A number of rectangular posters, photographs ...
随机推荐
- 容斥原理I
普利斯记号 以下以"人"代指受条件约束的元素. \(K(x)\)表示刚好\(x\)人满足条件的方案数. \(S(x)\)表示至少\(x\)人满足条件的方案数. \(C(x)\)表示 ...
- M2阶段测试报告
一.安全漏洞测试报告: http://files.cnblogs.com/hotsbuaa/M2-安全漏洞测试.pdf 二.全面兼容测试: http://files.cnblogs.com/hotsb ...
- 美团外卖app可行性分析
美团外卖app可行性分析 1 引言 1.1编写目的 年轻人追求时尚,快捷,因此外卖行业拥有广阔的消费群体:团购的兴起,也促进了人们的消费欲望,人们继续一个外卖平台,来满足他们的欲望.O2o模式的日渐完 ...
- 使用a标签实现文件的下载与保存
<a>标签的常规使用是定义超链接,用于从一个页面链接到另一个页面,并且需要指定链接目标href,除了定义超链接外,<a>还可以实现文件的保存,直接设置a标签的href属性,但是 ...
- Install Jetty web server on CentOS 7 / RHEL 7
http://www.eclipse.org/jetty/download.html http://www.eclipse.org/jetty/documentation/current/startu ...
- Docker查看容器IP
https://segmentfault.com/q/1010000001637726 https://blog.csdn.net/sannerlittle/article/details/77063 ...
- Ehcache Monitor使用一例
场景介绍:系统集成Shiro,使用Ehcache保存用户登录限制次数,常有用户密码被锁,影响工作效率. 在不考虑集成SSO,LDAP,也不引入身份校验,邮件,短信等解锁特性下.使用Ehcache Mo ...
- mongoDB的配置和使用
如何启动mongodb? mongod --dbpath C:\appStore\mongodata //数据库地址 再开一个cmder窗口 进入C:\Program Files\MongoDB\Se ...
- Docker(十四)-Docker四种网络模式
Docker 安装时会自动在 host 上创建三个网络,我们可用 docker network ls 命令查看: none模式,使用--net=none指定,该模式关闭了容器的网络功能. host模式 ...
- linux学习之centos(四):git的安装
整个流程如下:(参考文章:linux安装git方法) [carsonzhu@localhost 桌面]$ wget https://github.com/git/git/archive/v2.8.3. ...