[POJ1117] Picture && 线段树+扫描线

把边排序 从左往右进行扫描计算周长 注意排序时左边的边要排在右边的边的前面

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define SF scanf
#define PF printf
#define ls (i*2)
#define rs (i*2+1)
using namespace std;
const int MAXN = 5000;
struct Line {
    int x, y1, y2;
    bool GetIn;
    Line () {}
    Line(int _x, int _y, int __y, bool ok) : x(_x), y1(_y), y2(__y), GetIn(ok) {}
    bool operator < (const Line &t) const {
        return (x < t.x || (( x == t.x) && (GetIn && !t.GetIn)));
    }
};
Line L[MAXN*2+10];
int y[MAXN*2+10];
struct Node {
    int l, r, cnt, Seg, sum;
    bool L_ok, R_ok;
};
struct Seg_Tree {
    Node tree[MAXN * 4 + 10];
    void build(int i, int l, int r) {
        int mid = (l + r) >> 1;
        tree[i].l = l; tree[i].r = r;
        tree[i].Seg = tree[i].cnt = tree[i].sum = 0;
        tree[i].L_ok = tree[i].R_ok = false;
        if(l == r-1) return ;
        build(ls, l, mid);
        build(rs, mid, r);
    }
    void update(int i) {
        if(tree[i].cnt) {
            tree[i].L_ok = tree[i].R_ok = true;
            tree[i].Seg = 1;
            tree[i].sum = y[tree[i].r] - y[tree[i].l];
        }
        else {
            if(tree[i].l == tree[i].r - 1) {
                tree[i].L_ok = tree[i].R_ok = false;
                tree[i].Seg = 0;
                tree[i].sum = 0;
            }
            else {
                tree[i].L_ok = tree[ls].L_ok;
                tree[i].R_ok = tree[rs].R_ok;
                tree[i].Seg = tree[ls].Seg + tree[rs].Seg;
                if(tree[ls].R_ok && tree[rs].L_ok) tree[i].Seg--;
                tree[i].sum = tree[ls].sum + tree[rs].sum;
            }
        }
    }
    void insert(int i, int l, int r, bool f) {
        if(r < tree[i].l || tree[i].r <= l) return ;
        if(l <= tree[i].l && tree[i].r <= r) {
            tree[i].cnt += (f ? 1 : -1);
            update(i);
            return ;
        }
        insert(ls, l, r, f);
        insert(rs, l, r, f);
        update(i);
    }
} seg;
int main()
{
    int n;
    while(~SF("%d", &n)) {
        for(int i = 1; i <= n; i++) {
            int x1, x2, y1, y2;
            SF("%d%d%d%d", &x1, &y1, &x2, &y2);
            y[i*2-1] = y1; y[i*2] = y2;
            L[i*2-1] = Line(x1, y1, y2, true);
            L[i*2] = Line(x2, y1, y2, false);
        }
        sort(y+1, y+1+2*n);
        sort(L+1, L+1+2*n);
        int N = unique(y+1, y+1+2*n) - (y+1);
        seg.build(1, 1, N);
        int Len = 0, ans = 0;
        for(int i = 1; i <= 2*n; i++) {
            int pos1 = lower_bound(y+1, y+1+N, L[i].y1) - y;
            int pos2 = lower_bound(y+1, y+1+N, L[i].y2) - y;
            seg.insert(1, pos1, pos2, L[i].GetIn);
            ans += abs(Len - seg.tree[1].sum);
            ans += seg.tree[1].Seg * 2 * (L[i+1].x - L[i].x);
            Len = seg.tree[1].sum;
        }
        printf("%d\n" , ans);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值