【扫描线】样例没过求调
查看原帖
【扫描线】样例没过求调
481330
sunyizhe还是MC大佬楼主2025/8/30 20:18
//程序算法:线段树,离散化
#include <bits/stdc++.h>

namespace IO{
    void file() {
        freopen("input.in", "r", stdin);
        freopen("output.out", "w", stdout);
    }

    #define gc getchar
    #define pc putchar

    template <typename T>
    inline void read(T &x) {
        x = 0;
        bool f = 0;
        T s = 1;
        char ch = gc();
        while (!isdigit(ch) && ~ch) {
            f = (ch == '-');
            ch = gc();
        }

        while (isdigit(ch)) {
            x = x * 10 + (ch - 48);
            ch = gc();
        }

        if (ch == '.') {
            ch = gc();
            while (isdigit(ch)) {
                s *= 0.1;
                x += (ch - 48) * s;
                ch = gc();
            }
        }
        if (f) x = -x;
    }

    template <typename T>
    inline void write(T x) {
        if (x < 0) {
            pc('-');
            x = -x;
        }

        if (x >= 10) write(x / 10);
        pc(x % 10 + 48);
    }

    using std::string;
    inline void read(string &s) {
        s.clear();
        char ch = gc();
        while (isspace(ch)) ch = gc();
        while (!isspace(ch) && ~ch) s += ch, ch = gc();
    }

    inline void read(char &ch) {
        ch = gc();
        while (isspace(ch)) ch = gc();
    }

    inline void write(const string s) {
        for (auto ch : s) pc(ch);
    }

    inline void write(const char ch) {
        pc(ch);
    }

    inline void write(const double x) {
        printf("%.3Lf", (long double)x);
    }

    inline void write(const char *s) {
        while (*s) pc(*(s++));
    }

    template <typename T, typename ...t>
    inline void read(T &x, t &...y) {
        read(x);
        read(y...);
    }

    template <typename T, typename ...t>
    inline void write(T x, t ...y) {
        write(x);
        write(y...);
    }
}

using namespace IO;
using namespace std;
using ll = long long;
const int N = 1e5 + 10;

struct Line{
    int xl, xr, y, in;
    friend bool operator < (const Line& x, const Line& y) {
        return x.y < y.y;
    }
}line[N << 1];

struct Node{
    int l, r;
    int cnt, len;
}t[N << 2];

int n, tot, x[N];

void pushup(int rt) {
    if (t[rt].cnt) t[rt].len = x[t[rt].r] - x[t[rt].l - 1]; //线段编号[l,r] -> 点的编号[l-1, r]
    else if (t[rt].l == t[rt].r) t[rt].len = 0;
    else t[rt].len = t[rt * 2].len + t[rt * 2 + 1].len;
}

void build(int rt, int l, int r) {
    t[rt].l = l, t[rt].r = r;
    if (l == r) return;

    int mid = (l + r) >> 1;
    build(rt * 2, l, mid);
    build(rt * 2 + 1, mid + 1, r);
}

void modify(int rt, int l, int r, int v) {
    if (t[rt].l >= l && t[rt].r <= r) {
        t[rt].cnt += v;
        pushup(rt);
        return;
    }

    int mid = (t[rt].l + t[rt].r) >> 1;
    if (l <= mid) modify(rt * 2, l, r, v);
    if (r > mid) modify(rt * 2 + 1, l, r, v);
    
    pushup(rt);
}

int main() {
    read(n);
    for (int i = 1; i <= n; i++) {
        int xl, yl, xr, yr;
        read(xl, yl, xr, yr);
        x[++tot] = xl;
        line[tot] = {xl, xr, yl, 1};
        x[++tot] = xr;
        line[tot] = {xl, xr, yr, -1};
    }

    n = tot;
    sort(line + 1, line + tot + 1);
    sort(x + 1, x + n + 1);
    tot = unique(x + 1, x + n + 1) - x - 1;

    ll ans = 0;
    build(1, 1, tot - 1);
    for (int i = 1; i < n; i++) {
        int l = lower_bound(x + 1, x + n + 1, line[i].xl) - x;
        int r = lower_bound(x + 1, x + n + 1, line[i].xr) - x;
        modify(1, l + 1, r, line[i].in); //点的编号[l,r]->线段编号[l+1, r]
        ans += 1LL * t[1].len * (line[i + 1].y - line[i].y);
    }
    write(ans);
    return 0;
}
2025/8/30 20:18
加载中...