HDU1828 Picture 线段树+扫描线模板题
Picture
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6332 Accepted Submission(s): 2951
Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.
The corresponding boundary is the whole set of line segments drawn in Figure 2.
The vertices of all rectangles have integer coordinates.
0 <= number of rectangles < 5000
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.
Please process to the end of file.
-15 0 5 10
-5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
30 10 36 20
34 0 40 16
代码如下
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 1e5+;
struct node1{
int l,r;
int cover;
int len;
int lbt,rbt;
int segnum;
}st[maxn<<];
struct node2{
int x,y1,y2;
int flag;
}edge[maxn];
int Y[maxn];
void build(int root,int l,int r){
st[root].l=l;
st[root].r=r;
st[root].cover=st[root].len=;
st[root].lbt=st[root].rbt=;
st[root].segnum=;
if(r-l==) return;
int mid=(l+r+)/;
build(root<<,l,mid);
build(root<<|,mid,r);
} void update(int root){
if(st[root].cover>){
st[root].len=Y[st[root].r]-Y[st[root].l];
st[root].lbt=st[root].rbt=;
st[root].segnum=;
}else if(st[root].r-st[root].l==){
st[root].len=;
st[root].lbt=st[root].rbt=;
st[root].segnum=;
}else{
st[root].len=st[root<<].len+st[root<<|].len;
st[root].lbt=st[root<<].lbt;
st[root].rbt=st[root<<|].rbt;
st[root].segnum=st[root<<].segnum+st[root<<|].segnum-st[root<<].rbt*st[root<<|].lbt;
}
}
void update(int root,int val,int y1,int y2){
if(y1<=Y[st[root].l]&&Y[st[root].r]<=y2){
st[root].cover+=val;
}else{
int mid=(st[root].l+st[root].r+)/;
if(y1<Y[mid]) update(root<<,val,y1,y2);
if(y2>Y[mid]) update(root<<|,val,y1,y2);
}
update(root);
}
bool cmp(node2 a,node2 b){
if(a.x==b.x) return a.y1<b.y1;
return a.x<b.x;
}
int main(){
#ifndef ONLINE_JUDGE
FIN
#endif int n;
while(scanf("%d",&n) !=EOF){
if(n==) printf("0\n");
int x1,x2,y1,y2;
for(int i=;i<n;i++){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
edge[i<<].x=x1;
edge[i<<].y1=y1;
edge[i<<].y2=y2;
edge[i<<].flag=;
Y[i<<]=y1;
edge[i<<|].x=x2;
edge[i<<|].y1=y1;
edge[i<<|].y2=y2;
edge[i<<|].flag=-;
Y[i<<|]=y2;
}
sort(Y,Y+*n);
sort(edge,edge+*n,cmp);
int cnt=;
for(int i=;i<*n;i++){
if(Y[i]!=Y[i+]) Y[cnt++]=Y[i];
}
Y[cnt++]=Y[*n-];
build(,,cnt-);
int ans=;
int lastlen=;
for(int i=;i<*n-;i++){
update(,edge[i].flag,edge[i].y1,edge[i].y2);
ans+=*st[].segnum*(edge[i+].x-edge[i].x);
ans+=abs(st[].len-lastlen);
lastlen=st[].len;
}
ans+=(edge[*n-].y2-edge[*n-].y1);
printf("%d\n",ans);
}
return ;
}
HDU1828 Picture 线段树+扫描线模板题的更多相关文章
- hdu1828 Picture(线段树+扫描线+矩形周长)
看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积)) 解法一·:两次扫描线 如图我们可以 ...
- Zeratul的完美区间(线段树||RMQ模板题)
原题大意:原题链接 给定元素无重复数组,查询给定区间内元素是否连续 解体思路:由于无重复元素,所以如果区间内元素连续,则该区间内的最大值和最小值之差应该等于区间长度(r-l) 解法一:线段树(模板题) ...
- POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)
题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...
- poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)
题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...
- poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)
题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ...
- HDU 1828 Picture (线段树:扫描线周长)
依然是扫描线,只不过是求所有矩形覆盖之后形成的图形的周长. 容易发现,扫描线中的某一条横边对答案的贡献. 其实就是 加上/去掉这条边之前的答案 和 加上/去掉这条边之后的答案 之差的绝对值 然后横着竖 ...
- Luogu1856 [USACO5.5]矩形周长Picture (线段树扫描线)
对于横轴,加上与上一次扫描的差值:对于竖轴,加上高度差与区间内不相交线段\(*2\)的积: 难点在pushdown,注意维护覆盖关系.再就注意负数 #include <iostream> ...
- HDU 1828 Picture (线段树+扫描线)(周长并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 给你n个矩形,让你求出总的周长. 类似面积并,面积并是扫描一次,周长并是扫描了两次,x轴一次,y ...
- 线段树扫描线(一、Atlantis HDU - 1542(覆盖面积) 二、覆盖的面积 HDU - 1255(重叠两次的面积))
扫描线求周长: hdu1828 Picture(线段树+扫描线+矩形周长) 参考链接:https://blog.csdn.net/konghhhhh/java/article/details/7823 ...
随机推荐
- mysql5.7.19安装报错 无法定位程序输入点
https://blog.csdn.net/t876587201/article/details/79503688
- MyBatis的笔记
1.#{}和${}的区别是什么? #{}是预编译处理,${}是字符串替换. #{}是sql的参数占位符,${}是Properties文件中的变量占位符,它可以用于标签属性值和sql内部,属于静态文本替 ...
- ssrf小记
SSRF(Server-Side Request Forgery, 服务端请求伪造),攻击者伪造服务端发起的请求并执行,从而获得一些数据或进行攻击 一.危害 1.对内网的端口和服务进行扫描,对主机本地 ...
- bzoj 一些题目汇总
2140: 稳定婚姻 /* 求联通分量. */ #include<bits/stdc++.h> using namespace std; typedef long long LL; inl ...
- Android TextView 单行文本的坑
这是android系统的一个bug,描述如下:https://code.google.com/p/android/issues/detail?id=33868 具体来说就是当一个TextView设置了 ...
- (3)分布式下的爬虫Scrapy应该如何做-递归爬取方式,数据输出方式以及数据库链接
放假这段时间好好的思考了一下关于Scrapy的一些常用操作,主要解决了三个问题: 1.如何连续爬取 2.数据输出方式 3.数据库链接 一,如何连续爬取: 思考:要达到连续爬取,逻辑上无非从以下的方向着 ...
- eclipse 关闭validating
1.起因 validating XXX 总是非常的浪费时间,有时候还会造成程序卡死 2.解决 windows - Perferences - Validation build 全部去掉
- Django数据模型--字段整理
一.字段 1.CharField: 字段数据类型为字符串 2.IntegerField: 字段数据类型为整形 3.BooleanField: 布尔类型 4.NullBooleanField: 允许为空 ...
- Python图像全屏显示
需要在嵌入式设备上全屏显示图像,使用pil显示图像时,只能通过系统的图像浏览器显示.所以使用Python自带的tkinter import Tkinter as tk 这句在Python3中已经改 ...
- Daily Scrum02 12.03
Daily Scrum03 12.03 一天过去了,新的一天即将到来,我们组仍旧干劲十足呢~ Member Today's Task Tomorrow's Task 李孟 孟神有点累了呢 task85 ...