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 ...
随机推荐
- Python - 内置函数 选例
概览参见 https://www.runoob.com/python/python-built-in-functions.html 官方文档 https://docs.python.org/3/li ...
- 软件工程M1/M2总结
也不分M1/M2了,就从头到尾的梳理一下这学期的软工课吧. 第一节课,老师就稀里哗啦说了一下这学期要怎么搞,什么个人项目啦,结对项目啦,团队项目一二啦,还要组队啊什么的,然后风风火火的组队. 个人项目 ...
- 《Linux内核设计与实现》读书笔记六
第4章 进程调度35 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行态进程之间分配有限的处理器时间资源的内核子系统.只有通过调度程序的合理调度,系统资源才能最 ...
- github的使用心得
我的github地址:https://github.com/gaino1/test GitHub 是一个用于使用Git版本控制系统的项目的基于互联网的存取服务. GitHub可以托管各种git库,并提 ...
- Find Amir CodeForces - 805C (贪心+思维)
A few years ago Sajjad left his school and register to another one due to security reasons. Now he w ...
- iOS开发线程安全问题
先来看一下代码: - (void)viewDidLoad { [super viewDidLoad]; self.testStr = @"String initial complete&qu ...
- 7-Python3从入门到实战—基础之数据类型(字典-Dictionary)
Python从入门到实战系列--目录 字典的定义 字典是另一种可变容器模型,且可存储任意类型对象:使用键-值(key-value)存储,具有极快的查找速度: 字典的每个键值(key=>value ...
- Failed while installing JAX-RS (REST Web Services) 1.1. org.osgi.service.prefs.BackingStoreException: Resource '/.settings' does not exist.
https://issues.jboss.org/browse/JBIDE-10039?_sscc=t https://stackoverflow.com/questions/16836832/fai ...
- MySQL: Connection Refused,调整 mysql.ini中的 max_connections
连接相同的结构的MySQL数据库,一套库Tomcat启动正常,另一套库一直报Connection Refused. 可以断定是连接数太小了.查找mysql.ini中的 max_connections, ...
- Classification Truth Table
在机器学习中对于分类结果的描述,一般有四种:true positive, true negative, false positive 和 false negative. Precision, Reca ...