Hướng dẫn giải của Bedao OI Contest 5 - Lại là diện tích hình chữ nhật


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#ifdef LOCAL
#include </mnt/d/Cp/Lib/debug.h>
#else
#define print(...) void(2024927) /* ignore */
#define debug(...) void(2024927) /* ignore */
#endif

const int N = 2e5 + 5;

int n, l, r;

struct Compress : vector<ll> {
    void build() { sort(begin(), end()); erase(unique(begin(), end()), end()); }
    int prod(int val) { return lower_bound(begin(), end(), val) - begin(); }
};

struct Rect { ll x, y, u, v; } rect[N];


ll solve() {
    // Init 
    Compress cx, cy;
    {
        ll mx = rect[1].x, my = rect[1].y;
        for(int i = 1; i <= n; ++i) {
            mx = min(mx, rect[i].x);
            my = min(my, rect[i].y);
        }
        for(int i = 1; i <= n; ++i) {
            cx.push_back(rect[i].x);
            cx.push_back(rect[i].u);
            cy.push_back(rect[i].y);
            cy.push_back(rect[i].v);
            cy.push_back(rect[i].v + 1);
            cx.push_back(rect[i].u + 1);
        }
        cx.build(); cy.build();
        for(int i = 1; i <= n; ++i) {
            rect[i].x = cx.prod(rect[i].x);
            rect[i].u = cx.prod(rect[i].u);
            rect[i].y = cy.prod(rect[i].y);
            rect[i].v = cy.prod(rect[i].v);
        }
    }

    int X = cx.size(), Y = cy.size();
    cx.push_back(cx.back());
    cy.push_back(cy.back());
    int B = 900;
    int SZ = (X - 1) / B;

    vector<vector<pair<int, int>>> FullBlock(SZ + 1, vector<pair<int, int>>(Y + 5));
    vector<vector<vector<tuple<int, int, int>>>> HalfBlock(SZ + 1, vector<vector<tuple<int, int, int>>>(Y + 5));

    vector<pair<int, int>> range(SZ + 1, pair<int, int>(X, 0));
    for(int i = 0; i < X; ++i) {
        range[i / B].first = min(range[i / B].first, i);
        range[i / B].second = max(range[i / B].second, i);
    }

    auto UpdateHalfBlock = [&] (int id, int l, int r, int u, int v) -> void {
        l = max(l, range[id].first);
        r = min(r, range[id].second);
        HalfBlock[id][u].push_back({l, r, 1});
        HalfBlock[id][v].push_back({l, r, -1});
    };

    auto UpdateFullBlock = [&] (int id, int u, int v) -> void { 
        FullBlock[id][u].first++;
        FullBlock[id][v].second++;
    };

    for(int i = 1; i <= n; ++i) {
        int l = rect[i].x, r = rect[i].u;
        int lid = l / B, rid = r / B;
        if(lid == rid) {
            UpdateHalfBlock(lid, l, r, rect[i].y, rect[i].v);
        } else {
            for(int f = lid + 1; f < rid; ++f) {
                UpdateFullBlock(f, rect[i].y, rect[i].v);
            }
            UpdateHalfBlock(lid, l, r, rect[i].y, rect[i].v);
            UpdateHalfBlock(rid, l, r, rect[i].y, rect[i].v);
        }
    }

    ll ans = 0;
    for(int b = 0; b <= SZ; ++b) {
        ll sum = 0;
        int L = l, R = r;
        vector<int> cnt(n + 1), freq(X + 1);
        auto S_Add = [&] (int i) -> void {
            if(i < 0 || i > n) return;
            sum += cnt[i];
        };
        auto S_Remove = [&] (int i) -> void {
            if(i < 0 || i > n) return;
            sum -= cnt[i];
        };
        auto C_Add = [&] (int i) -> void {
            int t = cx[i + 1] - cx[i];
            cnt[freq[i]] -= t;
            if(L <= freq[i] && freq[i] <= R) sum -= t;
            ++freq[i];
            if(L <= freq[i] && freq[i] <= R) sum += t;
            cnt[freq[i]] += t;
        };
        auto C_Remove = [&] (int i) -> void {
            int t = cx[i + 1] - cx[i];
            cnt[freq[i]] -= t;
            if(L <= freq[i] && freq[i] <= R) sum -= t;
            --freq[i];
            if(L <= freq[i] && freq[i] <= R) sum += t;
            cnt[freq[i]] += t;
        };

        cnt[0] = cx[range[b].second + 1] - cx[range[b].first];

        for(int i = 0; i < Y; ++i) {
            for(auto[lx, rx, t] : HalfBlock[b][i]) {
                if(t == 1) {
                    for(int _ = lx; _ <= rx; ++_) {
                        C_Add(_);
                    }
                }
            }
            for(int _ = 0; _ < FullBlock[b][i].first; ++_) S_Add(--L), S_Remove(R--);
            if(i + 1 < Y) {
                assert(cy[i + 1] > cy[i]);
                ans += sum * (cy[i + 1] - cy[i]);
            }
            for(auto[lx, rx, t] : HalfBlock[b][i]) {
                if(t == -1) {
                    for(int _ = lx; _ <= rx; ++_) {
                        C_Remove(_);
                    }
                }
            }
            for(int _ = 0; _ < FullBlock[b][i].second; ++_) S_Add(++R), S_Remove(L++);
        }
    }
    return ans;
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    debug(file());
    freopen("rect.inp", "r", stdin);
    freopen("rect.out", "w", stdout);

    // input
    cin >> n >> l >> r;
    for(int i = 1; i <= n; ++i) {
        cin >> rect[i].x >> rect[i].y >> rect[i].u >> rect[i].v;
    }

    // solve
    ll ans = 0;

    ans += solve();

    cout << ans << '\n';
    return 0;
}

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.