覆盖的面积 HDU
来源:互联网 发布:2钻淘宝店铺转让 编辑:程序博客网 时间:2024/05/16 14:25
题意:
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.
输入:
输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.
注意:本题的输入数据较多,推荐使用scanf读入数据.
输出:
对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.
这里给出大神的解释,很容易懂:HUD-1255 O_O
主要是要注意cover = 1时的情况。刚开始我把cover的情况也直接算作 node[rt<<1].len + node[rt<<1|1].len了;但是这样其实会少算面积,因为cover = 1时,这部分面积被覆盖一次,但是这个节点的儿子的cover并没有加(线段树的特性),所以如果讲cover = 1当作cover = 0时的情况来看的话,这样的话实际上这部分的 cover =1 相当于没有加上,因为这部分的cover+1后就return了,对子节点并没有影响,只是让cover加了1。所以,如果这时候没对cover = 1的情况进行特判的话,相当与这部分就白覆盖了!~
代码:
#include<bits/stdc++.h>using namespace std;const int MAX_N = 2e3+9;double y[MAX_N];struct Line{ double x,y1,y2; // y1 < y2 int flag;}line[MAX_N<<2];struct Node{ int l,r,cover; double lf,rf,len1,len2;}node[MAX_N<<2];bool cmp(const Line & A,const Line & B){ return A.x < B.x;}void build(int rt,int l,int r){ node[rt].l = l; node[rt].r = r; node[rt].lf = y[l]; node[rt].rf = y[r]; node[rt].len1 = node[rt].len2 = node[rt].cover = 0; if(l+1 == r) return ; int mid = (l+r) >> 1; build(rt<<1,l,mid); build(rt<<1|1,mid,r);}void push_up(int rt){ /*覆盖一次的长度处理*/ if(node[rt].cover) { node[rt].len1 = node[rt].rf - node[rt].lf; } else if(node[rt].l+1==node[rt].r) { node[rt].len1 = 0; } else { node[rt].len1 = node[rt<<1].len1 + node[rt<<1|1].len1; } /*覆盖两次的长度处理*/ if(node[rt].cover > 1) { node[rt].len2 = node[rt].rf - node[rt].lf; return; } else if(node[rt].l+1 == node[rt].r) { node[rt].len2 = 0; } else if(node[rt].cover == 1) { node[rt].len2 = node[rt<<1].len1 + node[rt<<1|1].len1; } else { node[rt].len2 = node[rt<<1].len2 + node[rt<<1|1].len2; }}void updata(int rt,Line t){ if(t.y1 == node[rt].lf && t.y2 == node[rt].rf) { node[rt].cover += t.flag; push_up(rt); return; } if(t.y1 >= node[rt<<1|1].lf) { updata(rt<<1|1 , t); } else if(t.y2 <= node[rt<<1].rf) { updata(rt<<1 , t); } else { Line temp = t; temp.y2 = node[rt<<1].rf; updata(rt<<1 , temp); temp = t; temp.y1 = node[rt<<1|1].lf; updata(rt<<1|1 , temp); } push_up(rt);}int main(){ int N,M,T; cin>>T; while(T--) { scanf("%d",&N); int cnt = 0; for(int i=0;i<N;i++) { double x1,y1,x2,y2; scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); line[cnt].x = x1; line[cnt].y1 = y1; line[cnt].y2 = y2; line[cnt].flag = 1; y[cnt++] = y1; line[cnt].x = x2; line[cnt].y1 = y1; line[cnt].y2 = y2; line[cnt].flag = -1; y[cnt++] = y2; } sort(line,line+cnt,cmp); sort(y,y+cnt); build(1,0,cnt-1); updata(1,line[0]); double ans = 0; for(int i=1;i<cnt;i++) { ans += node[1].len2*(line[i].x - line[i-1].x); updata(1,line[i]); } printf("%.2lf\n",ans); } return 0;}
阅读全文
0 0
- 覆盖的面积 HDU
- 覆盖的面积 HDU
- 覆盖的面积 HDU
- 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 覆盖的面积
- eclipse安装swing插件
- linq to sql
- mysql读取数据不全问题
- OpenCV Python 学习笔记(五) 人脸识别
- ubuntu16.04 系统初始配置
- 覆盖的面积 HDU
- (试题)使用两个栈实现一个队列
- 微信内置浏览器中使用一键打电话功能
- 闲来无事的晚上
- 数组
- java虚拟机简介
- logback
- java.lang.SecurityException: Unknown calling package name, com.google.android.gms.common.internal.zz
- 微信支付之H5支付