POJ 1177 Picture

题目大意:矩形的周长并(够大意了~)

解题思路:

扫描线,跟面积并一样,只不过处理周长的时候注意边的增加与减少。

下面是代码:

#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>

#define eps 1e-8
#define pi acos(-1.0)
#define inf 107374182
#define inf64 1152921504606846976
#define lc l,m,tr<<1
#define rc m + 1,r,tr<<1|1
#define iabs(x)  ((x) > 0 ? (x) : -(x))
#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE))
#define clearall(A, X) memset(A, X, sizeof(A))
#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))
#define memcopyall(A, X) memcpy(A , X ,sizeof(X))
#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )
#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )

using namespace std;

struct node2
{
    int num;
    int y,l,r;
} edge[10005];

int tempx[10005],binx[10005];
int x1,x2,y1,y2,ans;
int cntx,n;

bool cmp(node2 a,node2 b)
{
    if(a.y==b.y)return a.num<b.num;
    return a.y<b.y;
}

int binnum(int num)
{
    int ll=0,m,rr=cntx-1;
    while(rr>ll)
    {
        m=(ll+rr)>>1;
        if(binx[m]==num)return m;
        else if(binx[m]<num)ll=m+1;
        else rr=m-1;
    }
    return ll;
}

struct node1
{
    int disnow;
    int cnt,segcnt;
    bool sta1,sta2;
} node[20000<<2];

inline void PushUp(int l,int r,int tr)
{
    if(node[tr].cnt)
    {
        node[tr].sta1=true;
        node[tr].sta2=true;
        node[tr].segcnt=1;
        node[tr].disnow=binx[r+1]-binx[l];
    }
    else if(l==r)
    {
        node[tr].disnow=0;
        node[tr].segcnt=0;
        node[tr].sta1=false;
        node[tr].sta2=false;
    }
    else
    {
        node[tr].disnow=node[tr<<1].disnow+node[tr<<1|1].disnow;

        node[tr].sta1=node[tr<<1].sta1;
        node[tr].sta2=node[tr<<1|1].sta2;
        node[tr].segcnt=node[tr<<1].segcnt+node[tr<<1|1].segcnt;
        if(node[tr<<1].sta2&&node[tr<<1|1].sta1)
        {
            node[tr].segcnt--;
        }
    }
}

void update(int L,int R,int num,int l,int r,int tr)
{
    if(L<=l&&r<=R)
    {
        node[tr].cnt+=num;
        PushUp(l,r,tr);
        return ;
    }
    int m=(l+r)>>1;
    if(L<=m)update(L,R,num,l,m,tr<<1);
    if(m<R)update(L,R,num,m+1,r,tr<<1|1);
    PushUp(l,r,tr);
}
int main()
{
    int case1=1,sta;
    int temp;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            tempx[2*i]=x1;
            tempx[2*i+1]=x2;
            edge[2*i].l=x1;
            edge[2*i].r=x2;
            edge[2*i+1]=edge[2*i];
            edge[2*i].y=y1;
            edge[2*i+1].y=y2;
            edge[2*i].num=1;
            edge[2*i+1].num=-1;
        }
        sort(tempx,tempx+2*n);
        binx[0]=tempx[0];
        cntx=1;
        for(int i=1; i<2*n; i++)
        {
            if(tempx[i]!=binx[cntx-1])
            {
                binx[cntx++]=tempx[i];
            }
        }
        sort(edge,edge+2*n,cmp);
        clearall(node,0);
        x1=binnum(edge[0].l);
        x2=binnum(edge[0].r);
        x2--;
        update(x1,x2,edge[0].num,0,cntx-2,1);
        y1=edge[0].y;
        sta=edge[0].num;
        ans=0;
        temp=0;
        for(int i=1; i<2*n; i++)
        {
            if(y1!=edge[i].y||sta!=edge[i].num)
            {
                ans+=iabs(node[1].disnow-temp);
                temp=node[1].disnow;
                sta=edge[i].num;
                if(y1!=edge[i].y)
                {
                    ans+=(edge[i].y-y1)*2*node[1].segcnt;
                    y1=edge[i].y;
                }
            }
            x1=binnum(edge[i].l);
            x2=binnum(edge[i].r);
            x2--;
            update(x1,x2,edge[i].num,0,cntx-2,1);
        }
        ans+=temp;
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-07-28 17:12:08

POJ 1177 Picture的相关文章

hdu1828 (Picture) &amp;poj 1177( Picture)&amp;sdibt 2390(5.5.1 Picture 矩形周长)(线段树+扫描)

题目地址:hdu1828 (Picture)  & poj 1177( Picture) & sdibt 2390(5.5.1 Picture 矩形周长) 题目大意: 给你N个矩形,求出N个矩形构成的周长. 解题思路: 线段数+扫描线. 推荐博客: POJ1177 Picture(线段树求轮廓周长) POJ 1177 Picture (线段树+离散化+扫描线) 详解 注意事项: 该题在求面积的基础上有了升级,多写了一个被调需要求出投影与Y轴有几段,不然样例会少算20,就是给出图形中间的小矩

POJ 1177 Picture(扫描线求周长)

与求面积并的差不多,但是这个与扫描的方向相同的情况不太好处理,如果扫描线离散化两次扫两遍其实也可以解决这个问题,但是这样无论在时间还是空间上稍微就有点浪费了啊.这里因为我是离散x坐标的所以对于平行于y轴的方向上的统计比较难统计.处理的方法是:标记区间左边的断点,和右边的断点,求出这个区间一共有多少个断点.就可以统计出平行于y轴的长度了.这里合并的时候需要判断右边的左区间和左边的右区间是否相同,如果相同的话,说明他们连接到了一起,要减去多加的. Picture Time Limit: 2000MS

poj 1177 Picture(扫描线+矩形周长并)

http://poj.org/problem?id=1177 求矩形的周长并,明确的一点是对于覆盖的边的长度忽略不计. 与求面积并类似,首先离散化,对矩形的每条横边从下往上扫描.扫描过程中要完成三个任务,更新相应的区间信息,求横边长,求竖边长. 节点信息: l,r:左右区间编号 cnt:表示该区间是否被完全覆盖.cnt > 0 表示完全覆盖,否则不完全覆盖. lp,rp:表示该区间的两个端点是否被覆盖,为1被覆盖,为0没被覆盖. num:该区间被覆盖的线段数目.例如区间[1,10],当更新[2,

poj 1177 Picture(线段树周长并)

题目链接:http://poj.org/problem?id=1177 题意:给你n个矩形问你重叠后外边缘总共多长. 周长并与面积并很像只不过是处理的时候是   增加的周长=abs(上一次的线段的长度-更新后线段的长度) 然后分别处理一下竖着的边和横着的边就好了即建两次树就好. 就是一道典型的周长并问题,可以拿来练练周长并的写法. #include <iostream> #include <cstring> #include <algorithm> #include &

poj 1177 Picture 线段树辅助解扫描线 矩形周长并

题目链接:http://poj.org/problem?id=1177 分析:对扫描线不太理解~~~ 第一题~~ 留个坑. 代码: /* *********************************************** Author :ltwy Created Time :2015年01月17日 星期六 18时06分53秒 File Name :1.cpp ************************************************ */ #include <s

HDU 1828 / POJ 1177 Picture --线段树求矩形周长并

题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 num: 此区间分离开的线段的条数 重点在转移的地方,不难理解. 代码: #include <iostream> #include <cmath> #include <iostream> #include <cstdio> #include <cstrin

POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的,这样理论上是对的 要注意处理高度相同的线段,把底边优先处理(在代码里就是f标记为1的线段),因为若是一个矩形的底边和另一个矩形的上边重合,则这个轮廓肯定不能算 不过POJ和HDU的数据好像都比较弱,我没进行上面的细节处理也AC了,不过一个很简单的数据就会不对,所以还是要处理一下才是真正正确的代码 我

POJ 1177 &amp; HDU 1828 Picture(扫描线 + 求周长)

题目链接: POJ:http://poj.org/problem?id=1177 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1828 几个不错的讲解: http://www.cnblogs.com/scau20110726/archive/2013/04/13/3018702.html http://blog.csdn.net/xingyeyongheng/article/details/8931410 Description A number

N - Picture - poj 1177(扫描线求周长)

题意:求周长的,把矩形先进行融合后的周长,包括内周长 分析:刚看的时候感觉会跟棘手,让人无从下手,不过学过扫描线之后相信就很简单了吧(扫描线的模板- -),还是不说了,下面是一精确图,可以拿来调试数据 ***************************************************************************************************************** #include<stdio.h>#include<math.