博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU-1828-Picture(线段树)
阅读量:6239 次
发布时间:2019-06-22

本文共 3245 字,大约阅读时间需要 10 分钟。

Problem Description
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.
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.
 
Input
Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.
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.
 
Output
Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.
 
Sample Input
 
7 -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
 
Sample Output
 
228
 
Source

思路:求纵向边要维护重叠次数大于0的纵向长度。用上一次的node[1].m减去当前的node[1].m的绝对值。求横向边要维护每一次更新之后须要加上去的边数。

#include 
#include
#include
using namespace std;struct L{int val;//1 左边。-1 右边int x,y1,y2;}l[10000];struct N{int l,r,n,m;//n记录节点相应的有效横向边数量,m记录纵向长度int cnt;//重叠情况bool lc,rc;}node[40000];int tempy[10000];bool cmp(struct L a,struct L b){ if(a.x==b.x) return a.val>b.val; return a.x
>1; build(idx<<1,s,mid); build(idx<<1|1,mid,e); }}void len(int idx,int s,int e){ if(node[idx].cnt>0) { node[idx].m=node[idx].r-node[idx].l; node[idx].n=2;//相应两条边 node[idx].lc=node[idx].rc=1; } else { if(s+1!=e) { int mid=(s+e)>>1; node[idx].m=node[idx<<1].m+node[idx<<1|1].m; node[idx].n=node[idx<<1].n+node[idx<<1|1].n; node[idx].lc=node[idx<<1].lc; node[idx].rc=node[idx<<1|1].rc; if(node[idx<<1].rc && node[idx<<1|1].lc) node[idx].n-=2;//假设左右儿子是连着的。就要减去多计算的两条边 } else { node[idx].m=0; node[idx].n=0; node[idx].lc=node[idx].rc=0; } }}void update(int idx,int s,int e,struct L line){ if(node[idx].l==line.y1 && node[idx].r==line.y2) { node[idx].cnt+=line.val; len(idx,s,e); return; } if(s+1!=e) { int mid=(s+e)>>1; if(line.y2<=node[idx<<1].r) update(idx<<1,s,mid,line); else if(line.y1>=node[idx<<1|1].l) update(idx<<1|1,mid,e,line); else { int temp=line.y2; line.y2=node[idx<<1].r; update(idx<<1,s,mid,line); line.y2=temp; line.y1=node[idx<<1|1].l; update(idx<<1|1,mid,e,line); } } len(idx,s,e);}int main(){ int T,n,i,t; int x1,x2,y1,y2; while(~scanf("%d",&n)) { for(i=0;i

转载地址:http://zcdia.baihongyu.com/

你可能感兴趣的文章
ArcEngine应用程序中无法实现TOC图层多选
查看>>
Java-笔记9-复习
查看>>
python---基本数据结构
查看>>
Windows下JDK,Tomcat,Eclipse安装配置
查看>>
vue的checkbox或多选的select的代码例子
查看>>
es6-Set和Map数据结构
查看>>
使用ffmpeg将录屏文件转换成gif
查看>>
作业七 总结
查看>>
Oracle的静默安装 升级和卸载 参考规范
查看>>
高效存储过程分页
查看>>
电脑用U盘启动
查看>>
Web漏洞扫描
查看>>
使用xtrabackup做数据库的增量备份
查看>>
“程序已停止工作”问题的解决方法,停止解决方法
查看>>
[c++] 幂法求特征向量
查看>>
WEB项目(B/S系统)打包安装(总结篇)
查看>>
Cartographer源码阅读(8):imu_tracker
查看>>
U盘,移动硬盘显示显示需要格式化怎么修复
查看>>
JVM基础和调优(一)
查看>>
ICommand in Silverlight
查看>>