线段树 hdu3265 Posters

刚开始想着添加4条边,但是发现这种思维得到的答案不对~

因为,如果中间没有洞,遇到下边界的时候就在区间(x1,x2)上加一,遇到下边界的时候就在区间(x1,x2)上减一

但是区间的cnt都会>=0

但是如果先减一,再加一,那么因为cnt会变成负数,,所以以前的push_up就没用了


后来想了一下,,为什么不直接把这个有洞的矩形,分割成4个完整的矩形呢??!

分割完后,将新的矩形所有的按照经典模板敲一遍,不就是答案了么

#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define root 0,50000,1

int const MX = 1e5 + 5;

int rear, cnt[MX << 2], S[MX << 2];

struct Que {
    int d;
    int top, L, R;
    Que() {}
    Que(int _top, int _L, int _R, int _d) {
        top = _top; L = _L; R = _R; d = _d;
    }
    bool operator<(const Que &b)const {
        return top < b.top;
    }
} Q[MX << 3];

void push_up(int l, int r, int rt) {
    if(cnt[rt]) S[rt] = r - l + 1;
    else if(l == r) S[rt] = 0;
    else S[rt] = S[rt << 1] + S[rt << 1 | 1];
}

void update(int L, int R, int d, int l, int r, int rt) {
    if(L <= l && r <= R) {
        cnt[rt] += d;
        push_up(l, r, rt);
        return;
    }

    int m = (l + r) >> 1;
    if(L <= m) update(L, R, d, lson);
    if(R > m) update(L, R, d, rson);
    push_up(l, r, rt);
}

int main() {
    //freopen("input.txt", "r", stdin);
    int n;
    while(~scanf("%d", &n), n) {
        rear = 0;
        memset(cnt, 0, sizeof(cnt));
        memset(S, 0, sizeof(S));

        for(int i = 1; i <= n; i++) {
            int x1, y1, x2, y2, x3, y3, x4, y4;
            scanf("%d%d%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
            Q[++rear] = Que(y1, x3, x4, 1); Q[++rear] = Que(y3, x3, x4, -1);
            Q[++rear] = Que(y4, x3, x4, 1); Q[++rear] = Que(y2, x3, x4, -1);
            Q[++rear] = Que(y1, x1, x3, 1); Q[++rear] = Que(y2, x1, x3, -1);
            Q[++rear] = Que(y1, x4, x2, 1); Q[++rear] = Que(y2, x4, x2, -1);
        }
        sort(Q + 1, Q + 1 + rear);

        int last = 0; LL ans = 0;
        for(int i = 1; i <= rear; i++) {
            ans += (LL)(Q[i].top - last) * S[1];
            if(Q[i].L <= Q[i].R - 1) update(Q[i].L, Q[i].R - 1, Q[i].d, root);
            last = Q[i].top;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值