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 ...
随机推荐
- 线程池ThreadPoolExecutor整理
项目用到线程池,但是其实很多人对原理并不熟悉 ,这里只是整理一下 ThreadPoolExecutor java.uitl.concurrent.ThreadPoolExecutor类是线程池中最核心 ...
- .net core实践系列之短信服务-Api的SDK的实现与测试
前言 上一篇<.net core实践系列之短信服务-Sikiro.SMS.Api服务的实现>讲解了API的设计与实现,本篇主要讲解编写接口的SDK编写还有API的测试. 或许有些人会认为, ...
- [T-ARA][떠나지마][不要离开]
歌词来源:http://music.163.com/#/song?id=22704408 잊기엔 너무 사랑했나봐 [id-ggi-en neo-mu sa-lang-haen-na-bwa] 아직도 ...
- cometd简单用例
准备工作 整个例子的源码下载:http://pan.baidu.com/s/1gfFYSbp 下载服务端jar文件 Comet4J目前仅支持Tomcat6.7版本,根据您所使用的Tomcat版本下载[ ...
- shell脚本--逻辑判断与字符串比较
涉及到比较和判断的时候,要注意 整数比较使用-lt,-gt,ge等比较运算符,详情参考:整数比较 文件测试使用 -d, -f, -x等运算发,详情参考:文件测试 逻辑判断使用 && ...
- HTML使用button的一个小坑
https://www.w3schools.com/TAGs/att_button_type.asp Definition and Usage The type attribute specifies ...
- 关于Tomcat性能监控的第三方工具Probe的简介
Tomcat Probe => Lambda Probe =>PSI Probe,这个小工具已经三易其名了.(现在挪窝到GitHub了,很方便). 这个Probe可以说是一个增强版本的 T ...
- Nginx+Php-fpm运行原理详解
一.代理与反向代理 现实生活中的例子 1.正向代理:访问google.com 如上图,因为google被墙,我们需要vpnFQ才能访问google.com. vpn对于“我们”来说,是可以感知到的(我 ...
- Qt__QWidget::update()与Qwidget::repaint()的区别
QT事件的产生 1.操作系统产生 操作系统将获取的事件,比如鼠标按键,键盘按键等keyPressEvent,keyReleaseEvent,mousePressEvent,mouseReleaseEv ...
- number (2)变量相关错误
变量没有被定义 fw cannot be resolved 变量没有被初始化 正确代码 package com.itheima_01; import java.io.FileWriter;import ...