답안 #403691

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
403691 2021-05-13T11:19:51 Z KoD Collapse (JOI18_collapse) C++17
100 / 100
3653 ms 13844 KB
#include <bits/stdc++.h>
#include "collapse.h"
 
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;
 
class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const Iter first, last;
 
  public:
    explicit constexpr rep(const usize first, const usize last) noexcept : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};
 
class RollbackUnionFind {
    std::vector<usize> data;
    std::stack<std::pair<usize, usize>> history;
 
  public:
    explicit RollbackUnionFind(const usize size = 0) : data(size, -1), history() {}
 
    usize size() const { return data.size(); }
 
    usize leader(usize u) const {
        assert(u < size());
        while (data[u] < size()) u = data[u];
        return u;
    }
 
    usize size(const usize u) const {
        assert(u < size());
        return -data[leader(u)];
    }
 
    std::pair<usize, bool> merge(usize u, usize v) {
        assert(u < size());
        assert(v < size());
        u = leader(u);
        v = leader(v);
        if (u == v) return std::make_pair(u, false);
        if (data[u] > data[v]) std::swap(u, v);
        history.emplace(u, data[u]);
        history.emplace(v, data[v]);
        data[u] += data[v];
        data[v] = u;
        return std::make_pair(u, true);
    }
 
    bool same(const usize u, const usize v) const {
        assert(u < size());
        assert(v < size());
        return leader(u) == leader(v);
    }
 
    void rollback(const usize steps) {
        assert(2 * steps <= history.size());
        for (usize i = 2 * steps; i > 0; --i) {
            const auto [k, x] = history.top();
            history.pop();
            data[k] = x;
        }
    }
};
 
template <class T> using Vec = std::vector<T>;
 
constexpr usize BSIZE = 300;
 
Vec<int> calc(const usize N, Vec<int> T, Vec<int> X, Vec<int> Y, Vec<int> W, Vec<int> P) {
    const auto C = T.size();
    const auto Q = W.size();
    Vec<std::pair<int, int>> edge;
    edge.reserve(C);
    for (const auto i : rep(0, C)) {
        if (X[i] > Y[i]) {
            std::swap(X[i], Y[i]);
        }
        edge.emplace_back(Y[i], X[i]);
    }
    std::sort(edge.begin(), edge.end());
    edge.erase(std::unique(edge.begin(), edge.end()), edge.end());
    Vec<usize> eid(C);
    for (const auto i : rep(0, C)) {
        eid[i] = std::lower_bound(edge.begin(), edge.end(), std::make_pair(Y[i], X[i])) - edge.begin();
    }
    const auto Blocks = (C + BSIZE - 1) / BSIZE;
    Vec<Vec<usize>> qid(Blocks);
    for (const auto i : rep(0, Q)) {
        qid[W[i] / BSIZE].push_back(i);
    }
    Vec<int> ret(Q);
    for (const auto block : rep(0, Blocks)) {
        const auto low = BSIZE * block;
        const auto high = std::min(C, low + BSIZE);
        auto& qs = qid[block];
        std::sort(qs.begin(), qs.end(), [&](const usize i, const usize j) { return P[i] < P[j]; });
        Vec<char> usage(edge.size()), changes(edge.size());
        for (const auto i : rep(0, low)) {
            usage[eid[i]] ^= 1;
        }
        for (const auto i : rep(low, high)) {
            changes[eid[i]] = true;
        }
        Vec<usize> naive;
        for (const auto i : rep(0, edge.size())) {
            if (changes[i]) {
                naive.push_back(i);
            }
        }
        RollbackUnionFind dsu(N);
        usize comps = N, seen = 0;
        for (const auto i : qs) {
            while (seen < edge.size() and edge[seen].first <= P[i]) {
                if (!changes[seen] and usage[seen]) {
                    comps -= dsu.merge(edge[seen].first, edge[seen].second).second;
                }
                seen += 1;
            }
            const auto memo = comps;
            for (const auto j : rep(low, W[i] + 1)) {
                usage[eid[j]] ^= 1;
            }
            for (const auto e : naive) {
                if (usage[e] and edge[e].first <= P[i]) {
                    comps -= dsu.merge(edge[e].first, edge[e].second).second;
                }
            }
            for (const auto j : rep(low, W[i] + 1)) {
                usage[eid[j]] ^= 1;
            }
            ret[i] = comps;
            dsu.rollback(memo - comps);
            comps = memo;
        }
    }
    return ret;
}
 
Vec<int> simulateCollapse(int N, Vec<int> T, Vec<int> X, Vec<int> Y, Vec<int> W, Vec<int> P) {
    const auto C = T.size();
    const auto Q = W.size();
    auto ret = calc(N, T, X, Y, W, P);
    for (const auto i : rep(0, C)) {
        X[i] = N - X[i] - 1;
        Y[i] = N - Y[i] - 1;
    }
    for (const auto i : rep(0, Q)) {
        P[i] = N - P[i] - 2;
    }
    const auto tmp = calc(N, T, X, Y, W, P);
    for (const auto i : rep(0, Q)) {
        ret[i] += tmp[i];
        ret[i] -= N;
    }
    return ret;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 796 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 6 ms 460 KB Output is correct
4 Correct 7 ms 512 KB Output is correct
5 Correct 23 ms 776 KB Output is correct
6 Correct 38 ms 792 KB Output is correct
7 Correct 4 ms 460 KB Output is correct
8 Correct 5 ms 460 KB Output is correct
9 Correct 23 ms 856 KB Output is correct
10 Correct 51 ms 820 KB Output is correct
11 Correct 62 ms 936 KB Output is correct
12 Correct 57 ms 960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 4752 KB Output is correct
2 Correct 52 ms 4760 KB Output is correct
3 Correct 619 ms 10076 KB Output is correct
4 Correct 127 ms 4764 KB Output is correct
5 Correct 875 ms 9964 KB Output is correct
6 Correct 545 ms 4820 KB Output is correct
7 Correct 1142 ms 10360 KB Output is correct
8 Correct 1055 ms 10128 KB Output is correct
9 Correct 44 ms 5512 KB Output is correct
10 Correct 69 ms 5580 KB Output is correct
11 Correct 703 ms 5552 KB Output is correct
12 Correct 1114 ms 10916 KB Output is correct
13 Correct 1510 ms 11144 KB Output is correct
14 Correct 1777 ms 11776 KB Output is correct
15 Correct 1609 ms 12052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 4856 KB Output is correct
2 Correct 57 ms 4812 KB Output is correct
3 Correct 87 ms 4812 KB Output is correct
4 Correct 133 ms 4844 KB Output is correct
5 Correct 636 ms 4556 KB Output is correct
6 Correct 593 ms 4820 KB Output is correct
7 Correct 1219 ms 8724 KB Output is correct
8 Correct 2029 ms 10528 KB Output is correct
9 Correct 62 ms 5512 KB Output is correct
10 Correct 876 ms 5260 KB Output is correct
11 Correct 2957 ms 13464 KB Output is correct
12 Correct 3653 ms 13844 KB Output is correct
13 Correct 2920 ms 13084 KB Output is correct
14 Correct 3560 ms 13628 KB Output is correct
15 Correct 2868 ms 13144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 796 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 6 ms 460 KB Output is correct
4 Correct 7 ms 512 KB Output is correct
5 Correct 23 ms 776 KB Output is correct
6 Correct 38 ms 792 KB Output is correct
7 Correct 4 ms 460 KB Output is correct
8 Correct 5 ms 460 KB Output is correct
9 Correct 23 ms 856 KB Output is correct
10 Correct 51 ms 820 KB Output is correct
11 Correct 62 ms 936 KB Output is correct
12 Correct 57 ms 960 KB Output is correct
13 Correct 38 ms 4752 KB Output is correct
14 Correct 52 ms 4760 KB Output is correct
15 Correct 619 ms 10076 KB Output is correct
16 Correct 127 ms 4764 KB Output is correct
17 Correct 875 ms 9964 KB Output is correct
18 Correct 545 ms 4820 KB Output is correct
19 Correct 1142 ms 10360 KB Output is correct
20 Correct 1055 ms 10128 KB Output is correct
21 Correct 44 ms 5512 KB Output is correct
22 Correct 69 ms 5580 KB Output is correct
23 Correct 703 ms 5552 KB Output is correct
24 Correct 1114 ms 10916 KB Output is correct
25 Correct 1510 ms 11144 KB Output is correct
26 Correct 1777 ms 11776 KB Output is correct
27 Correct 1609 ms 12052 KB Output is correct
28 Correct 41 ms 4856 KB Output is correct
29 Correct 57 ms 4812 KB Output is correct
30 Correct 87 ms 4812 KB Output is correct
31 Correct 133 ms 4844 KB Output is correct
32 Correct 636 ms 4556 KB Output is correct
33 Correct 593 ms 4820 KB Output is correct
34 Correct 1219 ms 8724 KB Output is correct
35 Correct 2029 ms 10528 KB Output is correct
36 Correct 62 ms 5512 KB Output is correct
37 Correct 876 ms 5260 KB Output is correct
38 Correct 2957 ms 13464 KB Output is correct
39 Correct 3653 ms 13844 KB Output is correct
40 Correct 2920 ms 13084 KB Output is correct
41 Correct 3560 ms 13628 KB Output is correct
42 Correct 2868 ms 13144 KB Output is correct
43 Correct 1010 ms 10116 KB Output is correct
44 Correct 1616 ms 10276 KB Output is correct
45 Correct 1176 ms 10336 KB Output is correct
46 Correct 2037 ms 10656 KB Output is correct
47 Correct 62 ms 5512 KB Output is correct
48 Correct 85 ms 5624 KB Output is correct
49 Correct 810 ms 5608 KB Output is correct
50 Correct 1046 ms 6548 KB Output is correct
51 Correct 1275 ms 10944 KB Output is correct
52 Correct 1929 ms 11876 KB Output is correct
53 Correct 1860 ms 11712 KB Output is correct
54 Correct 2380 ms 12100 KB Output is correct
55 Correct 2201 ms 11932 KB Output is correct
56 Correct 2511 ms 12600 KB Output is correct
57 Correct 2693 ms 13240 KB Output is correct
58 Correct 3197 ms 13328 KB Output is correct
59 Correct 2962 ms 13320 KB Output is correct
60 Correct 3643 ms 13732 KB Output is correct