POJ - 1177 Picture

POJ - 1177 Picture

线段树扫描线求矩形周长并

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <bitset>
#define INF 0x3f3f3f3f
#define eps 1e-6
#define PI 3.1415926
#define mod 1000000009
#define base 2333
using namespace std;
typedef long long LL;
const int maxn = 1e4 + 10;
const int maxx = 1e3 + 10;
inline void splay(int &v) {
    v=0;char c=0;int p=1;
    while(c<'0' || c >'9'){if(c=='-')p=-1;c=getchar();}
    while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();}
    v*=p;
}
int n, Hash[maxn];
struct edge {
    int x1, x2, h, cur;
    edge(int x1 = 0, int x2 = 0, int h = 0, int cur = 0):
        x1(x1), x2(x2), h(h), cur(cur) {}
    bool operator <(const edge &a) const {
        return h < a.h;
    }
} seg[maxn];

struct Tree {
    int l, r, m, len, mark, num;
    bool lc, rc;
} tr[maxn<<2];

void PushUp(int id) {
    if(tr[id].mark) {
        tr[id].len = Hash[tr[id].r+1]-Hash[tr[id].l];
        tr[id].lc = tr[id].rc = 1;
        tr[id].num = 1;
    }
    else if(tr[id].l == tr[id].r) {
        tr[id].len = tr[id].num = 0;
        tr[id].lc = tr[id].rc = 0;
    }
    else {
        tr[id].len = tr[id<<1].len+tr[id<<1|1].len;
        tr[id].lc = tr[id<<1].lc, tr[id].rc = tr[id<<1|1].rc;
        tr[id].num = tr[id<<1].num+tr[id<<1|1].num-(tr[id<<1].rc&tr[id<<1|1].lc);
        //去重
    }
}

void build(int id, int l, int r) {
    tr[id].l = l, tr[id].r = r, tr[id].m = (l+r)>>1;
    tr[id].mark = tr[id].len = tr[id].num = 0;
    tr[id].lc = tr[id].rc = 0;
    if(l == r) return ;
    build(id<<1, l, tr[id].m);
    build(id<<1|1, tr[id].m+1, r);
}

void update(int id, int l, int r, int d) {
    if(l <= tr[id].l && r >= tr[id].r)
        tr[id].mark += d;
    else {
        if(l <= tr[id].m) update(id<<1, l, r, d);
        if(r > tr[id].m) update(id<<1|1, l, r, d);
    }
    PushUp(id);
}

void solve() {
    while(scanf("%d", &n) != EOF) {
        int x1, x2, y1, y2;
        int k = 0, m = 1, ans = 0, last = 0;
        for(int i = 0; i < n; i++) {
            splay(x1), splay(y1), splay(x2), splay(y2);
            seg[++k] = edge(x1, x2, y1, 1);
            Hash[k] = x1;
            seg[++k] = edge(x1, x2, y2, -1);
            Hash[k] = x2;
        }
        sort(Hash+1, Hash+k+1);
        sort(seg+1, seg+k+1);
        for(int i = 1; i < k; i++) {           //离散化
            if(Hash[i] != Hash[i+1])
                Hash[++m] = Hash[i+1];
        }
        build(1, 1, m);
        for(int i = 1; i <= k; i++) {
            int L = lower_bound(Hash+1, Hash+m+1, seg[i].x1)-Hash;
            int R = lower_bound(Hash+1, Hash+m+1, seg[i].x2)-Hash-1;
            update(1, L, R, seg[i].cur);
            ans += abs(tr[1].len-last);                //求横边
            ans += (seg[i+1].h-seg[i].h)*2*tr[1].num;  //求纵边
            last = tr[1].len;
        }
        printf("%d\n", ans);
    }
}

int main() {
    //srand(time(NULL));
    //freopen("kingdom.in","r",stdin);
    //freopen("kingdom.out","w",stdout);
    solve();
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值