Submission #797580

# Submission time Handle Problem Language Result Execution time Memory
797580 2023-07-29T16:53:41 Z HaroldVemeno Digital Circuit (IOI22_circuit) C++17
100 / 100
910 ms 28968 KB
#include "circuit.h"

#include <bits/stdc++.h>

#ifdef GUDEB
    #define D(x) cerr << #x << ": " << (x) << '\n';
#else
    #define D(x) ;
#endif

using namespace std;
using ll = long long;

int n, m;
vector<int> ps;
vector<int> as;
vector<vector<int>> al;
vector<int> tss;
vector<int> tcs;
vector<int> td1;
vector<int> td2;
vector<int> ts1;
vector<int> ts2;
vector<int> mults;
int ms = 1;
int mis[400000];
int miv[400000];
bool ml[400000];

constexpr int m2 = 2242157;
constexpr int mod = 2*223*m2;
constexpr int ord = 222*2242156;

int bexp(ll b, ll e) {
    if(e < 0) e += ord;
    ll r = 1;
    while(e > 0) {
        if(e&1) {
            r *= b;
            r %= mod;
        }
        e /= 2;
        b *= b;
        b %= mod;
    }
    return r;
}

void iwalk(int v) {
    tcs[v] = 1;
    tss[v] = 1;
    int c = al[v].size()-1;
    if(ps[v] == -1) c += 1;
    if(c == 0) return;
    while(c % 2 == 0) {
        c /= 2;
        ts1[v] += 1;
    }
    while(c % 223 == 0) {
        c /= 223;
        ts2[v] += 1;
    }
    tss[v] = c;
    D(v)
    D(tss[v])
    for(auto a : al[v]) {
        if(a == ps[v]) continue;
        iwalk(a);
        tcs[v] = (1ll*tcs[v]*tss[a]) % mod;
        td1[v] += ts1[a];
        td2[v] += ts2[a];
    }
    tss[v] = (1ll*tcs[v]*tss[v]) % mod;
    ts1[v] += td1[v];
    ts2[v] += td2[v];
}

void mwalk(int v, int ml, int d1, int d2) {
    if(v >= n) {
        mults[v-n] = ml;
        mults[v-n] = (1ll*bexp(2, d1)*mults[v-n]) % mod;
        mults[v-n] = (1ll*bexp(223, d2)*mults[v-n]) % mod;
    }
    for(auto a : al[v]) {
       if(a == ps[v]) continue;
       ll nm = 1ll*ml*tcs[v];
       nm %= mod;
       nm *= bexp(tss[a], -1);
       nm %= mod;
       mwalk(a, nm, d1+td1[v]-ts1[a], d2+td2[v]-ts2[a]);
    }
}

void init(int N, int M, std::vector<int> P, std::vector<int> A) {
    n = N;
    m = M;
    ps = std::move(P);
    as = std::move(A);
    al.resize(N+M);
    mults.resize(M);
    tcs.resize(N+M);
    td1.resize(N+M);
    td2.resize(N+M);
    tss.resize(N+M);
    ts1.resize(N+M);
    ts2.resize(N+M);
    for(int i = 1; i < n+m; ++i) {
        al[i].push_back(ps[i]);
        al[ps[i]].push_back(i);
    }

    iwalk(0);
    mwalk(0, 1, 0, 0);
    if(0) {
        for(int i = 0; i < n+m; ++i) {
            cerr << tcs[i] << ' ';
        }
        cerr << '\n';
        for(int i = 0; i < m; ++i) {
            cerr << mults[i] << ' ';
        }
        cerr << '\n';
    }
    ms = 1;
    while(ms < m) ms *= 2;
    for(int i = 0; i < m; ++i) {
        mis[ms+i] = mults[i];
    }
    for(int i = ms-1; i > 0; --i) {
        mis[i] = (mis[2*i] + mis[2*i+1]) % mod;
    }
    for(int i = 0; i < m; ++i) {
        miv[ms+i] = mis[ms+i]*as[i];
    }
    for(int i = ms-1; i > 0; --i) {
        miv[i] = (miv[2*i] + miv[2*i+1]) % mod;
    }
}

void flip(int l, int r, int ul, int ur, int u) {
    if(ml[u]) {
        miv[u] = mis[u] - miv[u];
        miv[u] %= mod;
        if(u < ms) {
            ml[2*u] ^= 1;
            ml[2*u+1] ^= 1;
        }
        ml[u] = false;
    }
    if(l >= r) return;
    if(r <= ul) return;
    if(ur <= l) return;
    if(l <= ul && ur <= r) {
        miv[u] = mis[u] - miv[u];
        miv[u] %= mod;
        if(u < ms) {
            ml[2*u] ^= 1;
            ml[2*u+1] ^= 1;
        }
        return;
    }

    int um = (ul+ur)/2;
    flip(l, r, ul, um, 2*u);
    flip(l, r, um, ur, 2*u+1);

    miv[u] = (miv[2*u] + miv[2*u+1]) % mod;
}

int count_ways(int L, int R) {
    L -= n;
    R -= n;
    ++R;
    flip(L, R, 0, ms, 1);
    int res = miv[1];
    while(res < 0) res += mod;
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 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
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 336 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 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 464 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 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 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 208 KB Output is correct
10 Correct 1 ms 336 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 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 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 464 KB Output is correct
21 Correct 1 ms 464 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 464 KB Output is correct
25 Correct 1 ms 464 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 336 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 464 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 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 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 594 ms 6896 KB Output is correct
2 Correct 709 ms 13436 KB Output is correct
3 Correct 775 ms 13432 KB Output is correct
4 Correct 813 ms 13436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 594 ms 6896 KB Output is correct
2 Correct 709 ms 13436 KB Output is correct
3 Correct 775 ms 13432 KB Output is correct
4 Correct 813 ms 13436 KB Output is correct
5 Correct 532 ms 6956 KB Output is correct
6 Correct 716 ms 13564 KB Output is correct
7 Correct 830 ms 13564 KB Output is correct
8 Correct 751 ms 13460 KB Output is correct
9 Correct 363 ms 720 KB Output is correct
10 Correct 749 ms 1104 KB Output is correct
11 Correct 647 ms 1104 KB Output is correct
12 Correct 825 ms 1104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 336 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 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 464 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 464 KB Output is correct
13 Correct 594 ms 6896 KB Output is correct
14 Correct 709 ms 13436 KB Output is correct
15 Correct 775 ms 13432 KB Output is correct
16 Correct 813 ms 13436 KB Output is correct
17 Correct 532 ms 6956 KB Output is correct
18 Correct 716 ms 13564 KB Output is correct
19 Correct 830 ms 13564 KB Output is correct
20 Correct 751 ms 13460 KB Output is correct
21 Correct 363 ms 720 KB Output is correct
22 Correct 749 ms 1104 KB Output is correct
23 Correct 647 ms 1104 KB Output is correct
24 Correct 825 ms 1104 KB Output is correct
25 Correct 856 ms 20392 KB Output is correct
26 Correct 837 ms 20740 KB Output is correct
27 Correct 746 ms 20740 KB Output is correct
28 Correct 758 ms 20584 KB Output is correct
29 Correct 774 ms 28568 KB Output is correct
30 Correct 771 ms 28408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 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 208 KB Output is correct
10 Correct 1 ms 336 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 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 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 464 KB Output is correct
21 Correct 1 ms 464 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 464 KB Output is correct
25 Correct 1 ms 464 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 336 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 464 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 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 336 KB Output is correct
43 Correct 471 ms 848 KB Output is correct
44 Correct 685 ms 976 KB Output is correct
45 Correct 770 ms 848 KB Output is correct
46 Correct 769 ms 1232 KB Output is correct
47 Correct 747 ms 1368 KB Output is correct
48 Correct 730 ms 1360 KB Output is correct
49 Correct 679 ms 1360 KB Output is correct
50 Correct 778 ms 1360 KB Output is correct
51 Correct 546 ms 848 KB Output is correct
52 Correct 745 ms 848 KB Output is correct
53 Correct 593 ms 1104 KB Output is correct
54 Correct 764 ms 1232 KB Output is correct
55 Correct 724 ms 1144 KB Output is correct
56 Correct 647 ms 976 KB Output is correct
57 Correct 811 ms 848 KB Output is correct
58 Correct 793 ms 1744 KB Output is correct
59 Correct 713 ms 1772 KB Output is correct
60 Correct 797 ms 1772 KB Output is correct
61 Correct 646 ms 976 KB Output is correct
62 Correct 758 ms 840 KB Output is correct
63 Correct 656 ms 848 KB Output is correct
64 Correct 612 ms 900 KB Output is correct
65 Correct 284 ms 720 KB Output is correct
66 Correct 724 ms 1104 KB Output is correct
67 Correct 719 ms 1152 KB Output is correct
68 Correct 771 ms 1028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 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 208 KB Output is correct
10 Correct 1 ms 336 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 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 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 464 KB Output is correct
21 Correct 1 ms 464 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 464 KB Output is correct
25 Correct 1 ms 464 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 336 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 464 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 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 336 KB Output is correct
43 Correct 594 ms 6896 KB Output is correct
44 Correct 709 ms 13436 KB Output is correct
45 Correct 775 ms 13432 KB Output is correct
46 Correct 813 ms 13436 KB Output is correct
47 Correct 532 ms 6956 KB Output is correct
48 Correct 716 ms 13564 KB Output is correct
49 Correct 830 ms 13564 KB Output is correct
50 Correct 751 ms 13460 KB Output is correct
51 Correct 363 ms 720 KB Output is correct
52 Correct 749 ms 1104 KB Output is correct
53 Correct 647 ms 1104 KB Output is correct
54 Correct 825 ms 1104 KB Output is correct
55 Correct 856 ms 20392 KB Output is correct
56 Correct 837 ms 20740 KB Output is correct
57 Correct 746 ms 20740 KB Output is correct
58 Correct 758 ms 20584 KB Output is correct
59 Correct 774 ms 28568 KB Output is correct
60 Correct 771 ms 28408 KB Output is correct
61 Correct 471 ms 848 KB Output is correct
62 Correct 685 ms 976 KB Output is correct
63 Correct 770 ms 848 KB Output is correct
64 Correct 769 ms 1232 KB Output is correct
65 Correct 747 ms 1368 KB Output is correct
66 Correct 730 ms 1360 KB Output is correct
67 Correct 679 ms 1360 KB Output is correct
68 Correct 778 ms 1360 KB Output is correct
69 Correct 546 ms 848 KB Output is correct
70 Correct 745 ms 848 KB Output is correct
71 Correct 593 ms 1104 KB Output is correct
72 Correct 764 ms 1232 KB Output is correct
73 Correct 724 ms 1144 KB Output is correct
74 Correct 647 ms 976 KB Output is correct
75 Correct 811 ms 848 KB Output is correct
76 Correct 793 ms 1744 KB Output is correct
77 Correct 713 ms 1772 KB Output is correct
78 Correct 797 ms 1772 KB Output is correct
79 Correct 646 ms 976 KB Output is correct
80 Correct 758 ms 840 KB Output is correct
81 Correct 656 ms 848 KB Output is correct
82 Correct 612 ms 900 KB Output is correct
83 Correct 284 ms 720 KB Output is correct
84 Correct 724 ms 1104 KB Output is correct
85 Correct 719 ms 1152 KB Output is correct
86 Correct 771 ms 1028 KB Output is correct
87 Correct 1 ms 208 KB Output is correct
88 Correct 601 ms 18444 KB Output is correct
89 Correct 779 ms 12900 KB Output is correct
90 Correct 731 ms 12816 KB Output is correct
91 Correct 704 ms 20756 KB Output is correct
92 Correct 833 ms 20972 KB Output is correct
93 Correct 910 ms 20972 KB Output is correct
94 Correct 882 ms 20964 KB Output is correct
95 Correct 793 ms 20800 KB Output is correct
96 Correct 776 ms 12356 KB Output is correct
97 Correct 671 ms 12520 KB Output is correct
98 Correct 633 ms 16636 KB Output is correct
99 Correct 881 ms 20744 KB Output is correct
100 Correct 894 ms 16436 KB Output is correct
101 Correct 747 ms 14684 KB Output is correct
102 Correct 657 ms 12588 KB Output is correct
103 Correct 850 ms 28616 KB Output is correct
104 Correct 903 ms 28968 KB Output is correct
105 Correct 849 ms 28800 KB Output is correct
106 Correct 872 ms 13720 KB Output is correct
107 Correct 805 ms 12128 KB Output is correct
108 Correct 745 ms 12572 KB Output is correct
109 Correct 797 ms 12688 KB Output is correct