链接

神奇的扫描线啊

估计之前刷面积并的时候太急了 没来得及理解 。。

有一大段代码是与面积并一模一样的 都是离散化 更新

面积并是扫描的x  这次也一样

因为周长只算外围的 所以扫描到一条x边时 要减去上一次扫描到的 对于Y方向上 就是与面积并不大一样的地方了

看篇带图的讲解吧 网上的题解都大同小异 估计出自一人之手

http://blog.sina.com.cn/s/blog_691ce2b7010177dz.html

它是扫描的Y  其实方法是一样的 横过来就是

对于我的代码 自己感觉不好理解在于求区间数

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define N 10010
#define LL long long
struct node
{
int lx,rx,y,d;
node(){}
node(int a,int b,int c,int d):lx(a),rx(b),y(c),d(d){}
bool operator < (const node &S) const
{
if(y == S.y) return d > S.d;
return y<S.y;
}
}mat[N];
int que[N],sum[N<<],lg[N<<],rg[N<<],seg[N<<];
int cnt[N<<];
LL ans=;
int bin(int k,int low,int high)
{
int m;
while(low<=high)
{
m = (low+high)/;
if(que[m]==k)
return m;
if(que[m]>k)
high = m-;
else
low = m+;
}
return m;
}
void up(int l,int r,int w)
{
if(cnt[w])//入边就更新这段的值 及区间数 (1个区间 2条Y边)
{
sum[w] = que[r+]-que[l];
seg[w] = ;
lg[w] = ;
rg[w] = ;
}
else if(l==r)
{
sum[w] = seg[w] = lg[w] = rg[w] = ;//出边的话 消除这条边
}
else//非出入边 向上更新 与平常线段树一样 等于子孩子相加
{
sum[w] = sum[w<<]+sum[w<<|];
seg[w] = seg[w<<]+seg[w<<|];
lg[w] = lg[w<<];
rg[w] = rg[w<<|];
if(lg[w<<|]&&rg[w<<])//左右相连 减去重复的
seg[w]-=;
}
}
void update(int a,int b,int d,int l,int r,int w)
{
if(a<=l&&b>=r)
{
cnt[w]+=d;
up(l,r,w);
return ;
}
int m = (l+r)>>;
if(a<=m)
update(a,b,d,l,m,w<<);
if(b>m)
update(a,b,d,m+,r,w<<|);
up(l,r,w);
}
int main()
{
int i,n,a,b,c,d;
while(scanf("%d",&n)!=EOF)
{
int num=;
for(i = ; i <= n ; i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
que[num] = a;
mat[num++] = node(a,c,b,);
que[num] = c;
mat[num++] = node(a,c,d,-);
}
sort(que,que+num);
sort(mat,mat+num);
int k = ;
for(i = ; i < num ; i++)
{
if(que[i]!=que[i-])
que[k++] = que[i];
}
memset(cnt,,sizeof(cnt));
memset(sum,,sizeof(sum));
memset(lg,,sizeof(lg));
memset(rg,,sizeof(rg));
memset(seg,,sizeof(seg));
int o=;
ans=;
mat[num].y = ;
for(i = ; i < num ; i++)
{
int l = bin(mat[i].lx,,k-);
int r = bin(mat[i].rx,,k-)-;
update(l,r,mat[i].d,,k-,);
ans+=seg[]*(mat[i+].y-mat[i].y);
ans+=abs(sum[]-o);
o = sum[];
}
printf("%lld\n",ans);
}
return ;
}

poj1177Picture(线段树-周长并)的更多相关文章

  1. poj 1177 Picture(线段树周长并)

    题目链接:http://poj.org/problem?id=1177 题意:给你n个矩形问你重叠后外边缘总共多长. 周长并与面积并很像只不过是处理的时候是   增加的周长=abs(上一次的线段的长度 ...

  2. POJ 1177 Picture(线段树周长并)

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

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

    题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 ...

  4. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

  5. hdu 1828 线段树扫描线(周长)

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

  6. hdu 1828 Picture(线段树扫描线矩形周长并)

    线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...

  7. 51nod 1206 Picture 矩形周长求并 | 线段树 扫描线

    51nod 1206 Picture 矩形周长求并 | 线段树 扫描线 #include <cstdio> #include <cmath> #include <cstr ...

  8. POJ 1177 Picture(线段树:扫描线求轮廓周长)

    题目链接:http://poj.org/problem?id=1177 题目大意:若干个矩形,求这些矩形重叠形成的图形的轮廓周长. 解题思路:这里引用一下大牛的思路:kuangbin 总体思路: 1. ...

  9. luogu P1856 [USACO5.5]矩形周长Picture 扫描线 + 线段树

    Code: #include<bits/stdc++.h> #define maxn 200007 #define inf 100005 using namespace std; void ...

随机推荐

  1. 使用jQuery调用ASP.NET WebService的简易教程

    鉴于使用Javascript调用Web Service配置略麻烦,所以记录一下. 1. 新建一个Web服务(WebService.asmx) 2. 取消注释// [System.Web.Script. ...

  2. Famount game manufacuters

    一.Valve「美国」 1.Valve「中文意:阀门」の来源 REFRE.Valve官网 REFER.a history of valve's opening logos REFER.theguyin ...

  3. [java学习笔记]java语言基础概述之标识符&关键字&注释&常量和变量

    一.标识符 在程序中自定义的一些名称 由26个英文字母的大小写,数字,_$组成 定义合法标识符的规则: 不能以数字开头 不能使用关键字 java严格区分大小写 注意:在起名字时,为了提高阅读性,必须得 ...

  4. 计算序列中第k小的数

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4046399.html 使用分治算法,首先选择随机选择轴值pivot,并使的序列中比pivot ...

  5. 数据库之mysql 视图

    视图,把基本表的某些数据组合起来构成一个虚拟表的一种形式,之所以叫虚拟,是因为只有当视图用于一条语句中的时候,它才能存在.同时如果对视图中的数据进行修改,会同时修改到基本表中的数据. 创建视图: cr ...

  6. position:absolute,绝对定位和相对定位,JQ隐藏和显示

    需要在指定位置,用绝对定位. 如果直接写position:absolute,top:0;left:0,那就是以浏览器的左上角为参照了 现在需要在某一个指定位置用绝对定位 解决方法 在需要用绝对定位(p ...

  7. jquery.animate用法

    <!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/1.10. ...

  8. C#线程总结

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  9. Linux下GPIO驱动(四) ----gpio_request();gpio_free();

    //gpio_request申请gpio口 int gpio_request(unsigned gpio, const char *label) { struct gpio_desc *desc; s ...

  10. Oracle 分析函数 "ORA-30485: 在窗口说明中丢失 ORDER BY 表达式"

    跟顺序有关的几个分析函数row_number.rank.dense_rank.lead和lag的over窗口里,都必须有order_by_clause.其他几个如:first_value.last_v ...