【线段树 && 扫描线 && 周长】POJ - 1177 Picture
2017-09-05 11:12
453 查看
Problem Description
给你n个矩阵,每个矩阵给你左下角和右上角的下标。让你求所有矩阵并起来后的周长。
思路:
建议先做了hdu 1542再看这题因为这题线段树维护的量没那么多。比较好入门,学习扫描线什么的。这样的话,代码也是在hdu 1542的基础上改动改动而已。线段树维护有效长度len,区间整段是否有效的cover,的基础上多了 ly, lr, num这三个分别代表该区间最左边的下标是否被有效长度覆盖, 同理最右边的下标是否被有效长度覆盖, 该区间有多少段不连续的线段。 假设len为底,num的目的是让你知道有多少条高,num = 1的时候有两条,lastlen用来记录上一次的有效长度,最开始是0。
给你n个矩阵,每个矩阵给你左下角和右上角的下标。让你求所有矩阵并起来后的周长。
思路:
建议先做了hdu 1542再看这题因为这题线段树维护的量没那么多。比较好入门,学习扫描线什么的。这样的话,代码也是在hdu 1542的基础上改动改动而已。线段树维护有效长度len,区间整段是否有效的cover,的基础上多了 ly, lr, num这三个分别代表该区间最左边的下标是否被有效长度覆盖, 同理最右边的下标是否被有效长度覆盖, 该区间有多少段不连续的线段。 假设len为底,num的目的是让你知道有多少条高,num = 1的时候有两条,lastlen用来记录上一次的有效长度,最开始是0。
#include<cstdio> #include<algorithm> #include<cmath> using namespace std; #define lson root<<1 #define rson root<<1|1 #define N 5005 #define MID int mid = (l + r) / 2 struct node { int cover; int ly, ry, num; int len; }; node tree[N<<3]; int yy[N<<2]; struct Node { int cover; int x; int yd, yu; bool operator < (const Node &b) const{ return x < b.x; } }; Node a[N<<2]; void build(int root, int l, int r)//初始化 { tree[root].cover = tree[root].len = tree[root].ly = tree[root].ry = tree[root].num = 0;//全都没满足都为0 if(l + 1 == r) return; MID; build(lson, l, mid); build(rson, mid, r); } void get_len(int root, int l, int r) { if(tree[root].cover)//区间整段都是有效 { tree[root].len = yy[r] - yy[l];//求有效长度 tree[root].ly = 1; tree[root].ry = 1;//左右端点被有效长度覆盖 tree[root].num = 1;//该区间的num为1 } else if(l + 1 == r)//叶子节点,同时整段不是有效 { tree[root].len = 0;//所以全为0 tree[root].num = tree[root].ly = tree[root].ry = 0; } else//这时候得看左右儿子 { tree[root].len = tree[lson].len + tree[rson].len;//更新有效长度 tree[root].ly = tree[lson].ly; tree[root].ry = tree[rson].ry;//更新ly,ry tree[root].num = tree[lson].num + tree[rson].num - (tree[lson].ry & tree[rson].ly);//更新num } } void updata(int root, int l, int r, int ul, int ur, int cover) { if(ul <= l && r <= ur)//更新cover,len,ly,ry,num { tree[root].cover += cover; get_len(root, l, r); return; } MID; if(ul < mid) updata(lson, l, mid, ul, ur, cover); if(ur > mid) updata(rson, mid, r, ul, ur, cover); get_len(root, l, r);//回溯更新cover,len,ly,ry,num } int main() { int n, cnt, i; int x1, x2, y1, y2; while(~scanf("%d", &n) && n) { cnt = 0; for(i = 0; i < n; i++) { scanf("%d %d %d %d", &x1, &y1, &x2, &y2); yy[cnt] = y1; a[cnt++] = ((Node){1, x1, y1, y2}); yy[cnt] = y2; a[cnt++] = ((Node){-1, x2, y1, y2}); } sort(yy, yy + cnt); n = unique(yy, yy + cnt) - yy; build(1, 0, n - 1); sort(a, a + cnt); int ul, ur; int ans = 0;//最后总的周长 int lastlen = 0;//初始化为0 for(i = 0; i < cnt; i++) { ul = lower_bound(yy, yy + n, a[i].yd) - yy; ur = lower_bound(yy, yy + n, a[i].yu) - yy; updata(1, 0, n - 1, ul, ur, a[i].cover);//更新 ans += abs(tree[1].len - lastlen);//有效长度变化 lastlen = tree[1].len;//记录上一次的有效长度 if(i < cnt - 1)//有几条高 ans += (a[i + 1].x - a[i].x) * tree[1].num * 2; } printf("%d\n", ans); } }
相关文章推荐
- poj 1177 & hdu 1828 Picture 线段树 扫描线求矩形周长并
- 【线段树+扫描线】 HDOJ 1828 && POJ 1177 Picture
- poj 1177 Picture 【线段树 扫描线 求轮廓周长】
- poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)
- POJ 1177 Picture(扫描线 + 线段树 矩形覆盖区域周长的并)
- POJ 1177 & HDU 1828 Picture(扫描线 + 求周长)
- poj 1177 || HDU 1828 Picture (线段树扫描线求 图形并的周长)
- hdu 1828、poj1177求矩形周长并 线段树 扫描线
- POJ-1177-Picture(线段树+扫描线+离散化)[矩形周长并]
- poj_1151 Atlantis && poj_1177 Picture(线段树+扫描线+离散化)
- hdu 1828 / poj/pku 1177(Picture)(线段树求矩形覆盖面周长)
- POJ - 1177 Picture (线段树 + 扫描线)
- Picture - POJ 1177 扫描线 矩阵合并的周长
- POJ - 1177/HDU - 1828 Picture(线段树-矩形并周长)
- POJ-1177 Picture (线段树 求矩形周长)
- poj 1177 Picture(线段树+矩形周长并)
- poj&nbsp;1177&nbsp;Picture&nbsp;线段树加扫描线
- poj 1177 & hdu 1828 Picture(线段树+离散化)
- POJ 1177:Picture(线段树-扫描线)
- Picture POJ - 1177 扫描线+线段树求轮廓线长度