답안 #1071049

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1071049 2024-08-23T03:45:47 Z ProtonDecay314 디지털 회로 (IOI22_circuit) C++17
100 / 100
980 ms 130820 KB
// AM + DG
#include "circuit.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef vector<vpi> vvpi;
typedef vector<vpll> vvpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef short int si;
typedef vector<si> vsi;
typedef vector<vsi> vvsi;
#define IOS ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define L(varll, mn, mx) for(ll varll = (mn); varll < (mx); varll++)
#define LR(varll, mx, mn) for(ll varll = (mx); varll > (mn); varll--)
#define LI(vari, mn, mx) for(int vari = (mn); vari < (mx); vari++)
#define LIR(vari, mx, mn) for(int vari = (mx); vari > (mn); vari--)
#define INPV(varvec) for(auto& varveci : (varvec)) cin >> varveci
#define fi first
#define se second
#define pb push_back
#define INF(type) numeric_limits<type>::max()
#define NINF(type) numeric_limits<type>::min()
#define TCASES int t; cin >> t; while(t--)

// I was stuck in the paradigm that I can't divide
// But, I can! It's just a matter of whether or not I can deal with a nonprime modulus
const ll MODS[4] = {2ll, 223ll, 2'242'157ll, 1'000'002'022ll};

// Fast Modular Exponentiation
ll fpw(ll base, ll pw, ll md) {
    base = base % md;
    if(pw == 0ll) return 1ll;
    ll bsqr = fpw(base * base, pw >> 1ll, md);

    return (pw & 0b1ll ? (base * bsqr) % md : bsqr);
}

// Modular Multiplicative inverse
// ! CAREFUL, only works for prime modulus
ll inv(ll base, ll md) {
    return fpw(base, md - 2ll, md);
}

// This doesn't need to be as complicated as
// your code for pre-in-post
// Only multiplication and division are required
class ModInt {
    public:
        ll m; // Mantissa, guaranteed to be nonzero
        ll pw;
        ll md;

        ModInt(ll n, ll a_md): md(a_md) {
            ll cpw = 0ll;

            while((n % md) == 0ll) {
                n /= md;
                cpw++;
            }

            m = n;
            pw = cpw;
        }

        ModInt(ll a_m, ll a_pw, ll a_md): m(a_m), pw(a_pw), md(a_md) {
            assert(m != 0ll);
            assert(a_pw >= 0ll);
        }

        ModInt operator*(const ModInt& o) const {
            return {(m * o.m) % md, pw + o.pw, md};
        }

        ModInt operator/(const ModInt& o) const {
            return {(m * inv(o.m, md)) % md, pw - o.pw, md};
        }

        // Returns this number mod the modulus
        ll to_num() const {
            return (pw == 0ll ? m % md : 0ll);
        }
};

class CRTInt {
    public:
        ModInt m1, m2, m3;

        CRTInt(ll n): m1(n, MODS[0ll]), m2(n, MODS[1ll]), m3(n, MODS[2ll]) {};

        CRTInt(ModInt a_m1, ModInt a_m2, ModInt a_m3): m1(a_m1), m2(a_m2), m3(a_m3) {}

        CRTInt operator*(const CRTInt& o) const {
            return {m1 * o.m1, m2 * o.m2, m3 * o.m3};
        }
        
        CRTInt operator/(const CRTInt& o) const {
            return {m1 / o.m1, m2 / o.m2, m3 / o.m3};
        }

        ll to_num() const {
            ll res = 0ll;

            ll f1 = fpw(MODS[1ll] * MODS[2ll], MODS[0ll] - 1ll, MODS[3ll]);
            res += m1.to_num() * f1;
            res %= MODS[3ll];

            ll f2 = fpw(MODS[0ll] * MODS[2ll], MODS[1ll] - 1ll, MODS[3ll]);
            res += m2.to_num() * f2;
            res %= MODS[3ll];

            ll f3 = fpw(MODS[0ll] * MODS[1ll], MODS[2ll] - 1ll, MODS[3ll]);
            res += m3.to_num() * f3;
            res %= MODS[3ll];

            return res;
        }
};

typedef vector<CRTInt> vci;

vvll adj;
vll a_ll;
ll n, m;

vci num_tot;
vb num_tot_vis;
CRTInt count_tot(ll i, const vvll& adj) {
    CRTInt& ans = num_tot[i];

    if(!num_tot_vis[i]) {
        ll nc = adj[i].size();
        ans = CRTInt(max(nc, 1ll));
        if(nc > 0ll) {
            for(ll j : adj[i]) {
                ans = ans * count_tot(j, adj);
            }
        }

        num_tot_vis[i] = true;
    }

    return ans;
}

vci root_path_prod;

void compute_root_path_prod(ll i, CRTInt cur_prod, const vvll& adj) {
    root_path_prod[i] = cur_prod;

    for(ll j : adj[i]) {
        compute_root_path_prod(j, cur_prod * num_tot[j], adj);
    }
}

vll contribs;

void compute_contribs(ll i, CRTInt cur_contrib, const vvll& adj) {
    // Get product of everything
    CRTInt prod_all(1ll);

    contribs[i] = cur_contrib.to_num();

    for(ll j : adj[i]) {
        prod_all = prod_all * num_tot[j];
    }

    for(ll j : adj[i]) {
        compute_contribs(j, (prod_all * cur_contrib) / num_tot[j], adj);
    }
}

class Tree {
    public:
        Tree *lt, *rt;
        ll l, r;
        ll v;
        ll nv;
        bool marked;

        Tree(ll a_l, ll a_r): lt(nullptr), rt(nullptr), l(a_l), r(a_r), v(0ll), nv(0ll), marked(false) {};

        void push() {
            if(!marked) return;
            marked = false;

            if(lt == nullptr) return;

            lt->half_push();
            rt->half_push();
        }

        void half_push() {
            marked = !marked;

            swap(v, nv);
        }

        void pull() {
            v = (lt->v + rt->v) % MODS[3ll];
            nv = (lt->nv + rt->nv) % MODS[3ll];
        }

        void build(const vll& a, const vll& contribs) {
            if(l == r) {
                (a[l] == 1ll ? v : nv) = contribs[l + n];
                return;
            }

            ll m = (l + r) >> 1ll;

            lt = new Tree(l, m);
            rt = new Tree(m + 1ll, r);

            lt->build(a, contribs);
            rt->build(a, contribs);

            pull();
        }

        ll qry(ll ql, ll qr) {
            if(ql > r || qr < l) return 0ll;

            push();

            if(ql == l && qr == r) return v;

            ll m = (l + r) >> 1ll;

            return (lt->qry(ql, min(m, qr)) + rt->qry(max(m + 1ll, ql), qr)) % MODS[3ll];
        }

        void upd(ll ql, ll qr) {
            if(ql > r || qr < l) return;

            push();

            if(ql == l && qr == r) {
                half_push();
                return;
            }

            ll m = (l + r) >> 1ll;

            lt->upd(ql, min(m, qr));
            rt->upd(max(m + 1ll, ql), qr);

            pull();
        }
};

Tree* tr;

void init(int N, int M, vi P, vi A) {
    n = N; m = M;

    L(i, 0ll, N + M) {
        vll adjr;
        adj.pb(adjr);
    }

    L(i, 1ll, N + M) {
        adj[P[i]].pb(i);
    }


    num_tot.reserve(N + M);
    num_tot_vis.reserve(N + M);
    root_path_prod.reserve(N + M);
    contribs.reserve(N + M);

    L(i, 0ll, N + M) {
        num_tot.pb({1ll});
        contribs.pb(1ll);
        num_tot_vis.pb(false);
        root_path_prod.pb({1ll});
    }

    a_ll.reserve(M);

    for(int v : A) a_ll.pb((ll)v);

    count_tot(0ll, adj);

    compute_root_path_prod(0ll, CRTInt(1ll), adj);

    CRTInt prod_of_ways(1ll);


    // Compute the contributions to the sum! :))
    compute_contribs(0ll, CRTInt(1ll), adj);

    // Build the segtree
    tr = new Tree(0, m - 1ll);
    tr->build(a_ll, contribs);
}

int count_ways(int L, int R) {
    L -= n;
    R -= n;

    assert(0 <= L && L < m);
    assert(0 <= R && R < m);

    tr->upd(L, R);

    return tr->qry(0ll, m - 1ll);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 672 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 2 ms 856 KB Output is correct
8 Correct 2 ms 856 KB Output is correct
9 Correct 2 ms 856 KB Output is correct
10 Correct 2 ms 1624 KB Output is correct
11 Correct 2 ms 1624 KB Output is correct
12 Correct 2 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 672 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 856 KB Output is correct
16 Correct 2 ms 856 KB Output is correct
17 Correct 2 ms 856 KB Output is correct
18 Correct 2 ms 1624 KB Output is correct
19 Correct 2 ms 1624 KB Output is correct
20 Correct 2 ms 856 KB Output is correct
21 Correct 2 ms 856 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 2 ms 856 KB Output is correct
25 Correct 2 ms 856 KB Output is correct
26 Correct 2 ms 856 KB Output is correct
27 Correct 2 ms 856 KB Output is correct
28 Correct 2 ms 856 KB Output is correct
29 Correct 1 ms 648 KB Output is correct
30 Correct 1 ms 600 KB Output is correct
31 Correct 2 ms 1368 KB Output is correct
32 Correct 2 ms 856 KB Output is correct
33 Correct 1 ms 856 KB Output is correct
34 Correct 2 ms 600 KB Output is correct
35 Correct 1 ms 600 KB Output is correct
36 Correct 2 ms 1624 KB Output is correct
37 Correct 2 ms 1624 KB Output is correct
38 Correct 2 ms 1624 KB Output is correct
39 Correct 1 ms 856 KB Output is correct
40 Correct 2 ms 600 KB Output is correct
41 Correct 1 ms 704 KB Output is correct
42 Correct 1 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 495 ms 17860 KB Output is correct
2 Correct 758 ms 35260 KB Output is correct
3 Correct 804 ms 35228 KB Output is correct
4 Correct 776 ms 35264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 495 ms 17860 KB Output is correct
2 Correct 758 ms 35260 KB Output is correct
3 Correct 804 ms 35228 KB Output is correct
4 Correct 776 ms 35264 KB Output is correct
5 Correct 638 ms 17644 KB Output is correct
6 Correct 795 ms 35340 KB Output is correct
7 Correct 793 ms 35184 KB Output is correct
8 Correct 757 ms 35328 KB Output is correct
9 Correct 313 ms 1520 KB Output is correct
10 Correct 661 ms 2616 KB Output is correct
11 Correct 627 ms 2616 KB Output is correct
12 Correct 637 ms 2604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 2 ms 856 KB Output is correct
8 Correct 2 ms 856 KB Output is correct
9 Correct 2 ms 856 KB Output is correct
10 Correct 2 ms 1624 KB Output is correct
11 Correct 2 ms 1624 KB Output is correct
12 Correct 2 ms 856 KB Output is correct
13 Correct 495 ms 17860 KB Output is correct
14 Correct 758 ms 35260 KB Output is correct
15 Correct 804 ms 35228 KB Output is correct
16 Correct 776 ms 35264 KB Output is correct
17 Correct 638 ms 17644 KB Output is correct
18 Correct 795 ms 35340 KB Output is correct
19 Correct 793 ms 35184 KB Output is correct
20 Correct 757 ms 35328 KB Output is correct
21 Correct 313 ms 1520 KB Output is correct
22 Correct 661 ms 2616 KB Output is correct
23 Correct 627 ms 2616 KB Output is correct
24 Correct 637 ms 2604 KB Output is correct
25 Correct 856 ms 52896 KB Output is correct
26 Correct 878 ms 54424 KB Output is correct
27 Correct 935 ms 53688 KB Output is correct
28 Correct 740 ms 53716 KB Output is correct
29 Correct 830 ms 129208 KB Output is correct
30 Correct 815 ms 129460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 672 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 856 KB Output is correct
16 Correct 2 ms 856 KB Output is correct
17 Correct 2 ms 856 KB Output is correct
18 Correct 2 ms 1624 KB Output is correct
19 Correct 2 ms 1624 KB Output is correct
20 Correct 2 ms 856 KB Output is correct
21 Correct 2 ms 856 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 2 ms 856 KB Output is correct
25 Correct 2 ms 856 KB Output is correct
26 Correct 2 ms 856 KB Output is correct
27 Correct 2 ms 856 KB Output is correct
28 Correct 2 ms 856 KB Output is correct
29 Correct 1 ms 648 KB Output is correct
30 Correct 1 ms 600 KB Output is correct
31 Correct 2 ms 1368 KB Output is correct
32 Correct 2 ms 856 KB Output is correct
33 Correct 1 ms 856 KB Output is correct
34 Correct 2 ms 600 KB Output is correct
35 Correct 1 ms 600 KB Output is correct
36 Correct 2 ms 1624 KB Output is correct
37 Correct 2 ms 1624 KB Output is correct
38 Correct 2 ms 1624 KB Output is correct
39 Correct 1 ms 856 KB Output is correct
40 Correct 2 ms 600 KB Output is correct
41 Correct 1 ms 704 KB Output is correct
42 Correct 1 ms 600 KB Output is correct
43 Correct 489 ms 1952 KB Output is correct
44 Correct 667 ms 2120 KB Output is correct
45 Correct 703 ms 2096 KB Output is correct
46 Correct 678 ms 3132 KB Output is correct
47 Correct 686 ms 3120 KB Output is correct
48 Correct 664 ms 3124 KB Output is correct
49 Correct 687 ms 3124 KB Output is correct
50 Correct 701 ms 3136 KB Output is correct
51 Correct 617 ms 2096 KB Output is correct
52 Correct 626 ms 2092 KB Output is correct
53 Correct 583 ms 5232 KB Output is correct
54 Correct 674 ms 3100 KB Output is correct
55 Correct 620 ms 2604 KB Output is correct
56 Correct 630 ms 2364 KB Output is correct
57 Correct 700 ms 2052 KB Output is correct
58 Correct 711 ms 6840 KB Output is correct
59 Correct 675 ms 6888 KB Output is correct
60 Correct 662 ms 6888 KB Output is correct
61 Correct 639 ms 2876 KB Output is correct
62 Correct 680 ms 2024 KB Output is correct
63 Correct 629 ms 1988 KB Output is correct
64 Correct 649 ms 2100 KB Output is correct
65 Correct 337 ms 1620 KB Output is correct
66 Correct 607 ms 2608 KB Output is correct
67 Correct 646 ms 2832 KB Output is correct
68 Correct 620 ms 2604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 672 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 856 KB Output is correct
16 Correct 2 ms 856 KB Output is correct
17 Correct 2 ms 856 KB Output is correct
18 Correct 2 ms 1624 KB Output is correct
19 Correct 2 ms 1624 KB Output is correct
20 Correct 2 ms 856 KB Output is correct
21 Correct 2 ms 856 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 2 ms 856 KB Output is correct
25 Correct 2 ms 856 KB Output is correct
26 Correct 2 ms 856 KB Output is correct
27 Correct 2 ms 856 KB Output is correct
28 Correct 2 ms 856 KB Output is correct
29 Correct 1 ms 648 KB Output is correct
30 Correct 1 ms 600 KB Output is correct
31 Correct 2 ms 1368 KB Output is correct
32 Correct 2 ms 856 KB Output is correct
33 Correct 1 ms 856 KB Output is correct
34 Correct 2 ms 600 KB Output is correct
35 Correct 1 ms 600 KB Output is correct
36 Correct 2 ms 1624 KB Output is correct
37 Correct 2 ms 1624 KB Output is correct
38 Correct 2 ms 1624 KB Output is correct
39 Correct 1 ms 856 KB Output is correct
40 Correct 2 ms 600 KB Output is correct
41 Correct 1 ms 704 KB Output is correct
42 Correct 1 ms 600 KB Output is correct
43 Correct 495 ms 17860 KB Output is correct
44 Correct 758 ms 35260 KB Output is correct
45 Correct 804 ms 35228 KB Output is correct
46 Correct 776 ms 35264 KB Output is correct
47 Correct 638 ms 17644 KB Output is correct
48 Correct 795 ms 35340 KB Output is correct
49 Correct 793 ms 35184 KB Output is correct
50 Correct 757 ms 35328 KB Output is correct
51 Correct 313 ms 1520 KB Output is correct
52 Correct 661 ms 2616 KB Output is correct
53 Correct 627 ms 2616 KB Output is correct
54 Correct 637 ms 2604 KB Output is correct
55 Correct 856 ms 52896 KB Output is correct
56 Correct 878 ms 54424 KB Output is correct
57 Correct 935 ms 53688 KB Output is correct
58 Correct 740 ms 53716 KB Output is correct
59 Correct 830 ms 129208 KB Output is correct
60 Correct 815 ms 129460 KB Output is correct
61 Correct 489 ms 1952 KB Output is correct
62 Correct 667 ms 2120 KB Output is correct
63 Correct 703 ms 2096 KB Output is correct
64 Correct 678 ms 3132 KB Output is correct
65 Correct 686 ms 3120 KB Output is correct
66 Correct 664 ms 3124 KB Output is correct
67 Correct 687 ms 3124 KB Output is correct
68 Correct 701 ms 3136 KB Output is correct
69 Correct 617 ms 2096 KB Output is correct
70 Correct 626 ms 2092 KB Output is correct
71 Correct 583 ms 5232 KB Output is correct
72 Correct 674 ms 3100 KB Output is correct
73 Correct 620 ms 2604 KB Output is correct
74 Correct 630 ms 2364 KB Output is correct
75 Correct 700 ms 2052 KB Output is correct
76 Correct 711 ms 6840 KB Output is correct
77 Correct 675 ms 6888 KB Output is correct
78 Correct 662 ms 6888 KB Output is correct
79 Correct 639 ms 2876 KB Output is correct
80 Correct 680 ms 2024 KB Output is correct
81 Correct 629 ms 1988 KB Output is correct
82 Correct 649 ms 2100 KB Output is correct
83 Correct 337 ms 1620 KB Output is correct
84 Correct 607 ms 2608 KB Output is correct
85 Correct 646 ms 2832 KB Output is correct
86 Correct 620 ms 2604 KB Output is correct
87 Correct 0 ms 344 KB Output is correct
88 Correct 536 ms 47884 KB Output is correct
89 Correct 882 ms 32584 KB Output is correct
90 Correct 812 ms 33168 KB Output is correct
91 Correct 884 ms 55480 KB Output is correct
92 Correct 893 ms 54188 KB Output is correct
93 Correct 950 ms 54708 KB Output is correct
94 Correct 943 ms 54460 KB Output is correct
95 Correct 868 ms 54440 KB Output is correct
96 Correct 758 ms 33072 KB Output is correct
97 Correct 786 ms 33216 KB Output is correct
98 Correct 733 ms 96704 KB Output is correct
99 Correct 927 ms 53684 KB Output is correct
100 Correct 919 ms 45804 KB Output is correct
101 Correct 835 ms 39104 KB Output is correct
102 Correct 763 ms 33472 KB Output is correct
103 Correct 929 ms 128696 KB Output is correct
104 Correct 980 ms 130820 KB Output is correct
105 Correct 874 ms 130228 KB Output is correct
106 Correct 868 ms 46428 KB Output is correct
107 Correct 798 ms 30656 KB Output is correct
108 Correct 842 ms 32740 KB Output is correct
109 Correct 833 ms 34240 KB Output is correct