QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#523326#8809. Telephone Planslwm77080 0ms3876kbC++173.6kb2024-08-18 06:48:512024-08-18 06:48:52

Judging History

你现在查看的是最新测评结果

  • [2024-08-18 06:48:52]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:3876kb
  • [2024-08-18 06:48:51]
  • 提交

answer

#include <cassert>
#include <cstdint>
#include <ios>
#include <iostream>
#include <numeric>
#include <set>
#include <utility>
#include <vector>

template <typename F>
class y_combinator {

private:

    F f;

public:

    explicit y_combinator(F&& f) : f(f) {}

    template <typename... Args>
    decltype(auto) operator()(Args&&... args) const {

        return f(*this, std::forward<Args>(args)...);

    }

};

template <typename F>
y_combinator(F) -> y_combinator<F>;

void solve() {

    bool e;
    std::int32_t n;
    std::int32_t q;

    std::cin >> e >> n >> q;

    std::vector<std::set<std::int32_t>> adj(n);
    std::vector<std::int32_t> comps(n);
    std::vector<std::set<std::int32_t>> grps(n);
    std::vector<std::int64_t> pfxs(q + 1);
    std::int64_t prv = 0;
    std::int64_t tot_pairs = 0;

    std::iota(std::begin(comps), std::end(comps), 0);

    for (std::int32_t i = 0; i < n; ++i) {
        grps[i].insert(i);
    }

    for (std::int32_t i = 0; i < q; ++i) {
        pfxs[i + 1] = pfxs[i];
        std::int32_t tp = 0;
        std::cin >> tp;
        if (tp == 1) {
            std::int64_t x;
            std::int64_t y;
            std::cin >> x >> y;
            if (e) {
                x ^= prv;
                y ^= prv;
            }
            --x;
            --y;
            adj[x].insert(y);
            adj[y].insert(x);
            x = comps[x];
            y = comps[y];
            const std::int64_t sz_1 = std::size(grps[x]);
            const std::int64_t sz_2 = std::size(grps[y]);
            tot_pairs += sz_1 * sz_2;
            if (sz_1 < sz_2) {
                std::swap(x, y);
            }
            for (auto z : grps[y]) {
                comps[z] = x;
            }
            grps[x].merge(grps[y]);
            assert(std::empty(grps[x]));
        } else if (tp == 2) {
            using grp_t = std::vector<std::int32_t>;
            std::int64_t x;
            std::int64_t y;
            std::cin >> x >> y;
            if (e) {
                x ^= prv;
                y ^= prv;
            }
            --x;
            --y;
            adj[x].erase(y);
            adj[y].erase(x);
            const auto dfs = y_combinator(
                [&](auto self, std::int32_t node, std::int32_t par, grp_t& grp) -> void {
                    grp.push_back(node);
                    for (auto x : adj[node]) {
                        if (x != par) {
                            self(x, node, grp);
                        }
                    }
                }
            );
            std::set<std::int32_t>& grp = grps[comps[x]];
            grp_t grp_1;
            grp_t grp_2;
            dfs(x, -1, grp_1);
            dfs(y, -1, grp_2);
            const std::int64_t sz_1 = std::size(grp_1);
            const std::int64_t sz_2 = std::size(grp_2);
            assert(sz_1 + sz_2 == std::int32_t(std::size(grp)));
            pfxs[i + 1] += sz_1 * sz_2;
            if (sz_1 < sz_2) {
                std::swap(grp_1, grp_2);
            }
            for (auto z : grp_2) {
                grp.erase(z);
                comps[z] = std::size(grps);
            }
            grps.emplace_back(std::begin(grp_2), std::end(grp_2));
        } else {
            std::int64_t t;
            std::cin >> t;
            if (e) {
                t ^= prv;
            }
            prv = tot_pairs - pfxs[i - t];
            std::cout << prv << '\n';
        }
    }

}

int main() {

    std::cin.tie(nullptr);

    std::ios_base::sync_with_stdio(false);

    solve();

    return 0;

}

详细

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 3
Accepted
time: 0ms
memory: 3876kb

input:

0
1 147
3 0
3 0
3 1
3 1
3 0
3 5
3 5
3 1
3 1
3 4
3 8
3 2
3 10
3 13
3 10
3 8
3 8
3 0
3 16
3 3
3 1
3 20
3 2
3 10
3 16
3 13
3 17
3 12
3 22
3 7
3 8
3 2
3 12
3 32
3 12
3 31
3 2
3 0
3 21
3 24
3 28
3 32
3 9
3 18
3 26
3 11
3 45
3 35
3 14
3 34
3 49
3 31
3 43
3 11
3 21
3 50
3 4
3 11
3 31
3 51
3 28
3 26
3 18
3 ...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 147 lines

Test #2:

score: 0
Runtime Error

input:

0
2 10
1 1 2
3 1
3 1
3 2
3 3
3 3
3 3
2 1 2
3 2
3 3

output:


result:


Subtask #2:

score: 0
Runtime Error

Test #29:

score: 2
Accepted
time: 0ms
memory: 3600kb

input:

1
1 147
3 0
3 0
3 1
3 1
3 3
3 0
3 6
3 6
3 0
3 2
3 0
3 5
3 12
3 1
3 2
3 10
3 13
3 15
3 3
3 12
3 20
3 18
3 10
3 12
3 2
3 12
3 14
3 26
3 12
3 24
3 7
3 7
3 6
3 29
3 32
3 16
3 23
3 14
3 25
3 13
3 13
3 31
3 20
3 26
3 0
3 40
3 23
3 28
3 35
3 1
3 31
3 2
3 34
3 37
3 3
3 39
3 17
3 4
3 41
3 11
3 16
3 48
3 10
3...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 147 lines

Test #30:

score: 0
Runtime Error

input:

1
2 10
1 1 2
3 1
3 1
3 1
3 1
3 1
3 2
3 6
2 0 3
3 2

output:


result:


Subtask #3:

score: 0
Skipped

Dependency #1:

0%

Subtask #4:

score: 0
Skipped

Dependency #2:

0%

Subtask #5:

score: 0
Skipped

Dependency #3:

0%

Subtask #6:

score: 0
Skipped

Dependency #4:

0%

Subtask #7:

score: 0
Skipped

Dependency #6:

0%