HDU1828线段树(扫描线)

Picture

Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12 Accepted Submission(s): 10
 
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
 
 
Source
IOI 1998
 

题意:

给出n个矩形的右下和左上角的坐标,问这n个矩形组成的图形的边长

代码:

参考博客

http://blog.csdn.net/u013480600/article/details/22548393

http://blog.csdn.net/u012860063/article/details/43163949

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=20004;
int cnt[maxn*4],segnum[maxn*4],sum[maxn*4],pre[maxn*4],suf[maxn*4];
struct node{
    int l,r,h,d;
    node(){}
    node(int a,int b,int c,int d):l(a),r(b),h(c),d(d){}
    bool operator < (node&p){
        if(h==p.h) return d>p.d;
        return h<p.h;
    }
}nodes[maxn];
void pushup(int l,int r,int rt){
    if(cnt[rt]){//是下底边
        segnum[rt]=2;
        pre[rt]=suf[rt]=1;
        sum[rt]=r-l+1;
    }
    else if(l==r)//是叶子
        segnum[rt]=sum[rt]=pre[rt]=suf[rt]=0;
    else{
        segnum[rt]=segnum[rt<<1]+segnum[rt<<1|1];
        if(suf[rt<<1]&&pre[rt<<1|1]) segnum[rt]-=2;//有竖边重合
        sum[rt]=sum[rt<<1]+sum[rt<<1|1];
        pre[rt]=pre[rt<<1];//用两个儿子的两头更新父亲的两头
        suf[rt]=suf[rt<<1|1];
    }
}
void build(int l,int r,int rt){//本题中可以不用build递归,直接memset数组就行
    cnt[rt]=0;suf[rt]=0;pre[rt]=0;segnum[rt]=0;sum[rt]=0;
    if(l==r) return;
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
}
void update(int L,int R,int c,int l,int r,int rt){
    if(L<=l&&R>=r){
        cnt[rt]+=c;
        pushup(l,r,rt);
        return;
    }
    int m=(l+r)>>1;
    if(L<=m) update(L,R,c,l,m,rt<<1);
    if(R>m) update(L,R,c,m+1,r,rt<<1|1);
    pushup(l,r,rt);
}
int main()
{
    int t,x1,x2,y1,y2;
    while(scanf("%d",&t)==1){
        int lbd=-10000,rbd=10000,m=0;
        while(t--){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            lbd=min(lbd,x1);
            rbd=max(rbd,x2);
            nodes[m++]=node(x1,x2,y1,1);
            nodes[m++]=node(x1,x2,y2,-1);
        }
        build(lbd,rbd-1,1);
        sort(nodes,nodes+m);
        int ans=0,last=0;
        for(int i=0;i<m;i++){
            if(nodes[i].l<=nodes[i].r-1) //这个判断要有!
            update(nodes[i].l,nodes[i].r-1,nodes[i].d,lbd,rbd-1,1);
            ans+=abs(sum[1]-last);//算横向边
            last=sum[1];//更新前一个边
            if(i!=m-1)//算纵向边
            ans+=segnum[1]*(nodes[i+1].h-nodes[i].h);
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/6401417.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值