POJ 1177

/*
扫描线
求矩阵的周长交并
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson (pos<<1)
#define rson (pos<<1|1)
typedef long long LL;
const int ADD = 10005;
const int maxn = 25555;
struct Seg{
    int a,b,h,c;
    Seg(int a = 0,int b = 0,int h = 0,int c = 0):
        a(a),b(b),h(h),c(c){};
    friend bool operator < (Seg p,Seg q){
        return p.h < q.h;
    }
}seg[maxn];
int cnt;
struct Node{
    int l,r,sum1,sum2,cover;
    bool left,right;
    int mid(){
        return (l + r) >> 1;
    }
    int len(){
        return (r - l + 1);
    }
}node[maxn <<2];
void pushup(int pos){
    if(node[pos].cover){
        node[pos].sum1 = node[pos].len();
        node[pos].sum2 = 2;
        node[pos].left = node[pos].right = true;
    }
    else if(node[pos].l == node[pos].r){
        node[pos].left = node[pos].right = false;
        node[pos].sum1 = node[pos].sum2  = 0;
    }
    else{
        node[pos].left  = node[lson].left;
        node[pos].right = node[rson].right;
        node[pos].sum1  = node[lson].sum1 + node[rson].sum1;
        node[pos].sum2  = node[lson].sum2 + node[rson].sum2;
        if(node[lson].right && node[rson].left)
            node[pos].sum2 -= 2;
    }
}
void build(int l,int r,int pos){
    node[pos].l = l;
    node[pos].r = r;
    node[pos].cover = 0;
    node[pos].sum1 = node[pos].sum2 = 0;
    node[pos].left = node[pos].right = false;
    if(l == r) return;
    int mid = node[pos].mid();
    build(l,mid,lson);
    build(mid + 1,r,rson);
}
void update(int l,int r,int pos,int d){
    if(l <= node[pos].l && node[pos].r <= r){
         node[pos].cover += d;
         pushup(pos);
         return;
    }
    int mid = node[pos].mid();
    if(l <= mid)
        update(l,r,lson,d);
    if(r  > mid)
        update(l,r,rson,d);
    pushup(pos);
}
int main(){
    int n;
    while(scanf("%d",&n) != EOF){
        cnt = 0;
        for(int i = 0; i < n; i++){
            int a,b,c,d;
            scanf("%d%d%d%d",&a,&b,&c,&d);
            a += ADD;
            c += ADD;
            seg[cnt++] = Seg(a,c,b,1);
            seg[cnt++] = Seg(a,c,d,-1);
        }
        sort(seg,seg + cnt);
        int last = 0;
        LL  ans  = 0;
        build(1,21000,1);
        for(int i = 0; i < cnt; i++){
            int l = seg[i].a,r = seg[i].b, c = seg[i].c;
            update(l,r - 1,1,c);
            ans += node[1].sum2 * (seg[i + 1].h - seg[i].h);
            ans += abs(node[1].sum1 - last);
            last = node[1].sum1;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值