[模板] 扫描线(面积并)

题源:https://www.luogu.org/problem/P5490

#include <bits/stdc++.h>

#define numm ch - 48
#define pd putchar(' ')
#define pn putchar('\n')
#define pb push_back
#define fi first
#define se second
#define fre1 freopen("1.txt", "r", stdin)
#define fre2 freopen("2.txt", "w", stdout)
typedef long long int ll;
typedef long long int LL;
using namespace std;

template<typename T>
void read(T &res) {
    bool flag = false;
    char ch;
    while (!isdigit(ch = getchar())) (ch == '-') && (flag = true);
    for (res = numm; isdigit(ch = getchar()); res = (res << 1) + (res << 3) + numm);
    flag && (res = -res);
}

template<typename T>
void write(T x) {
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

//static auto _ = []() {
//    ios::sync_with_stdio(false);
//    cin.tie(0);
//    return 0;
//}();
//#pragma GCC optimize(3,"Ofast","inline")
//

const int maxn = 1e6 + 5;

ll n, x, y, xx, yy, cnt = 0;
ll Y[maxn << 1];

struct node {
    ll x, y, yy;
    int flag;
} scan[maxn << 1];

bool cmp(node a, node b) {
    return a.x < b.x;
}

struct tree {
    int l, r, sum;
    ll len;
} tr[maxn << 2];

void build(int o, int l, int r) {
    tr[o].l = l, tr[o].r = r;
    tr[o].len = tr[o].sum = 0;
    if (l == r) return;
    int mid = (l + r) >> 1;
    build(o << 1, l, mid);
    build(o << 1 | 1, mid + 1, r);
}

void push(int o) {
    int l = tr[o].l, r = tr[o].r;
    if (tr[o].sum) tr[o].len = Y[r + 1] - Y[l];
    else tr[o].len = tr[o << 1].len + tr[o << 1 | 1].len;
}

void change(int o, ll L, ll R, int c) {
    int l = tr[o].l, r = tr[o].r;
    if (R <= Y[l] || Y[r + 1] <= L) return;
    if (L <= Y[l] && Y[r + 1] <= R) {
        tr[o].sum += c;
        push(o);
        return;
    }
    change(o << 1, L, R, c);
    change(o << 1 | 1, L, R, c);
    push(o);
}

int main() {
    read(n);
    for (int i = 1; i <= n; i++) {
        read(x), read(y), read(xx), read(yy);
        scan[++cnt].x = x;
        scan[cnt].y = y;
        scan[cnt].yy = yy;
        scan[cnt].flag = 1;
        Y[cnt] = y;
        scan[++cnt].x = xx;
        scan[cnt].y = y;
        scan[cnt].yy = yy;
        scan[cnt].flag = -1;
        Y[cnt] = yy;
    }
    sort(scan + 1, scan + cnt + 1, cmp);
    sort(Y + 1, Y + cnt + 1);
    int tot = unique(Y + 1, Y + cnt + 1) - (Y + 1);
    build(1, 1, tot - 1);
    ll ans = 0;
    for (int i = 1; i < cnt; i++) {
        change(1, scan[i].y, scan[i].yy, scan[i].flag);
        ans += tr[1].len *(scan[i + 1].x - scan[i].x);
    }
    write(ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值