hdu 1828 Picture 线段树+离散化(其区间的个数)
来源:互联网 发布:小米生活和淘宝的关系 编辑:程序博客网 时间:2024/06/06 07:12
题意:
给定n个矩形,求这些矩形所构成图形的周长。
题解:
跟hdu1542很像,那道题是求构成图形的面积,我一开始也以为是面积,最后求了之后才发现时周长。不过那题的代码还是有用的,这题相当于将那道题代码的二倍化。
我们先离散化矩形各条竖直线的y坐标,然后根据x坐标排序,我们可以发现相邻的x坐标间的图形是一个或者多个矩形,那么图形的上下方向上的周长怎么求呢?我们很容易想到ans=∑(xi-xj)*w*2(其中j=i-1,而w表示被两个横坐标分割区域的矩形数目),那么我们就能转换下,每次碰到左边的时候维护线段树[yi,yj](这条边所覆盖的离散化区间)区域加一,碰到右边的时候减一。那么矩形个数就相当于线段树上非0段的个数了。同理,我们将x坐标离散化,排序y坐标可以求得左右方向上的周长。
线段树结点介绍:
p表示该段区间左边是否有非0数,q表示该段区间右边是否有非0数,w表示该段区间的非0段个数,cov表示覆盖情况。
代码:
#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <iostream>#include <algorithm>#include <vector>#include <map>#include <queue>#include <stack>using namespace std;const int maxn=1e4+10;struct node{ int l,r,cov; int p,q,w;}e[maxn*4];struct segment{ int x,up,down; int cov;}f[maxn],g[maxn];int y[maxn],x[maxn];int cmp(segment a,segment b){ return a.x<b.x;}void build(int a,int b,int c){ e[c].l=a;e[c].r=b; e[c].w=e[c].p=e[c].q=e[c].cov=0; if(a==b)return ; int mid=(a+b)/2; build(a,mid,2*c); build(mid+1,b,2*c+1);}void push_up(int c){ if(e[2*c].q==1&&e[2*c+1].p==1)e[c].w=e[2*c].w+e[2*c+1].w-1; else e[c].w=e[2*c].w+e[2*c+1].w; e[c].p=e[2*c].p;e[c].q=e[2*c+1].q;}void update(int a,int b,int c,int val){ //printf("%d\n",c); if(e[c].l==a&&e[c].r==b) { e[c].cov+=val; if(e[c].cov!=0)e[c].w=e[c].p=e[c].q=1; else { if(e[c].l==e[c].r)e[c].w=e[c].p=e[c].q=0; else push_up(c); } return ; } int mid=(e[c].l+e[c].r)/2; if(b<=mid)update(a,b,2*c,val); else if(a>mid)update(a,b,2*c+1,val); else { update(a,mid,2*c,val); update(mid+1,b,2*c+1,val); } if(e[c].cov==0) push_up(c);}int main(){ int n,tt=0; while(scanf("%d",&n)!=EOF) { if(n==0)break; int i,j,k,t=0,tt=0,l,r; int x1,y1,x2,y2; for(i=0;i<n;i++) { scanf("%d%d%d%d",&x1,&y1,&x2,&y2); y[++t]=y1;f[t].x=x1;f[t].up=y2;f[t].down=y1,f[t].cov=1; y[++t]=y2;f[t].x=x2;f[t].up=y2;f[t].down=y1,f[t].cov=-1; x[++tt]=x1;g[tt].x=y1;g[tt].up=x2;g[tt].down=x1;g[tt].cov=1; x[++tt]=x2;g[tt].x=y2;g[tt].up=x2;g[tt].down=x1;g[tt].cov=-1; } int ans=0; sort(y+1,y+t+1); sort(f+1,f+t+1,cmp); build(1,t,1); for(i=1;i<=t;i++) { //printf("%d:%d\n",i,e[1].w); ans+=(f[i].x-f[i-1].x)*e[1].w*2; l=lower_bound(y+1,y+t+1,f[i].down)-y; r=lower_bound(y+1,y+t+1,f[i].up)-y-1; update(l,r,1,f[i].cov); } //printf("%d\n",ans); sort(x+1,x+tt+1); sort(g+1,g+tt+1,cmp); build(1,tt,1); for(i=1;i<=tt;i++) { //printf("%d:%d\n",i,e[1].w); ans+=(g[i].x-g[i-1].x)*e[1].w*2; l=lower_bound(x+1,x+tt+1,g[i].down)-x; r=lower_bound(x+1,x+tt+1,g[i].up)-x-1; update(l,r,1,g[i].cov); } printf("%d\n",ans); } return 0;}
0 0
- hdu 1828 Picture 线段树+离散化(其区间的个数)
- HDU 1828 Picture (线段树+扫描线+离散化)
- poj 1177 & hdu 1828 Picture(线段树+离散化)
- HDU 1828 && POJ 1177 Picture(线段树+扫描线+离散化)
- 【NOI2016】区间(线段树+离散化)
- POJ 1177-Picture(线段树+离散化+扫描线)
- 【HDOJ】 Picture (离散化+线段树)
- HDU 1828 Picture(线段树的周长并)
- hdu 1828 picture 线段树
- HDU-1828-Picture(线段树)
- hdu 1828 Picture(线段树)
- |Hdu 1828|线段树|Picture
- hdu 1199 Color the Ball (线段树区间合并,离散化)
- hdu 1199 Color the ball(线段树离散化区间染色)
- HDU 1828 Picture(矩形周长的并+扫描线+离散化)
- HDU 1255 覆盖的面积(离散化+线段树)
- HDU 1828 Picture (线段树扫描线求周长并 区间合并)
- hdu 1255(线段树+离散化)
- [2767]翻转排序 sdutOJ
- 线程传递参数
- GDB高级调试
- hdoj 1070 milk
- 解决拍照按钮为英文问题
- hdu 1828 Picture 线段树+离散化(其区间的个数)
- mininet+FlowVisor+OpenDayLight环境搭建及实验一
- CodeBlocks的常用快捷键
- 【辗转相除法求最大公约数】
- ZOJ-1274
- hdu 1011 Starship Troopers(树形动态规划)
- hdu 3657最大点权独立集变形(方格取数变形)
- poj 2155 Matrix (二维树状数组)
- 让你提前认识软件开发(44):如何解决软件故障?