답안 #692518

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
692518 2023-02-01T17:37:11 Z CatalinT 디지털 회로 (IOI22_circuit) C++17
100 / 100
2065 ms 23792 KB
#include "circuit.h"

using namespace std;

#include <iostream>
#include <vector>
#include <cassert>

template <std::uint32_t mod>
class modint {
private:
    std::uint32_t n;
public:
    modint() : n(0) {};
    modint(std::int64_t n_) : n((n_ >= 0 ? n_ : mod - (-n_) % mod) % mod) {};
    static constexpr std::uint32_t get_mod() { return mod; }
    std::uint32_t get() const { return n; }
    bool operator==(const modint& m) const { return n == m.n; }
    bool operator!=(const modint& m) const { return n != m.n; }
    modint& operator+=(const modint& m) { n += m.n; n = (n < mod ? n : n - mod); return *this; }
    modint& operator-=(const modint& m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; }
    modint& operator*=(const modint& m) { n = std::uint64_t(n) * m.n % mod; return *this; }
    modint operator+(const modint& m) const { return modint(*this) += m; }
    modint operator/(const modint& m) const { return modint(*this) * m.inv(); }
    modint operator-(const modint& m) const { return modint(*this) -= m; }
    modint operator*(const modint& m) const { return modint(*this) *= m; }
    modint inv() const { return (*this).pow(mod - 2); }
    modint pow(std::uint64_t b) const {
        modint ans = 1, m = modint(*this);
        while (b) {
            if (b & 1) ans *= m;
            m *= m;
            b >>= 1;
        }
        return ans;
    }
};

using Mint = modint<1'000'002'022>; 

int N, M;

vector<int> P;
vector<int> A;

vector<vector<int>> tree;

vector<Mint> weight;
vector<Mint> tot_prod;

struct SQRT {
    struct Block {
        Mint cur_sum;
        Mint tot_sum;
        int rev;

        int l, r;

        void build(bool first = false) {
            if (first) tot_sum = 0;
            cur_sum = 0;
            for (int i = l; i <= r; i++) {
                A[i] ^= rev;
                if (first) tot_sum += weight[i]; // can only use once
                cur_sum += weight[i] * A[i];
            }
            rev = 0;
        }

        void flip() {
            rev ^= 1;
            cur_sum = tot_sum - cur_sum;
        }
    };

    int BLOCK;
    int NBLOCK;
    vector<Block> blocks;

    Mint ans;

    SQRT() {;}

    SQRT(int _BLOCK) {
        BLOCK = _BLOCK;
        NBLOCK = (M + BLOCK - 1) / BLOCK;

        blocks.resize(NBLOCK);

        cerr << NBLOCK << " / " << BLOCK << "\n";

        int l = 0, r = 0;
        for (int i = 0; i < NBLOCK; i++) {
            r = min(M - 1, l + BLOCK - 1);
            blocks[i] = Block {0, 0, 0, l, r};
            l += BLOCK;
            blocks[i].build(true);
            ans += blocks[i].cur_sum;
            // cerr << blocks[i].l << " - " << blocks[i].r << "\n";
        }
    }

    Mint query(int L, int R) {
        int s = L / BLOCK;
        int e = R / BLOCK;

        // Mint ans = 0;

        blocks[s].build();
        if (e != s)
            blocks[e].build();

        for (int i = s; i <= e; i++)
            ans -= blocks[i].cur_sum;

        for (int i = L; i <= min(blocks[s].r, R); i++) {
            blocks[s].cur_sum -= weight[i] * A[i];
            A[i] ^= 1;
            blocks[s].cur_sum += weight[i] * A[i];
            // cerr << i << " " << A[i] << "\n";
        }

        for (int i = s + 1; i <= e - 1; i++) {
            blocks[i].flip();
        }

        if (e > s) {
            for (int i = blocks[e].l; i <= R; i++) {
                blocks[e].cur_sum -= weight[i] * A[i];
                A[i] ^= 1;
                blocks[e].cur_sum += weight[i] * A[i];
            }
        }

        for (int i = s; i <= e; i++)
            ans += blocks[i].cur_sum;

        return ans;
    }
};

SQRT ds;

Mint dfs1(int v) {
    if (v >= N) {
        return tot_prod[v] = 1;
    }

    Mint & res = tot_prod[v];
    res = size(tree[v]);
    for (auto u : tree[v]) {
        res *= dfs1(u);
    }
    return res;
}

void dfs2(int v, Mint w) {
    if (v >= N) {
        weight[v - N] = w;
        return;
    }

    int n = size(tree[v]);

    assert(n);

    vector<Mint> suf(n);

    suf[n-1] = tot_prod[tree[v].back()];
    for (int i = n - 2; i >= 0; i--) {
        suf[i] = tot_prod[tree[v][i]] * suf[i+1];
    }    

    Mint pref = 1;
    for (int i = 0; i < n; i++) {
        Mint level = (i + 1 < n ? suf[i+1]: 1);
        level *= pref;
        dfs2(tree[v][i], w * level); 
        pref *= tot_prod[tree[v][i]];
    }
}

void init(int N, int M, std::vector<int> P, std::vector<int> A) {
    ::N = N;
    ::M = M;
    ::P = P;
    ::A = A;

    tree.resize(N);

    for (int i = 1; i < N + M; i++) {
        tree[P[i]].push_back(i);
    }

    tot_prod.resize(N + M);
    dfs1(0);
    weight.resize(M);
    dfs2(0, 1);

    ds = SQRT(400);
}

int count_ways(int L, int R) {
    L -= N;
    R -= N;
    return ds.query(L, R).get();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 304 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 208 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
37 Correct 1 ms 464 KB Output is correct
38 Correct 1 ms 464 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 671 ms 3620 KB Output is correct
2 Correct 969 ms 6932 KB Output is correct
3 Correct 918 ms 6984 KB Output is correct
4 Correct 1006 ms 6960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 671 ms 3620 KB Output is correct
2 Correct 969 ms 6932 KB Output is correct
3 Correct 918 ms 6984 KB Output is correct
4 Correct 1006 ms 6960 KB Output is correct
5 Correct 1175 ms 3668 KB Output is correct
6 Correct 1518 ms 6932 KB Output is correct
7 Correct 1952 ms 6940 KB Output is correct
8 Correct 1951 ms 6960 KB Output is correct
9 Correct 761 ms 464 KB Output is correct
10 Correct 1472 ms 720 KB Output is correct
11 Correct 1546 ms 592 KB Output is correct
12 Correct 1358 ms 592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 671 ms 3620 KB Output is correct
14 Correct 969 ms 6932 KB Output is correct
15 Correct 918 ms 6984 KB Output is correct
16 Correct 1006 ms 6960 KB Output is correct
17 Correct 1175 ms 3668 KB Output is correct
18 Correct 1518 ms 6932 KB Output is correct
19 Correct 1952 ms 6940 KB Output is correct
20 Correct 1951 ms 6960 KB Output is correct
21 Correct 761 ms 464 KB Output is correct
22 Correct 1472 ms 720 KB Output is correct
23 Correct 1546 ms 592 KB Output is correct
24 Correct 1358 ms 592 KB Output is correct
25 Correct 1831 ms 10264 KB Output is correct
26 Correct 1879 ms 10424 KB Output is correct
27 Correct 1599 ms 10436 KB Output is correct
28 Correct 1749 ms 10472 KB Output is correct
29 Correct 1659 ms 22964 KB Output is correct
30 Correct 1958 ms 22884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 304 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 208 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
37 Correct 1 ms 464 KB Output is correct
38 Correct 1 ms 464 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 208 KB Output is correct
43 Correct 917 ms 592 KB Output is correct
44 Correct 1403 ms 592 KB Output is correct
45 Correct 1264 ms 592 KB Output is correct
46 Correct 987 ms 720 KB Output is correct
47 Correct 1336 ms 720 KB Output is correct
48 Correct 1360 ms 720 KB Output is correct
49 Correct 1525 ms 720 KB Output is correct
50 Correct 1406 ms 720 KB Output is correct
51 Correct 1519 ms 492 KB Output is correct
52 Correct 1463 ms 488 KB Output is correct
53 Correct 876 ms 1232 KB Output is correct
54 Correct 1416 ms 720 KB Output is correct
55 Correct 1447 ms 592 KB Output is correct
56 Correct 1500 ms 572 KB Output is correct
57 Correct 1455 ms 464 KB Output is correct
58 Correct 1353 ms 1360 KB Output is correct
59 Correct 1442 ms 1360 KB Output is correct
60 Correct 1621 ms 1360 KB Output is correct
61 Correct 1412 ms 788 KB Output is correct
62 Correct 1380 ms 464 KB Output is correct
63 Correct 1543 ms 464 KB Output is correct
64 Correct 1591 ms 464 KB Output is correct
65 Correct 715 ms 464 KB Output is correct
66 Correct 1480 ms 592 KB Output is correct
67 Correct 1508 ms 592 KB Output is correct
68 Correct 1452 ms 592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 304 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 208 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
37 Correct 1 ms 464 KB Output is correct
38 Correct 1 ms 464 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 208 KB Output is correct
43 Correct 671 ms 3620 KB Output is correct
44 Correct 969 ms 6932 KB Output is correct
45 Correct 918 ms 6984 KB Output is correct
46 Correct 1006 ms 6960 KB Output is correct
47 Correct 1175 ms 3668 KB Output is correct
48 Correct 1518 ms 6932 KB Output is correct
49 Correct 1952 ms 6940 KB Output is correct
50 Correct 1951 ms 6960 KB Output is correct
51 Correct 761 ms 464 KB Output is correct
52 Correct 1472 ms 720 KB Output is correct
53 Correct 1546 ms 592 KB Output is correct
54 Correct 1358 ms 592 KB Output is correct
55 Correct 1831 ms 10264 KB Output is correct
56 Correct 1879 ms 10424 KB Output is correct
57 Correct 1599 ms 10436 KB Output is correct
58 Correct 1749 ms 10472 KB Output is correct
59 Correct 1659 ms 22964 KB Output is correct
60 Correct 1958 ms 22884 KB Output is correct
61 Correct 917 ms 592 KB Output is correct
62 Correct 1403 ms 592 KB Output is correct
63 Correct 1264 ms 592 KB Output is correct
64 Correct 987 ms 720 KB Output is correct
65 Correct 1336 ms 720 KB Output is correct
66 Correct 1360 ms 720 KB Output is correct
67 Correct 1525 ms 720 KB Output is correct
68 Correct 1406 ms 720 KB Output is correct
69 Correct 1519 ms 492 KB Output is correct
70 Correct 1463 ms 488 KB Output is correct
71 Correct 876 ms 1232 KB Output is correct
72 Correct 1416 ms 720 KB Output is correct
73 Correct 1447 ms 592 KB Output is correct
74 Correct 1500 ms 572 KB Output is correct
75 Correct 1455 ms 464 KB Output is correct
76 Correct 1353 ms 1360 KB Output is correct
77 Correct 1442 ms 1360 KB Output is correct
78 Correct 1621 ms 1360 KB Output is correct
79 Correct 1412 ms 788 KB Output is correct
80 Correct 1380 ms 464 KB Output is correct
81 Correct 1543 ms 464 KB Output is correct
82 Correct 1591 ms 464 KB Output is correct
83 Correct 715 ms 464 KB Output is correct
84 Correct 1480 ms 592 KB Output is correct
85 Correct 1508 ms 592 KB Output is correct
86 Correct 1452 ms 592 KB Output is correct
87 Correct 0 ms 208 KB Output is correct
88 Correct 818 ms 9484 KB Output is correct
89 Correct 1454 ms 7320 KB Output is correct
90 Correct 1635 ms 6864 KB Output is correct
91 Correct 1055 ms 10556 KB Output is correct
92 Correct 1728 ms 10556 KB Output is correct
93 Correct 1606 ms 10596 KB Output is correct
94 Correct 1529 ms 10584 KB Output is correct
95 Correct 2065 ms 10548 KB Output is correct
96 Correct 1839 ms 4288 KB Output is correct
97 Correct 1486 ms 4288 KB Output is correct
98 Correct 772 ms 19872 KB Output is correct
99 Correct 1638 ms 10444 KB Output is correct
100 Correct 1619 ms 6904 KB Output is correct
101 Correct 1564 ms 5652 KB Output is correct
102 Correct 1488 ms 3888 KB Output is correct
103 Correct 1587 ms 22928 KB Output is correct
104 Correct 1599 ms 23792 KB Output is correct
105 Correct 1785 ms 23696 KB Output is correct
106 Correct 1474 ms 9756 KB Output is correct
107 Correct 1537 ms 5188 KB Output is correct
108 Correct 1640 ms 4944 KB Output is correct
109 Correct 1565 ms 4024 KB Output is correct