Submission #1056668

# Submission time Handle Problem Language Result Execution time Memory
1056668 2024-08-13T10:31:37 Z vjudge1 Digital Circuit (IOI22_circuit) C++17
100 / 100
662 ms 37076 KB
#include "circuit.h"

#include <bits/stdc++.h>
using namespace std;

using vi = vector<int>;
using ll = long long;
using vll = vector<ll>;
const int MOD = 1e9 + 2022;

vi W, A;
int n0;

struct AINT {
    int n;
    vi S0, S1, Lz;
    
    AINT() {}
    AINT(int N) : n(N), S0(4 * N, 0), S1(4 * N, 0), Lz(4 * N, 0) {}

    void update(int p, int v0, int v1, int u, int s, int d) {
        if(d < p || p < s) return;
        if(s == d) {
            S0[u] = v0;
            S1[u] = v1;
            return;
        }
        update(p, v0, v1, u << 1, s, (s + d) >> 1);
        update(p, v0, v1, u << 1 | 1, ((s + d) >> 1) + 1, d);
        S0[u] = (S0[u << 1] + S0[u << 1 | 1]) % MOD;
        S1[u] = (S1[u << 1] + S1[u << 1 | 1]) % MOD;
    }

    void update(int p, int v0, int v1) {
        update(p, v0, v1, 1, 0, n - 1);
    }

    void prop(int u, int s, int d) {
        if(!Lz[u]) return;
        swap(S0[u], S1[u]);
        if(s != d) {
            Lz[u << 1] ^= 1;
            Lz[u << 1 | 1] ^= 1;
        }
        Lz[u] = 0;
    }

    void toggle(int l, int r, int u, int s, int d) {
        prop(u, s, d);
        if(r < s || d < l) return;
        if(l <= s && d <= r) {
            Lz[u] ^= 1;
            prop(u, s, d);
            return;
        }
        toggle(l, r, u << 1, s, (s + d) >> 1);
        toggle(l, r, u << 1 | 1, ((s + d) >> 1) + 1, d);
        S0[u] = (S0[u << 1] + S0[u << 1 | 1]) % MOD;
        S1[u] = (S1[u << 1] + S1[u << 1 | 1]) % MOD;
    }

    void toggle(int l, int r) {
        toggle(l, r, 1, 0, n - 1);
    }

    int query() { return S1[1]; }
};

AINT Sol;

void init(int n, int m, vi P, vi A0) {
    n0 = n;
    A = A0;
    vector<vi> G(n + m);
    vi NR(n + m, 0);
    for(int i = 1; i < n + m; ++i) {
        G[P[i]].push_back(i);
    }

    function<void(int)> dfs0 = [&](int u) {
        NR[u] = 1;
        if(u >= n) {
            return;
        }
        for(auto it : G[u]) {
            dfs0(it);
            NR[u] = 1ll * NR[u] * NR[it] % MOD;
        }
        NR[u] = 1ll * NR[u] * G[u].size() % MOD;
    };
    dfs0(0);

    W.resize(m, 0);
    function<void(int, int)> dfs = [&](int u, int v) {
        if(u >= n) {
            W[u - n] = v;
            return;
        }
        vi Pref, Suf;
        for(auto it : G[u]) {
            Pref.push_back(NR[it]);
            Suf.push_back(NR[it]);
        }
        int nr = Pref.size();
        for(int i = 1; i < nr; ++i)
            Pref[i] = 1ll * Pref[i - 1] * Pref[i] % MOD;
        for(int i = nr - 2; i >= 0; --i)
            Suf[i] = 1ll * Suf[i + 1] * Suf[i] % MOD;
        for(int i = 0; i < nr; ++i) {
            ll f = 1;
            if(i) f *= Pref[i - 1];
            if(i + 1 < nr) f *= Suf[i + 1];
            f %= MOD;
            dfs(G[u][i], f * v % MOD);
        }
    };
    dfs(0, 1);
    Sol = AINT(m);
    for(int i = 0; i < m; ++i) {
        if(A[i]) Sol.update(i, 0, W[i]);
        else Sol.update(i, W[i], 0);
    }
}


int count_ways(int l, int r) {
    l -= n0; r -= n0;
    Sol.toggle(l, r);
    return Sol.query();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 0 ms 600 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 0 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 432 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 371 ms 5720 KB Output is correct
2 Correct 559 ms 11088 KB Output is correct
3 Correct 536 ms 11096 KB Output is correct
4 Correct 560 ms 11096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 371 ms 5720 KB Output is correct
2 Correct 559 ms 11088 KB Output is correct
3 Correct 536 ms 11096 KB Output is correct
4 Correct 560 ms 11096 KB Output is correct
5 Correct 443 ms 5720 KB Output is correct
6 Correct 575 ms 11096 KB Output is correct
7 Correct 610 ms 11096 KB Output is correct
8 Correct 580 ms 11096 KB Output is correct
9 Correct 257 ms 744 KB Output is correct
10 Correct 529 ms 916 KB Output is correct
11 Correct 512 ms 1112 KB Output is correct
12 Correct 524 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 371 ms 5720 KB Output is correct
14 Correct 559 ms 11088 KB Output is correct
15 Correct 536 ms 11096 KB Output is correct
16 Correct 560 ms 11096 KB Output is correct
17 Correct 443 ms 5720 KB Output is correct
18 Correct 575 ms 11096 KB Output is correct
19 Correct 610 ms 11096 KB Output is correct
20 Correct 580 ms 11096 KB Output is correct
21 Correct 257 ms 744 KB Output is correct
22 Correct 529 ms 916 KB Output is correct
23 Correct 512 ms 1112 KB Output is correct
24 Correct 524 ms 856 KB Output is correct
25 Correct 591 ms 16572 KB Output is correct
26 Correct 645 ms 16700 KB Output is correct
27 Correct 662 ms 16720 KB Output is correct
28 Correct 491 ms 16884 KB Output is correct
29 Correct 582 ms 35500 KB Output is correct
30 Correct 526 ms 35592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 0 ms 600 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 0 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 432 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 314 ms 856 KB Output is correct
44 Correct 510 ms 856 KB Output is correct
45 Correct 490 ms 856 KB Output is correct
46 Correct 505 ms 1112 KB Output is correct
47 Correct 471 ms 1112 KB Output is correct
48 Correct 482 ms 1108 KB Output is correct
49 Correct 513 ms 1112 KB Output is correct
50 Correct 477 ms 1112 KB Output is correct
51 Correct 496 ms 932 KB Output is correct
52 Correct 492 ms 928 KB Output is correct
53 Correct 455 ms 1880 KB Output is correct
54 Correct 542 ms 1112 KB Output is correct
55 Correct 537 ms 856 KB Output is correct
56 Correct 508 ms 856 KB Output is correct
57 Correct 556 ms 928 KB Output is correct
58 Correct 571 ms 2136 KB Output is correct
59 Correct 554 ms 2136 KB Output is correct
60 Correct 485 ms 2136 KB Output is correct
61 Correct 529 ms 1108 KB Output is correct
62 Correct 477 ms 856 KB Output is correct
63 Correct 456 ms 876 KB Output is correct
64 Correct 530 ms 940 KB Output is correct
65 Correct 266 ms 744 KB Output is correct
66 Correct 546 ms 920 KB Output is correct
67 Correct 507 ms 856 KB Output is correct
68 Correct 506 ms 920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 0 ms 600 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 0 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 432 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 371 ms 5720 KB Output is correct
44 Correct 559 ms 11088 KB Output is correct
45 Correct 536 ms 11096 KB Output is correct
46 Correct 560 ms 11096 KB Output is correct
47 Correct 443 ms 5720 KB Output is correct
48 Correct 575 ms 11096 KB Output is correct
49 Correct 610 ms 11096 KB Output is correct
50 Correct 580 ms 11096 KB Output is correct
51 Correct 257 ms 744 KB Output is correct
52 Correct 529 ms 916 KB Output is correct
53 Correct 512 ms 1112 KB Output is correct
54 Correct 524 ms 856 KB Output is correct
55 Correct 591 ms 16572 KB Output is correct
56 Correct 645 ms 16700 KB Output is correct
57 Correct 662 ms 16720 KB Output is correct
58 Correct 491 ms 16884 KB Output is correct
59 Correct 582 ms 35500 KB Output is correct
60 Correct 526 ms 35592 KB Output is correct
61 Correct 314 ms 856 KB Output is correct
62 Correct 510 ms 856 KB Output is correct
63 Correct 490 ms 856 KB Output is correct
64 Correct 505 ms 1112 KB Output is correct
65 Correct 471 ms 1112 KB Output is correct
66 Correct 482 ms 1108 KB Output is correct
67 Correct 513 ms 1112 KB Output is correct
68 Correct 477 ms 1112 KB Output is correct
69 Correct 496 ms 932 KB Output is correct
70 Correct 492 ms 928 KB Output is correct
71 Correct 455 ms 1880 KB Output is correct
72 Correct 542 ms 1112 KB Output is correct
73 Correct 537 ms 856 KB Output is correct
74 Correct 508 ms 856 KB Output is correct
75 Correct 556 ms 928 KB Output is correct
76 Correct 571 ms 2136 KB Output is correct
77 Correct 554 ms 2136 KB Output is correct
78 Correct 485 ms 2136 KB Output is correct
79 Correct 529 ms 1108 KB Output is correct
80 Correct 477 ms 856 KB Output is correct
81 Correct 456 ms 876 KB Output is correct
82 Correct 530 ms 940 KB Output is correct
83 Correct 266 ms 744 KB Output is correct
84 Correct 546 ms 920 KB Output is correct
85 Correct 507 ms 856 KB Output is correct
86 Correct 506 ms 920 KB Output is correct
87 Correct 0 ms 344 KB Output is correct
88 Correct 399 ms 14676 KB Output is correct
89 Correct 616 ms 10328 KB Output is correct
90 Correct 593 ms 10328 KB Output is correct
91 Correct 582 ms 16976 KB Output is correct
92 Correct 651 ms 16984 KB Output is correct
93 Correct 618 ms 16968 KB Output is correct
94 Correct 628 ms 16976 KB Output is correct
95 Correct 644 ms 16976 KB Output is correct
96 Correct 620 ms 10612 KB Output is correct
97 Correct 610 ms 10612 KB Output is correct
98 Correct 543 ms 30552 KB Output is correct
99 Correct 655 ms 16720 KB Output is correct
100 Correct 645 ms 13452 KB Output is correct
101 Correct 627 ms 12284 KB Output is correct
102 Correct 632 ms 10584 KB Output is correct
103 Correct 612 ms 35408 KB Output is correct
104 Correct 625 ms 37076 KB Output is correct
105 Correct 610 ms 37072 KB Output is correct
106 Correct 564 ms 13400 KB Output is correct
107 Correct 612 ms 9564 KB Output is correct
108 Correct 576 ms 10128 KB Output is correct
109 Correct 597 ms 10840 KB Output is correct