HDU-1828-Picture(线段树)
来源:互联网 发布:卷皮网源码7.0 编辑:程序博客网 时间:2024/05/17 08:40
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.
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.
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 2515 -4 24 140 -6 16 42 15 10 2230 10 36 2034 0 40 16
Sample Output
228
Source
IOI 1998
思路:求纵向边要维护重叠次数大于0的纵向长度,用上一次的node[1].m减去当前的node[1].m的绝对值。求横向边要维护每一次更新之后需要加上去的边数。
#include <cstdio>#include <cmath>#include <algorithm>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<b.x;}void build(int idx,int s,int e){ node[idx].cnt=0; node[idx].m=0; node[idx].lc=node[idx].rc=0; node[idx].l=tempy[s-1]; node[idx].r=tempy[e-1]; if(s+1!=e) { int mid=(s+e)>>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<n;i++) { scanf("%d%d%d%d",&x1,&y1,&x2,&y2); tempy[i*2]=y1; tempy[i*2+1]=y2; l[i*2].val=1; l[i*2].x=x1; l[i*2].y1=y1; l[i*2].y2=y2; l[i*2+1].val=-1; l[i*2+1].x=x2; l[i*2+1].y1=y1; l[i*2+1].y2=y2; } sort(tempy,tempy+n*2); sort(l,l+n*2,cmp); t=unique(tempy,tempy+n*2)-tempy;//去重 build(1,1,t); int ans=0,last=0; for(i=0;i<n*2;i++) { if(i) ans+=node[1].n*(l[i].x-l[i-1].x); update(1,1,t,l[i]); ans+=abs(node[1].m-last); last=node[1].m; } printf("%d\n",ans); }}
49 0
- hdu 1828 picture 线段树
- HDU-1828-Picture(线段树)
- hdu 1828 Picture(线段树)
- |Hdu 1828|线段树|Picture
- HDU 1828 Picture(线段树:扫描线)
- hdu 1828 Picture 线段树+扫描线
- poj 1177 & hdu 1828 Picture(线段树+离散化)
- Hdu 1828 Picture 线段树+矩形周长并
- HDU - 1828 Picture (线段树求并周长)
- hdu 1828 Picture(线段树&扫描线&周长并)
- 【HDU】1828-Picture(线段树扫描线)
- hdu 1828 Picture 线段树 矩形周长并
- HDU 1828 Picture(线段树 + 扫描线)
- hdu 1828 Picture (线段树+扫描线)
- HDU 1828 Picture (线段树+扫描线+离散化)
- HDU 1828 Picture(线段树的周长并)
- HUD 1828 Picture 矩形周长/线段树
- hdu 1828 / poj/pku 1177(Picture)(线段树求矩形覆盖面周长)
- ios App上传错误处理
- Python的魅力(Pythonic)及各种“不为人知”的小技巧
- 有关信号量的讨论
- c语言—指针高级和宏定义
- Linux内存管理之一:基本概念篇 .
- HDU-1828-Picture(线段树)
- MyEclipse快捷键大全
- 环境变量中administrator的用户变量和系统变量
- ubuntu下使用haproxy做tcp 层的负载均衡
- spintf用法
- CSS样式中"大于号"
- Swift编程语言学习1.7——断言
- 陇南做流产哪里好
- 为什么要用Linux做开发