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

*****************************************************************************************************************

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std; #define Lson r<<1
#define Rson r<<1|1 const int MAXN = 2e5+;
const int oo = 1e9+; struct stgmentTree
{///len 保存区间包含的边的长度,cover 保存区间被覆盖的次数, sum保存有多少个不相连的区间段
    int L, R, len, cover, sum;
    bool lb, rb;///左边和右边是否被覆盖
    int mid(){return (R+L)>>;}
}a[MAXN<<];
///dir 等于 1 的时候代表左边,-1时候代表右边,右边会抵消左边
struct Point{int x, y1, y2, dir;}ege[MAXN];
int Hash[MAXN], nh;///保存离散化后的数据,nh表示元素个数 bool cmp(Point n1, Point n2)
{///把边按照x的值从左往右排序
    return n1.x < n2.x;
}
///查找一条边的长度, x1 < x2
int findSegLen(int x1, int x2)
{///用下标可以直接检索值
    return Hash[x2] - Hash[x1];
}
void buildTree(int r, int L, int R)
{
    a[r].L = L, a[r].R = R, a[r].cover=;     if(L == R-)return ;     buildTree(Lson, L, a[r].mid());
    buildTree(Rson, a[r].mid(), R);
}
void pushUp(int r)///合并操作
{///需要先注意本区间是否被覆盖
    if(a[r].cover != )
    {
        a[r].len= findSegLen( a[r].L, a[r].R );
        a[r].sum = a[r].lb = a[r].rb = ;
    }
    else if(a[r].L == a[r].R - )
    {
        a[r].len = ;
        a[r].sum = a[r].lb = a[r].rb = ;
    }
    else
    {///如果本区间未被覆盖,并且不为叶子节点,那么就等于子区间的和
        a[r].len = a[Lson].len + a[Rson].len;         a[r].lb = a[Lson].lb, a[r].rb = a[Rson].rb;
        a[r].sum = a[Lson].sum + a[Rson].sum - (a[Lson].rb & a[Rson].lb);
    } }
void upData(int r, int L, int R, int dir)
{
    if( a[r].L == L && a[r].R == R )
    {
        a[r].cover += dir;
        pushUp(r);         return ;
    }     if(R <= a[r].mid())
        upData(Lson, L, R, dir);
    else if(L >= a[r].mid())
        upData(Rson, L, R, dir);
    else
    {
        upData(Lson, L, a[r].mid(), dir);
        upData(Rson, a[r].mid(), R, dir);
    }     pushUp(r);
} int main()
{
    int N;     while(scanf("%d", &N) != EOF)
    {
        int i, x1, x2, y1, y2, k=, C=, pre=; nh = ;         for(i=; i<N; i++)
        {
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            ege[k].x=x1, ege[k].y1=y1, ege[k].y2=y2, ege[k++].dir=;///左边y
            ege[k].x=x2, ege[k].y1=y1, ege[k].y2=y2, ege[k++].dir=-;///右边y
            Hash[nh++] = y1, Hash[nh++] = y2;
        }         sort(Hash, Hash+nh);///排序去重复
        nh = unique(Hash, Hash+nh) - Hash;
        ///离散化后的数据是从0下标开始的
        buildTree(, , nh-);         ///对边按照x排序
        sort(ege, ege+k, cmp);         for(i=; i<k-; i++)
        {
            int L = lower_bound(Hash, Hash+nh, ege[i].y1) - Hash;
            int R = lower_bound(Hash, Hash+nh, ege[i].y2) - Hash;             upData(, L, R, ege[i].dir);             C += fabs(a[].len - pre) + (ege[i+].x - ege[i].x)*a[].sum * ;
            pre = a[].len;
        }         printf("%d\n", C+pre);
    }     return ; } 

N - Picture - poj 1177(扫描线求周长)的更多相关文章

  1. Picture POJ - 1177 (扫描线)

    扫描线求周长,可以看成两条线,一条扫x轴,一条扫y轴,然后这两天线扫过去的 周长加起来,就是周长了 #include<map> #include<set> #include&l ...

  2. poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)

    题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ...

  3. hdu1828 Picture(线段树+扫描线+矩形周长)

    看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积))  解法一·:两次扫描线 如图我们可以 ...

  4. POJ1177(扫描线求周长并)

    题意:..求周长并... 解析:参考求面积并 图借鉴自:https://www.cnblogs.com/shuaiwhu/archive/2012/04/22/2464876.html 自下而上扫描 ...

  5. 求矩形的周长(线段树+扫描线) Picture POJ - 1177

    题目链接:https://cn.vjudge.net/problem/POJ-1177 题目大意:求矩形外部的周长 具体思路:借用一下bin巨的一张图片. 我们按照y周从下往上的扫描线进行扫描,第一下 ...

  6. Picture POJ - 1177 线段树+离散化+扫描线 求交叉图像周长

    参考  https://www.cnblogs.com/null00/archive/2012/04/22/2464876.html #include <stdio.h> #include ...

  7. Picture POJ - 1177(扫描线求面积并)

    题意:求矩形并的面积.. 解析: 扫描线第一道题....自下而上扫描的... 如果不懂什么是扫描线戳我 #include <iostream> #include <cstdio> ...

  8. HDU 1828 Picture(线段树扫描线求周长)

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  9. Picture POJ - 1177 (线段树-扫描线)

    A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wa ...

随机推荐

  1. Linux设备驱动中的阻塞和非阻塞I/O

    [基本概念] 1.阻塞 阻塞操作是指在执行设备操作时,托不能获得资源,则挂起进程直到满足操作所需的条件后再进行操作.被挂起的进程进入休眠状态(不占用cpu资源),从调度器的运行队列转移到等待队列,直到 ...

  2. Linux控制台下的快捷键

    Linux控制台(文本模式)下提高工作效率的快捷键 在Linux环境里,有一些按键有特殊的含意.# Ctrl-U: 擦除一行光标前面的部分.# Ctrl-H: 擦除光标前面的一个字符.# Ctrl-D ...

  3. yii criteria select column as 与 时间段查询

    需要查询某时间段的记录,但是数据库里只有一个时间记录,如果写sql的话,很快的,放到yii里一时竟然没办法... 不过,最后还是解决了,使用了一个第三方的插件 参考http://www.yiifram ...

  4. JAVA通过url获取页面内容

    String address = "http://sports.sina.com.cn/nba/live.html?id=2015050405"; URL url = new UR ...

  5. -webkit-appearance改变任何元素的浏览器默认风格

    前段时间,公司有个紧急发布会,需要在移动端做一个邀请函的页面.但是在实现下拉框的时候,IOS和安卓展示的效果总是不一样.经过我一番查找,偶然间发现了-webkit-appearance这个样式属性.后 ...

  6. css文件和js文件后面带一个问号

    经常看一些网站页面源代码中的css文件和js文件后面带一个问号,后面跟着一连串数字或字符,这是干什么用的? 这个方法我也用过,而且很好用?,它的作用有两个:1.作为版本号,让自己方便记忆.查找:2.作 ...

  7. Swift函数的定义建议

    /* Swift中函数命名的智慧 */ // 1.一般情况下, 我们写一个函数是这么写的 func sayHello(name: String , greeting: String) { print( ...

  8. iOS改变图片尺寸

    - (UIImage *)originImage:(UIImage *)image scaleToSize:(CGSize)size { UIGraphicsBeginImageContext(siz ...

  9. [转]Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile

    http://blog.csdn.net/stpeace/article/details/39534361 进程间的通信方式有很多种, 上次我们说了最傻瓜的“共享外存/文件”的方法. 那么, 在本文中 ...

  10. linux下定时任务

    一.linux定时任务软件种类 .at : 适合执行一次的任务.突发性的任务.需要启动 atd 服务才能执行任务. .crontab: 周期性的执行任务工作:启动crond 服务后可以执行任务.最常用 ...