HDU-1255 覆盖的面积 线段树 + 扫描线
2013-10-27 20:08
447 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1225
求矩形面积交 主要是扫描线不太懂 纠结了很久
竖向建线段树 横向扫描线
以扫描线扫描出相邻2条竖边的距离 再以线段树求出两竖向边的交集面积高度
tree[1].inlen * ( Line[i].x - Line[i-1].x ); tree[1].len 表示f为1的线段树长度 Line[i].x - Line[i-1].x相邻2边的距离
求矩形面积交 主要是扫描线不太懂 纠结了很久
竖向建线段树 横向扫描线
以扫描线扫描出相邻2条竖边的距离 再以线段树求出两竖向边的交集面积高度
tree[1].inlen * ( Line[i].x - Line[i-1].x ); tree[1].len 表示f为1的线段树长度 Line[i].x - Line[i-1].x相邻2边的距离
#include "stdio.h" #include "algorithm" using namespace std; const int maxn = 2010; int n; double y[maxn]; struct node { double x,y1,y2; //x 边的位置 y边的区间大小 int f; //f 标记 前边还是后边 }Line[maxn]; struct node1 { double y1,y2,len,inlen; // inlen线段被覆盖两次的长度 int ld,rd,c; // c为被覆盖次数 }tree[maxn*4]; bool cmp( node a,node b ) { return a.x < b.x; } void buildtree( int ld,int rd,int t) // 建树 { tree[t].c = 0; tree[t].inlen = 0; tree[t].ld = ld; tree[t].rd = rd; tree[t].y1 = y[ld]; tree[t].y2 = y[rd]; tree[t].len = 0; if( ld+1 == rd ) return; int mid = ( ld+rd )>>1; buildtree( ld,mid,t<<1 ); buildtree( mid,rd,t<<1|1 ); } void calen( int t ) //求线段树被覆盖1次长度 { if( tree[t].c > 0 ) // t区间f是否被覆盖 { tree[t].len = tree[t].y2 - tree[t].y1; return; } if( tree[t].ld +1 == tree[t].rd ) tree[t].len = 0; else tree[t].len = tree[t<<1].len + tree[t<<1|1].len; // 回带len的值 } void incalen(int t) //线段被覆盖两次或两次以上 { if(tree[t].c >= 2) { tree[t].inlen = tree[t].y2 - tree[t].y1; return; } if( tree[t].ld+1 == tree[t].rd ) tree[t].inlen = 0; else if(tree[t].c == 1) { tree[t].inlen = tree[t<<1].len + tree[t<<1|1].len; } else tree[t].inlen = tree[t<<1].inlen + tree[t<<1|1].inlen; } void updata( int t,node e ) { if( e.y2 < tree[t].y1 || e.y1 > tree[t].y2 ) return; if( e.y1 == tree[t].y1 && e.y2 == tree[t].y2 ) //在线段树中找到边e所在的区间 更新f求出长度冷 { tree[t].c += e.f; calen(t); incalen(t); return; } if( e.y2 <= tree[t<<1].y2 ) //如果e全在tree[t]的左半边 updata( t<<1,e ); else if( e.y1 >= tree[t<<1|1].y1 ) //e在tree[t]的右半边 updata( 2*t+1,e ); else if( e.y2 > tree[t<<1|1].y1 && e.y1 < tree[t<<1].y2 ) //一部分在左半边 一部分在右半边 { node temp = e; temp.y2 = tree[t<<1].y2; updata( t<<1,temp ); temp = e; temp.y1 = tree[t<<1|1].y1; updata( t<<1|1,temp ); } calen(t); incalen(t); } int main() { //freopen("data.txt","r",stdin); int i,j,cas,pos; double x1,y1,x2,y2,ans; scanf("%d",&cas); while( cas-- ) { scanf("%d",&n); pos = 0; for( i = 1; i <= n; i ++ ) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); pos++; Line[pos].x = x1; //存前边 Line[pos].y1 = y1; Line[pos].y2 = y2; Line[pos].f = 1; y[pos] = y1; pos++; Line[pos].x = x2; //存后边 Line[pos].y1 = y1; Line[pos].y2 = y2; Line[pos].f = -1; y[pos] = y2; } sort( y+1,y+pos+1); //对横边进行排序 sort( Line+1,Line+pos+1,cmp ); //对竖边进行排序 buildtree( 1,pos,1 ); ans = 0; for( i = 1; i <= pos; i ++ ) //当某一条线段被覆盖两次或两次以上 计算一次面积 { ans += tree[1].inlen * ( Line[i].x - Line[i-1].x ); //tree[1].len 表示f为1的线段树长度 Line[i].x - Line[i-1].x相邻2边的距离 updata( 1,Line[i] ); } printf("%.2lf\n",ans); } return 0; }
相关文章推荐
- hdu 1255 覆盖的面积 (线段树 扫描线)
- HDU 1255 覆盖的面积(线段树+扫描线求面积【升级版】)
- HDU 1255 覆盖的面积 (扫描线 线段树 离散化 矩形面积并)
- HDU - 1255 覆盖的面积(线段树 + 扫描线)
- HDU 1255 覆盖的面积 线段树 扫描线
- hdu 1255 覆盖的面积 线段树扫描线求重叠面积
- HDU 1255 - 覆盖的面积 (线段树 扫描线 面积交)
- HDU-1255 覆盖的面积(线段树扫描线模板+离散化+加点修改题)
- HDU 1255 覆盖的面积 线段树+扫描线求面积并
- 线段树 扫描线 HDU 1255 覆盖的面积
- HDU 1255 覆盖的面积 (线段树 + 离散化 + 扫描线)
- hdu 1255 覆盖的面积 (线段树 扫描线)
- 覆盖的面积 HDU - 1255 线段树-扫描线
- hdu 1255 覆盖的面积(线段树|扫描线)
- HDU - 1255 覆盖的面积(线段树 扫描线)
- hdu 1255 覆盖的面积 (线段树扫描线 计算两次覆盖的面积)
- HDU 1255 覆盖的面积[离散化 + 扫描线 + 线段树]
- HDU 1255 覆盖的面积(线段树扫描线求面积的交)
- HDU 1255 覆盖的面积[离散化 + 扫描线 + 线段树]
- 【线段树 && 扫描线 && 面积交】HDU - 1255 覆盖的面积