(线段树/扫描线周长)Picture

http://poj.org/problem?id=1177

经典线段树扫描线入门题目,参考博客:https://blog.csdn.net/tomorrowtodie/article/details/52048323
对给出的图由下往上扫描(方向自定),将每个矩形分解为上面、下面的两条边,保存其左右坐标和高度,并用线段树维护有效区间长度。
求矩形的相交后的面积:线段树保存lc,rc(左右端点是否被占用),len(区间有效长度),按区间合并更新线段树(不需要pushdown),将边按高度排序,下边flg设为1(表示在线段树中加入),上边设为-1(表示从线段树中删去),由下往上对每条边,按flg更新到线段树,每次总面积加上该边上面一条边的高度-该边高度再乘以查询得到的有效长度
求矩形的相交后的周长:线段树需要多保存信息,len保存有效长度(用于计算横边),sum保存该范围由几个连续区间占用(用于计算竖边,sum * 2 * 高度差),s保存被几次占用(用于更新)。之后同计算面积,由下往上处理每条边,计算时pre保存上一次的有效长度,即每一次总周长加上当前查询的有效长度减去pre + 2 * 总sum * 高度差(上面的边高度减当前边的高度)

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <queue>
using namespace std;

const int maxn = 20000 + 3;
const int inf = 0x3f3f3f3f;
struct node {
    int len, s, num;
    bool lc, rc;
} a[maxn << 2];
void pushup(int rt, int l, int r) {
    if(a[rt].s) {
        a[rt].len = r - l + 1;
        a[rt].lc = a[rt].rc = a[rt].num = 1;
    }
    else if(l == r) 
        a[rt].lc = a[rt].rc = a[rt].num = a[rt].len = 0;
    else {
        a[rt].len = a[rt << 1].len + a[rt << 1 | 1].len;
        a[rt].lc = a[rt << 1].lc;
        a[rt].rc = a[rt << 1 | 1].rc;
        a[rt].num = a[rt << 1].num + a[rt << 1 | 1].num - (a[rt << 1].rc & a[rt << 1 | 1].lc);
    }
}
void build(int rt, int l, int r) {
    a[rt].len = a[rt].s = a[rt].num = a[rt].lc = a[rt].rc = 0;
    if(l == r) return;
    int mid = l + r >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
}
void update(int rt, int L, int R, int val, int l, int r) {
    if(L <= l && r <= R) {
        a[rt].s += val;
        pushup(rt, l, r);
        return;
    }
    int mid = l + r >> 1;
    if(mid >= R) update(rt << 1, L, R, val, l, mid);
    else if(mid < L) update(rt << 1 | 1, L, R, val, mid + 1, r);
    else update(rt << 1, L, R, val, l, mid), update(rt << 1 | 1, L, R, val, mid + 1, r);
    pushup(rt, l, r);
}
int query(int rt, int L, int R, int l, int r) {
    if(L <= l && r <= R) return a[rt].len;
    int mid = l + r >> 1;
    if(mid >= R) return query(rt << 1, L, R, l, mid);
    else if(mid < L) return query(rt << 1 | 1, L, R, mid + 1, r);
    else return query(rt << 1, L, R, l, mid) + query(rt << 1 | 1, L, R, mid + 1, r);
}
const int maxm = 5000 + 4;
struct edgenode {
    int l, r, h, flg;
    edgenode() {}
    edgenode(int _l, int _r, int _h, int _flg) : l(_l), r(_r), h(_h), flg(_flg) {}
    bool operator <(const edgenode& x) const {
        return h < x.h;
    }
} E[maxm << 1];
int main()
{
    int n;
    while(~scanf("%d", &n)) {
        int tot = 0, mx = -inf, mn = inf;
        for(int i = 1; i <= n; i++) {
            int lx, ly, rx, ry;
            scanf("%d%d%d%d", &lx, &ly, &rx, &ry);
            mx = max(mx, max(lx, rx));
            mn = min(mn, min(lx, rx));
            E[tot++] = edgenode(lx, rx, ly, 1);
            E[tot++] = edgenode(lx, rx, ry, -1);
        }
        build(1, mn, mx - 1);
        sort(E, E + tot);
        int ans = 0, pre = 0;
        for(int i = 0; i < tot; i++) {
            int l = E[i].l, r = E[i].r, h = E[i].h, flg = E[i].flg;
            update(1, l, r - 1, flg, mn, mx - 1);
            ans += abs(a[1].len - pre);
            ans += (E[i + 1].h - h) * 2 * a[1].num;
            pre = a[1].len;
        }
        printf("%d\n", ans);
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值