码迷,mamicode.com
首页 > 其他好文 > 详细

扫描线

时间:2019-09-30 23:20:15      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:char   bool   signed   article   push   void   node   http   space   

建议移步fsy的博客
讲的比较清楚
注意判一下边界条件
丢个板

#include<bits/stdc++.h>
#define int long long
#define N (300000 + 10)
using namespace std;

inline void read(int &cnt) {
    cnt = 0;
    int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
    while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + (c ^ 48); c = getchar();}
    cnt *= f;
}

int n, tot = 0, ans = 0;
int a, b, c, d, cnt;
int Y[N << 1];
struct Line {
    int x, Y1, Y2;
    int k;
}seg[N << 1];
bool cmp(Line a, Line b) {return a.x == b.x ? a.Y1 < b.Y1 : a.x < b.x;}

struct node {
    int l, r;
    int cnt, len;
    #define l(p) tree[p].l
    #define r(p) tree[p].r
    #define cnt(p) tree[p].cnt
    #define len(p) tree[p].len
}tree[N << 2];

void pushup(int p) {
    if (cnt(p) > 0) len(p) = Y[r(p)] - Y[l(p)];
    else len(p) = len(p << 1) + len(p << 1 | 1);
}

void build(int p, int l, int r) {
//  cout<<p<<endl;
    l(p) = l, r(p) = r;
    if (l == r - 1) {len(p) = cnt(p) = 0; return;}
    int mid = (l + r + 1) >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid, r);
//  pushup(p);
}

void modify(int p, int l, int r, int k) {
    if (l <= Y[l(p)] && r >= Y[r(p)]) cnt(p) += k;
    else {  
        int mid = (l(p) + r(p) + 1) >> 1;
        if (l < Y[mid]) modify(p << 1, l, r, k);
        if (r > Y[mid]) modify(p << 1 | 1, l, r, k);
    }
        pushup(p);
}

signed main() {
    read(n);
    for (register int i = 1; i <= n; ++i) {
        read(a), read(b), read(c), read(d);
        seg[++cnt].x = a, seg[cnt].Y1 = b, seg[cnt].Y2 = d, seg[cnt].k = 1, Y[cnt] = b;
        seg[++cnt].x = c, seg[cnt].Y1 = b, seg[cnt].Y2 = d, seg[cnt].k = -1, Y[cnt] = d;
    }
    sort(seg + 1, seg + cnt + 1, cmp);
    sort (Y + 1, Y + cnt + 1);
    tot = unique(Y + 1, Y + cnt + 1) - Y - 1;
    build(1, 1, tot);
    for (register int i = 1; i <= cnt - 1; ++i) {
        modify(1, seg[i].Y1, seg[i].Y2, seg[i].k);
        ans += (seg[i + 1].x - seg[i].x) * len(1);
//      cout<<ans<<endl;
    } printf("%lld", ans);
    return 0;
}

扫描线

标签:char   bool   signed   article   push   void   node   http   space   

原文地址:https://www.cnblogs.com/kma093/p/11614211.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!