POJ 1177(扫描线 轮廓线)

题目链接:http://poj.org/problem?id=1177

 

题目大意:求多个矩形重合后的周长和

 

题目思路:从上到下来一遍扫描线,从左到右来一遍扫描线。然后每次更新线段树后整体解与之前的结果相减取绝对值就是新的周长。可以画图理解。

 

以下是代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
const int MAXN = 1e5+5;
const int MOD = 1e9+7;
struct node{
    int l,r,cnt;
    int val;
}a[MAXN<<2];
struct seg{
    int x,y1,y2;
    int flag;
}b[MAXN];
struct seg2{
    int y,x1,x2;
    int flag;
}d[MAXN];
int c[MAXN];
bool cmp(seg a,seg b){
    return a.x<b.x;
}
bool cmp2(seg2 a,seg2 b){
    return a.y<b.y;
}
void build(int rt,int l,int r){
    a[rt].l=l,a[rt].r=r;
    a[rt].cnt=a[rt].val=0;
    if(l==r)return;
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
}
void push_up(int rt){
    if(a[rt].cnt){
        a[rt].val=c[a[rt].r+1]-c[a[rt].l];
    }
    else if(a[rt].l==a[rt].r)a[rt].val = 0;
    else a[rt].val=a[rt<<1].val+a[rt<<1|1].val;
}
void update(int rt,int l,int r,int val){
    if(l<=a[rt].l&&a[rt].r<=r){
        a[rt].cnt+=val;
        push_up(rt);
        return;
    }
    int mid=(a[rt].l+a[rt].r)>>1;
    if(l<=mid)update(rt<<1,l,r,val);
    if(r>mid)update(rt<<1|1,l,r,val);
    push_up(rt);
}
int main(){
    int n;
    while(~scanf("%d",&n)){
        int x1,y1,x2,y2;
        int num=1,p=0;
        rep(i,1,n){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            b[num].x=x1,b[num].y1=y1,b[num].y2=y2,b[num].flag=1;
            d[num].y=y1,d[num].x1=x1,d[num].x2=x2,d[num++].flag=1;
            b[num].x=x2,b[num].y1=y1,b[num].y2=y2,b[num].flag=-1;
            d[num].y=y2,d[num].x1=x1,d[num].x2=x2,d[num++].flag=-1;
            c[p++]=x1,c[p++]=y1,c[p++]=x2,c[p++]=y2;
        }
        sort(b+1,b+num,cmp);
        sort(c,c+p);
        p=unique(c,c+p)-c;
        build(1,0,p);
        int ans=0,late=0;
        rep(i,1,2*n){
            int l=lower_bound(c,c+p,b[i].y1)-c,r=lower_bound(c,c+p,b[i].y2)-c-1;
            update(1,l,r,b[i].flag);
            ans+=abs(a[1].val-late);
            late=a[1].val;
        }
        late=0;
        build(1,0,p);
        sort(d+1,d+num,cmp2);
        rep(i,1,2*n){
            int l=lower_bound(c,c+p,d[i].x1)-c,r=lower_bound(c,c+p,d[i].x2)-c-1;
            update(1,l,r,d[i].flag);
            ans+=abs(a[1].val-late);
            late=a[1].val;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值