HDU1828:Picture
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1828
扫描线求周长。如果你不知道什么是扫描线可以先去做这题:https://www.cnblogs.com/AKMer/p/9951300.html
每次扫描线上变化的长度就是周长的一部分。横着做一遍竖着做一遍就行了。
如果两条边\(pos\)相同应该先加边后删边,否则重叠部分会多算。
时间复杂度:\(O(nlogn)\)
空间复杂度:\(O(n)\)
代码如下:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=10005;
int n;
ll ans;
int tmp[2][maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct line {
int pos,st,ed,mark;
line() {}
line(int _pos,int _st,int _ed,int _mark) {
pos=_pos,st=_st,ed=_ed,mark=_mark;
}
bool operator<(const line &a)const {
if(pos==a.pos)return mark>a.mark;//pos相同优先加边,这样就不会算重叠部分的长度
return pos<a.pos;
}
}p[2][maxn];
struct segment_tree {
int len[maxn<<2],cnt[maxn<<2];
void clear() {
memset(len,0,sizeof(len));
memset(cnt,0,sizeof(cnt));
}
void updata(int id,int p,int l,int r) {
if(cnt[p])len[p]=tmp[id][r+1]-tmp[id][l];
else if(l==r)len[p]=0;
else len[p]=len[p<<1]+len[p<<1|1];
}
void change(int id,int p,int l,int r,int L,int R,int v) {
if(L<=l&&r<=R) {
cnt[p]+=v;
updata(id,p,l,r);
return;
}
int mid=(l+r)>>1;
if(L<=mid)change(id,p<<1,l,mid,L,R,v);
if(R>mid)change(id,p<<1|1,mid+1,r,L,R,v);
updata(id,p,l,r);
}
}T;
int main() {
while(~scanf("%d",&n)) {
ans=0;
for(int i=1;i<=n;i++) {
int x1=read(),y1=read(),x2=read(),y2=read();
tmp[0][(i<<1)-1]=y1,tmp[0][i<<1]=y2;
tmp[1][(i<<1)-1]=x1,tmp[1][i<<1]=x2;
p[0][(i<<1)-1]=line(x1,y1,y2,1);
p[0][i<<1]=line(x2,y1,y2,-1);
p[1][(i<<1)-1]=line(y1,x1,x2,1);
p[1][i<<1]=line(y2,x1,x2,-1);
}for(int i=0;i<2;i++) {
T.clear();
sort(p[i]+1,p[i]+2*n+1);
sort(tmp[i]+1,tmp[i]+2*n+1);
int cnt=unique(tmp[i]+1,tmp[i]+2*n+1)-tmp[i]-1;
for(int j=1;j<=2*n;j++) {
p[i][j].st=lower_bound(tmp[i]+1,tmp[i]+cnt+1,p[i][j].st)-tmp[i];
p[i][j].ed=lower_bound(tmp[i]+1,tmp[i]+cnt+1,p[i][j].ed)-tmp[i];
int tmp=T.len[1];
T.change(i,1,1,cnt-1,p[i][j].st,p[i][j].ed-1,p[i][j].mark);
ans+=abs(T.len[1]-tmp);//差值就是周长的一部分
}
}
printf("%lld\n",ans);
}
return 0;
}
HDU1828:Picture的更多相关文章
- 51nod 1206:Picture 求覆盖周长
1206 Picture 题目来源: IOI 1998 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 收藏 关注 给出平面上的N个矩形(矩形的边平行于X轴和 ...
- IOI1998 hdu1828 poj1177 Picture
写了一发扫描线竟然狂WA不止,hdu死活过不了,poj和当时IOI的数据(还花了我1dsdn积分..)都过了. 然后看到谋篇blog里有评论,把数据拿下来发现WA了. 数据是 20 0 1 11 0 ...
- HDU 1828:Picture(扫描线+线段树 矩形周长并)
题目链接 题意 给出n个矩形,求周长并. 思路 学了区间并,比较容易想到周长并. 我是对x方向和y方向分别做两次扫描线.应该记录一个pre变量,记录上一次扫描的时候的长度,对于每次遇到扫描线统计答案的 ...
- 【20190305】CSS-响应式图片:srcset+sizes,picture,svg
响应式图片可以根据不同的设备屏幕大小从而选择加载不同的图片,从而节省带宽.实现响应式图片有三种方法:srcset+sizes属性.picture标签.svg 1. srcset+sizes srcse ...
- 移动端布局:视口viewport的理解
移动端开发中,有一些基本概念需要理解清楚,才能更好的组织编程逻辑.在刚接触时,移动端视口的缩放和rem单位的缩放搞混淆了,弄得自己很蒙圈.所以仔细总结下自己的理解. 移动端的适配,我理解为两点: 第一 ...
- 潭州课堂25班:Ph201805201 爬虫基础 第九课 图像处理- PIL (课堂笔记)
Python图像处理-Pillow 简介 Python传统的图像处理库PIL(Python Imaging Library ),可以说基本上是Python处理图像的标准库,功能强大,使用简单. 但是由 ...
- mfc Picture Control 控件属性
知识点: Picture Control 控件属性 CStatic类 图片控件 图片控件使用 一.图片控件属性 Picture Control 属性: Type:Frame //框架 Type:Etc ...
- 知名网站内部资料:WEB页面内容优化管理与性能技巧
回想一下,以前我们不得不花费大量时间去优化页面内容(图片.CSS等等),如今用户有更快速的互联网链接,我们似乎能够使用更大的图像或更大的闪 存文件,里面包含的有视频或者图片.然而,随着移动开发的兴起, ...
- picture control控件
注意:picture control控件,需要先更改其ID再使用. CImage myImage; CFileDialog fileDlg(TRUE,NULL,NULL,OFN_ALLOWMULTIS ...
随机推荐
- Spring Cloud Zuul API服务网关之请求路由
目录 一.Zuul 介绍 二.构建Spring Cloud Zuul网关 构建网关 请求路由 请求过滤 三.路由详解 一.Zuul 介绍 通过前几篇文章的介绍,我们了解了Spring Cloud ...
- 软件工程第3次作业——Visual Studio 2017下针对代码覆盖率的C/C++单元测试
本项目Github地址(同时包括两个作业项目): Assignment03 -- https://github.com/Oberon-Zheng/SoftwareEngineeringAssignme ...
- java.util.Date和java.sql.Date之间的转换
java.util.Date是在除了SQL语句的情况下面使用的.java.sql.Date是针对SQL语句使用的,它只包含日期而没有时间部分它 们都有getTime方法返回毫秒数,自然就可以直接构建. ...
- git分支处理
查看分支:git branch 创建分支:git branch <name> 切换分支:git checkout <name> 创建+切换分支:git checkout -b ...
- 已知某公司总人数为W,平均年龄为Y岁(每年3月末计算,同时每年3月初入职新人),假设每年离职率为x,x>0&&x<1,每年保持所有员工总数不变进行招聘,新员工平均年龄21岁。 从今年3月末开始,请实现一个算法,可以计算出第N年后公司员工的平均年龄。(最后结果向上取整)。
// ConsoleApplication12.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" // ConsoleApplication1 ...
- C#使用for循环移除HTML标记
public static string StripTagsCharArray(string source) { char[] array = new char[source.Length]; int ...
- Spring和ActiveMQ整合的完整实例
Spring和ActiveMQ整合的完整实例 前言 这篇博文,我们基于Spring+JMS+ActiveMQ+Tomcat,做一个Spring4.1.0和ActiveMQ5.11.1整合实例,实现了 ...
- 【题解】Painting Fence
[题解]Painting Fence 分治模板.贪心加分治.直接\(O(n^2logn)\)分治过去.考虑一块联通的柱形是子问题的,是递归的,贪心分治就可.记得对\(r-l+1\)取\(min\). ...
- linux install beanstalkd
you can instal it via git and then copy systemd script: Step 0. Install git yum install git Step 1. ...
- JAVA 水果机游戏及编码
[转自 http://tech.it168.com/j/2007-11-07/200711070910328_1.shtml] 3. 游戏的开发与编码 在我们开发这个游戏之前,我们先讲一个这 ...