题目链接

看的HH的题解。。周长有两部分组成,横着和竖着的,横着通过,sum[1] - last来计算,竖着的通过标记,记录有多少段。

 #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 10000
#define lson l , m, rt<<1
#define rson m+1, r,rt<<1|1
int que[maxn*];
int sum[maxn*];
int cnt[maxn*];
bool lbd[maxn*] , rbd[maxn*];
int numseg[maxn*];
struct node
{
int lx,rx,y;
int s;
node() {}
node(int a,int b,int c,int d):lx(a),rx(b),y(c),s(d) {}
bool operator < (const node &S) const
{
if(y == S.y) return s > S.s;
return y < S.y;
}
} mat[maxn];
int bin(int x,int n)
{
int str,end,mid;
str = ,end = n;
while(str <= end)
{
mid = (str+end)/;
if(que[mid] == x)
return mid;
else if(que[mid] > x)
end = mid - ;
else
str = mid + ;
}
return mid;
}
void pushup(int rt,int l,int r)
{
if(cnt[rt])
{
lbd[rt] = rbd[rt] = ;
sum[rt] = que[r+] - que[l];
numseg[rt] = ;
}
else if (l == r)
{
sum[rt] = numseg[rt] = lbd[rt] = rbd[rt] = ;
}
else
{
lbd[rt] = lbd[rt<<];
rbd[rt] = rbd[rt<<|];
sum[rt] = sum[rt<<] + sum[rt<<|];
numseg[rt] = numseg[rt<<] + numseg[rt<<|];
if (lbd[rt<<|] && rbd[rt<<]) numseg[rt] -= ;
}
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(L <= l&&r <= R)
{
cnt[rt] += c;
pushup(rt,l,r);
return ;
}
int m = (l+r)>>;
if(L <= m)
update(L,R,c,lson);
if(R > m)
update(L,R,c,rson);
pushup(rt,l,r);
}
int main()
{
int n,num,i,l,r;
int a,b,c,d;
while(scanf("%d",&n)!=EOF)
{
if(!n) break;
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];
}
int ans = ,last = ;
memset(cnt,,sizeof(cnt));
memset(sum,,sizeof(sum));
memset(lbd,,sizeof(lbd));
memset(rbd,,sizeof(rbd));
for(i = ; i < num; i ++)
{
l = bin(mat[i].lx,k-);
r = bin(mat[i].rx,k-)-;
if(l <= r)
update(l,r,mat[i].s,,k-,);
ans += numseg[] * (mat[i+].y - mat[i].y);
ans += abs(sum[] - last);
last = sum[];
}
printf("%d\n",ans);
}
return ;
}

POJ 1177 Picture(求周长并)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)

    题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...

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

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

  8. poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)

    题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...

  9. poj 1177 picture

    题目链接:http://poj.org/problem?id=1177 分析:这道题主要用到了线段树.扫描线以及离散化的相关算法. 离散化 离散化是当数字不多但是范围很大时采用的一种方法,将大区间的数 ...

随机推荐

  1. yum 配置

    1.配置yum本地源 # mount /dev/cdrom /mnt/ # vim /etc/yum.repos.d/rhel-source.repo 1 [rhel-source] 2 name=R ...

  2. Python列表的remove方法的注意事项

    为何没有删除列表中的全部元素? 解释: 按照执行顺序,第一个空格被删除之后,后面的元素会前移(变成['空格','空格','12','23']),指针下一次会指向新列表的第二个元素(即初始状态的第三个空 ...

  3. linux shell expr 使用

    linux shell expr 使用 收藏人:春秋百味 -- | 阅: 转: | | 分享 非原创, 摘自:<LINUX与UNIX Shell编程指南> 17.5 expr用法 expr ...

  4. Redis系列-远程连接redis并给redis加锁

    假设两台redis服务器,ip分别为:192.168.1.101和192.168.1.103,如何在101上通过redis-cli访问103上的redis呢?在远程连接103之前,先讲下redis-c ...

  5. 右移>> 和 左移<<

    一个int占四个字节,也就是32位,这样的话1不论左移还是右移32位仍旧移到原来的位置,就仍旧是1了. 右移是除,左移是乘.1除1除32次和1乘1乘32次当然都还是1了. 移位操作的简单计算方法 &g ...

  6. 【Spring】Spring系列3之Spring AOP

    3.Spring AOP 3.1.AOP概述 3.2.前置通知 3.3.后置通知 3.4.返回通知.异常通知.环绕通知 3.5.指定切面优先级 3.6.重用切入点表达式 3.7.引入通知 3.8.基于 ...

  7. poj 1833

    http://poj.org/problem?id=1833 next_permutation这个函数是用来全排列的,按字典的序进行排列,当排列无后继的最大值时,会执行字典升序排列,相当于排序: 当排 ...

  8. django LDAP

    > http://goodosoft.github.io/2015/02/25/Using-AD-as-authentication-for-Django/ > http://my.osc ...

  9. iOS CoreData学习资料 和 问题

    这里是另一篇好文章 http://blog.csdn.net/kesalin/article/details/6739319 这里是另一篇 http://hxsdit.com/1622 (不一定能访问 ...

  10. 通过百度echarts实现数据图表展示功能

    现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解 ...