HDU 1828——Picture(线段树+周长并+扫描线)

Picture

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2902    Accepted Submission(s): 1533

Problem Description

A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of
all rectangles is called the perimeter.

Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.

The corresponding boundary is the whole set of line segments drawn in Figure 2.

The vertices of all rectangles have integer coordinates.

Input

Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The
values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.

0 <= number of rectangles < 5000

All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.

Please process to the end of file.

Output

Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.

Sample Input

7
-15 0 5 10
-5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
30 10 36 20
34 0 40 16

Sample Output

228

—————————————————————分割线————————————————————

题目大意:

给你n个矩形,保证这些矩形都是水平或者垂直的,求这些矩形所围成图形的周长

思路:

结构体保存矩形的左右端点,高度,下底边为1,上底边为-1

对矩形的高度从小到大排序,用一根扫描线从下往上扫

1、求底边:

周长并是长加宽,对于面积并中,已经知道每次覆盖区间线段长度,那么只要保存上一次的长度,那么这次和上一次的差值就是每次增加的线段长。即是底边

2、求高:

已知底边的长度,那么只要记录底边是由多少条连续区间线段构成的,假如为m,那么宽的数量就是m*2了。画画图

那么怎么求得连续子区间的数量?

一个节点的连续子区间的数量=左二子的连续区间数量+右儿子连续区间数量-(中间区间是否相连)

怎么判断中间区间是否相连? 记录一个区间的左右端点是否存在,如果左二子的右端点和右儿子的左端点都存在,则表明中间是连续的

仍然点表示线段,更新线段树的时候右端点-1,计算的时候把线段变为点  所以len[rt]=r-l+1

代码中保存节点信息的数组并没有初始化,因为根本没必要,是为什么呢?想想就知道了

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=20001;
using namespace std;
int cnt[maxn<<2],len[maxn<<2],numseg[maxn<<2];
bool lbd[maxn<<2],rbd[maxn<<2];
struct Seg
{
    int l,r,h;
    int s;
    Seg(){};
    Seg(int a,int b,int c,int d):l(a),r(b),h(c),s(d){};
    bool operator<(const Seg&cmp)const{
        return h<cmp.h;
    }
}ss[maxn];
void push_up(int rt,int l,int r)
{
    if(cnt[rt]){
        len[rt]=r-l+1;
        numseg[rt]=lbd[rt]=rbd[rt]=1;
    }
    else if(l==r){
        len[rt]=numseg[rt]=lbd[rt]=rbd[rt]=0;
    }
    else {
        lbd[rt]=lbd[rt<<1];
        rbd[rt]=rbd[rt<<1|1];
        len[rt]=len[rt<<1]+len[rt<<1|1];
        numseg[rt]=numseg[rt<<1]+numseg[rt<<1|1]-lbd[rt<<1|1]*rbd[rt<<1];
    }
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R){
        cnt[rt]+=c;
        push_up(rt,l,r);
        return ;
    }
    int m=(l+r)>>1;
    if(L<=m) update(L,R,c,lson);
    if(m<R) update(L,R,c,rson);
    push_up(rt,l,r);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF){
        int a,b,c,d,m=0;
        int l_bd=1<<30,r_bd=-1<<30;
        while(n--){
            scanf("%d %d %d %d",&a,&b,&c,&d);
            l_bd=min(l_bd,a),r_bd=max(r_bd,c);
            ss[m++]=Seg(a,c,b,1);
            ss[m++]=Seg(a,c,d,-1);
        }
        sort(ss,ss+m);
        int ret=0,last=0;
        for(int i=0;i<m;++i){
            update(ss[i].l,ss[i].r-1,ss[i].s,l_bd,r_bd,1);
            ret+=numseg[1]*(ss[i+1].h-ss[i].h)*2;
            ret+=abs(len[1]-last);
            last=len[1];
        }
        printf("%d\n",ret);
    }
    return 0;
}

因为一个样例结束后,是计算到上底边的,下个样例开始时是从下底边开始计算的,那么 -1+1=0,就清空了,节点信息是实时更新的。

时间: 2024-10-10 12:22:33

HDU 1828——Picture(线段树+周长并+扫描线)的相关文章

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

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

hdu 1828 Picture(线段树&amp;扫描线&amp;周长并)

Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2578    Accepted Submission(s): 1363 Problem Description A number of rectangular posters, photographs and other pictures of the same shap

HDU 1828 Picture 线段树+扫描线

题意:给你一些矩形的左上角点的坐标和右下角点的坐标,求周长并 最显而易见的思路就是对于x轴和y轴做两次扫描线,对于负数的坐标进行离散化.每次增加的值是线段变化量的绝对值.具体写法和求面积并的差不多. #include <cstdio> #include <algorithm> #include <cstring> #include <vector> using namespace std; #define lson rt << 1 , l , m

hdu 1828 Picture(线段树轮廓线)

Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3075    Accepted Submission(s): 1616 Problem Description A number of rectangular posters, photographs and other pictures of the same shape

HDU 1828 Picture(矩形周长并)

HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4个方向都加完就是答案 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 5005; int n; struct Rec { int

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 Picture(长方形的周长和)

HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4个方向都加完就是答案 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 5005; int n; struct Rec { int

hdu1828 Picture(线段树+离散化+扫描线)两种方法

C - Picture Time Limit:2000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or

hdu 1542 Atlantis(线段树&amp;扫描线&amp;面积并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6386    Accepted Submission(s): 2814 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i