hdu1255(线段树求面积交)
来源:互联网 发布:淘宝电棍 编辑:程序博客网 时间:2024/05/17 01:48
和Poj1151线段树求面积并的原理相同
不同的地方:至少覆盖一次one和至少覆盖两次two的更新
注意点:
1、由于没有push_down的操作,当父节点的cover==1,子节点的cover也等于1,则父节点的two要更新
当父节点的over==0,子节点的cover=1,父节点的one要更新
子节点的cover=2,父节点的two要更新
2、由于是左闭有开区间,而我们记录的是线段,当l+1==r时注意返回
代码
#pragma warning(disable:4996)#include <stdio.h>#include <algorithm>using namespace std;const int maxn = 1005 * 2;struct node{int l, r, cover;double ly, ry, one, two;}segTree[maxn * 3];struct line{double x, y1, y2;int tag;bool operator < (const line& l)const {return x<l.x;}}lines[maxn];double y[maxn];void build(int i, int l, int r){segTree[i].l = l;segTree[i].r = r;segTree[i].cover = 0;segTree[i].one = 0;segTree[i].two = 0;segTree[i].ly = y[l];segTree[i].ry = y[r];if (l + 1 == r)return;int mid = (l + r) / 2;build(i << 1, l, mid);build(i << 1 | 1, mid, r);}void cal(int i){if (segTree[i].cover >= 2){segTree[i].two = segTree[i].ry - segTree[i].ly;}else if (segTree[i].cover == 1){segTree[i].one = segTree[i].ry - segTree[i].ly;if (segTree[i].l + 1 == segTree[i].r)segTree[i].two = 0;else segTree[i].two = segTree[i << 1].one + segTree[i << 1 | 1].one;}else{if (segTree[i].l + 1 == segTree[i].r){segTree[i].one = segTree[i].two = 0;}else{segTree[i].one = segTree[i << 1].one + segTree[(i << 1) | 1].one;segTree[i].two = segTree[i << 1].two + segTree[(i << 1) | 1].two;}}}void update(int i, line&l){if (l.y1 == segTree[i].ly&&l.y2 == segTree[i].ry){segTree[i].cover += l.tag;cal(i);return;}if (l.y2 <= segTree[i << 1].ry)update(i << 1, l);else if (l.y1 >= segTree[i << 1 | 1].ly)update(i << 1 | 1, l);else{double tmpy = l.y2;l.y2 = segTree[i << 1].ry;update(i << 1, l);l.y2 = tmpy;tmpy = l.y1;l.y1 = segTree[i << 1 | 1].ly;update(i << 1 | 1, l);l.y1 = tmpy;}cal(i);}int main(){//freopen("in.txt", "r", stdin);int n, T, tot ;double x1, y1, x2, y2;scanf("%d", &T);while (T--){tot = 1;scanf("%d", &n);for (int i = 0; i<n; i++){scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);lines[tot].x = x1;lines[tot].y1 = y1;lines[tot].y2 = y2;lines[tot].tag = 1;y[tot] = y1;tot++;lines[tot].x = x2;lines[tot].y1 = y1;lines[tot].y2 = y2;lines[tot].tag = -1;y[tot] = y2;tot++;}sort(lines + 1, lines + tot);sort(y + 1, y + tot);build(1, 1, tot - 1);update(1, lines[1]);double area = 0.0;for (int i = 2; i<tot; i++){area += segTree[1].two*(lines[i].x - lines[i - 1].x);update(1, lines[i]);}printf("%.2lf\n", area);}return 0;}
0 0
- hdu1255(线段树求面积交)
- HDU1255 覆盖的面积 (线段树求面积交)
- HDU1255 求面积交 线段树+离散化
- 【HDU1255】覆盖的面积【线段树】【矩形面积交】
- HDU1255-覆盖的面积(线段树+扫描线——面积交)
- 【hdu1255】覆盖的面积(矩形面积交+线段树+扫描线)
- HDU1255 覆盖的面积(线段树,离散化,扫描线,矩形面积交)
- hdu1255 覆盖的面积(线段树+扫描线+离散化,求矩形面积并)
- 线段树 hdu1255 覆盖的面积
- 【HDU1255】【线段树】【扫描线】【面积】
- hdu1255 矩形面积二次交
- 线段树求周长求交面积的做法
- hdu 1255 线段树 求矩形面积的交
- 矩形面积交-线段树
- 线段树——HDU1255 覆盖的面积解题报告
- hdu1255 覆盖的面积 扫描线+线段树+离散化
- hdu1255--覆盖的面积(线段树+离散化+扫描线)
- 覆盖的面积 hdu1255 扫描线+线段树
- android模拟器,Genymotion问题:Unable to start the virtual device.
- unity 打包完成之后 (黑色控制台提示)
- 二叉树练习——建树+叶子数+深度
- 渗透工具之nmap使用
- Java回顾之序列化
- hdu1255(线段树求面积交)
- Perl语言入门(06+ 正则表达式)——小骆驼书正则表达式相关章节笔记
- ES 5.0.2报错记录
- 【数据结构1】顺序表
- Android设计模式——接口隔离原则
- 新人常识普及:我们为什么必须会git和maven
- Android设计模式——迪米特原则
- 关于didFinishLaunchingWithOptions参数的中的launchOptions这个参数
- Scrapy爬虫(十):爬虫总结以及扩展