Submission #627557

# Submission time Handle Problem Language Result Execution time Memory
627557 2022-08-12T16:52:04 Z Tigerpants Digital Circuit (IOI22_circuit) C++17
100 / 100
1136 ms 39448 KB
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <numeric>

using namespace std;

typedef long long int ll;
typedef vector<int> stdvi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pi;
typedef vector<pi> vpi;
typedef vector<bool> vb;

const ll big_mod = 1000002022;

ll N;
ll M;
vi A;
vi P;

vvi Q;

vpi segtree;
vpi left_right;
vb lazy;

pi combine(pi a, pi b) {
    return make_pair( (a.first + b.first) % big_mod, (a.second + b.second) % big_mod);
}

pi flip(pi a) {
    return make_pair( (big_mod + a.second - a.first) % big_mod, a.second);
}

void lazy_propagation(int x) {
    if (lazy[x]) {
        segtree[x] = flip(segtree[x]);
        lazy[x] = false;
        if (left_right[x].first != left_right[x].second) {
            lazy[2 * x] = !lazy[2 * x];
            lazy[2 * x + 1] = !lazy[2 * x + 1];
        }
    }
}

void update(int x, int l, int r) { // flip all in the interval [l, r]
    lazy_propagation(x);
    if ((left_right[x].first > r) || (l > left_right[x].second)) {
        return;
    }
    if ((l <= left_right[x].first) && (left_right[x].second <= r)) {
        lazy[x] = true;
        lazy_propagation(x);
        return;
    }
    update(2 * x, l, r);
    update(2 * x + 1, l, r);
    segtree[x] = combine(segtree[2 * x], segtree[2 * x + 1]);
}

pi query(int x, int l, int r) {
    lazy_propagation(x);
    if ((left_right[x].first > r) || (l > left_right[x].second)) {
        return make_pair((ll)0, (ll)0);
    }
    if ((l <= left_right[x].first) && (left_right[x].second <= r)) {
        return segtree[x];
    }
    return combine(query(2 * x, l, r), query(2 * x + 1, l, r));
}

vi B;

void build(int x, int l, int r) {
    left_right[x] = make_pair(l, r);
    lazy[x] = false;
    if (l == r) {
        segtree[x] = make_pair(A[l] * B[l], B[l]);
        return;
    }
    int m = (l + r) /  2;
    build(2 * x, l, m);
    build(2 * x + 1, m + 1, r);
    segtree[x] = combine(segtree[2 * x], segtree[2 * x + 1]);
}

vi dp;

ll calc_dp(int cur) {

    if (cur >= N) {
        return (ll)1;
    }
    
    ll n = (ll)Q[cur].size();

    if (n == 1) {
        dp[Q[cur][0]] = 1;
        return calc_dp(Q[cur][0]);
    }

    vi children(n);
    for (int i = 0; i < n; i++) {
        children[i] = calc_dp(Q[cur][i]);
    }

    vi left(n);
    vi right(n);

    left[0] = children[0];
    right[n - 1] = children[n - 1];

    for (int i = 1; i < n; i++) {
        left[i] = (left[i - 1] * children[i]) % big_mod;
        right[n - 1 - i] = (right[n - i] * children[n - 1 - i]) % big_mod;
    }

    dp[Q[cur][0]] = right[1];
    for (int i = 1; i < n - 1; i++) {
        dp[Q[cur][i]] = (left[i - 1] * right[i + 1]) % big_mod;
    }
    dp[Q[cur][n - 1]] = left[n - 2];

    return (right[0] * n) % big_mod;
}

void calc_delta(int cur, ll product_above) {
    product_above *= dp[cur];
    product_above %= big_mod;

    if (cur >= N) {
        B[cur - N] = product_above;
        return;
    }

    for (vi::iterator child = Q[cur].begin(); child != Q[cur].end(); child++) {
        calc_delta(*child, product_above);
    }
}

void init(int N_, int M_, stdvi P_, stdvi A_) {
    N = N_;
    M = M_;

    //cout << "one" << endl;

    A.resize(M);
    for (int i = 0; i < M; i++) {
        A[i] = (ll)A_[i];
    }

    //cout << "two" << endl;
    P.resize(N + M);
    for (int i = 0; i < N + M; i++) {
        P[i] = (ll)P_[i];
    }

    //cout << "three" << endl;
    Q.resize(N);
    for (int i = 1; i < N + M; i++) {
        Q[P[i]].push_back(i);
    }

    //cout << "four" << endl;
    B.resize(M);
    // now we install B[i] with the delta which node N + i inflicts upon the root...

    //cout << "five" << endl;
    dp.resize(N + M);
    calc_dp(0);
    dp[0] = 1;

    /*
    for (auto t : dp) {
        cout << t << " ";
    }
    cout << endl;
    */

    calc_delta(0, 1);

    /*
    for (auto t : B) {
        cout << t << " ";
    }
    cout << endl;
    */

    segtree.resize(4 * M);
    left_right.resize(4 * M);
    lazy.resize(4 * M);

    build(1, 0, M - 1);

    /*
    for (int i = 0; i < segtree.size(); i++) {
        cout << "(" << segtree[i].first << ", " << segtree[i].second << " : " << left_right[i].first << ", " << left_right[i].second << " " << lazy[i] << ") ";
    }
    cout << endl;
    for (auto t : segtree) {
        cout << "(" << t.first << ", " << t.second << ") ";
    }
    cout << endl;
    */
}

int count_ways(int L, int R) {
    update(1, L - N, R - N);

    /*
    for (int i = 0; i < segtree.size(); i++) {
        cout << "(" << segtree[i].first << ", " << segtree[i].second << " : " << left_right[i].first << ", " << left_right[i].second << " " << lazy[i] << ") ";
    }
    cout << endl;
    for (auto t : segtree) {
        cout << "(" << t.first << ", " << t.second << ") ";
    }
    cout << endl;
    */

    return (int)query(1, 0, M - 1).first;
}

/*
int main() {

    int N_ = 3;
    int M_ = 4;
    stdvi P_ = {-1, 0, 1, 2, 1, 1, 0};
    stdvi A_ = {1, 0, 1, 0};

    int N_ = 1;
    int M_ = 100;
    stdvi P_ = {-1, 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};
    stdvi A_ = {1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0};
    init(N_, M_, P_, A_);
    //cout << count_ways(3, 4) << endl;
    //cout << count_ways(4, 5) << endl;
    cout << count_ways(0, 76) << endl;
    cout << count_ways(2, 54) << endl;
    cout << count_ways(1, 101) << endl;
    cout << count_ways(49, 80) << endl;
    cout << count_ways(79, 79) << endl;
}
*/
# 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 464 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 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 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 464 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 592 KB Output is correct
11 Correct 1 ms 592 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 464 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 0 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 464 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 592 KB Output is correct
19 Correct 1 ms 592 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 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 336 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 464 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 592 KB Output is correct
37 Correct 1 ms 612 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 464 KB Output is correct
41 Correct 1 ms 464 KB Output is correct
42 Correct 1 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 615 ms 8528 KB Output is correct
2 Correct 836 ms 16712 KB Output is correct
3 Correct 919 ms 16680 KB Output is correct
4 Correct 862 ms 16712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 615 ms 8528 KB Output is correct
2 Correct 836 ms 16712 KB Output is correct
3 Correct 919 ms 16680 KB Output is correct
4 Correct 862 ms 16712 KB Output is correct
5 Correct 789 ms 8552 KB Output is correct
6 Correct 954 ms 16692 KB Output is correct
7 Correct 1074 ms 16672 KB Output is correct
8 Correct 980 ms 16716 KB Output is correct
9 Correct 447 ms 808 KB Output is correct
10 Correct 896 ms 1332 KB Output is correct
11 Correct 878 ms 1324 KB Output is correct
12 Correct 793 ms 1324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 464 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 592 KB Output is correct
11 Correct 1 ms 592 KB Output is correct
12 Correct 1 ms 464 KB Output is correct
13 Correct 615 ms 8528 KB Output is correct
14 Correct 836 ms 16712 KB Output is correct
15 Correct 919 ms 16680 KB Output is correct
16 Correct 862 ms 16712 KB Output is correct
17 Correct 789 ms 8552 KB Output is correct
18 Correct 954 ms 16692 KB Output is correct
19 Correct 1074 ms 16672 KB Output is correct
20 Correct 980 ms 16716 KB Output is correct
21 Correct 447 ms 808 KB Output is correct
22 Correct 896 ms 1332 KB Output is correct
23 Correct 878 ms 1324 KB Output is correct
24 Correct 793 ms 1324 KB Output is correct
25 Correct 1048 ms 24888 KB Output is correct
26 Correct 979 ms 25288 KB Output is correct
27 Correct 1136 ms 25288 KB Output is correct
28 Correct 776 ms 25288 KB Output is correct
29 Correct 1027 ms 39352 KB Output is correct
30 Correct 1067 ms 39404 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 464 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 0 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 464 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 592 KB Output is correct
19 Correct 1 ms 592 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 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 336 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 464 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 592 KB Output is correct
37 Correct 1 ms 612 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 464 KB Output is correct
41 Correct 1 ms 464 KB Output is correct
42 Correct 1 ms 464 KB Output is correct
43 Correct 654 ms 1016 KB Output is correct
44 Correct 818 ms 1064 KB Output is correct
45 Correct 815 ms 1068 KB Output is correct
46 Correct 783 ms 1588 KB Output is correct
47 Correct 866 ms 1580 KB Output is correct
48 Correct 877 ms 1580 KB Output is correct
49 Correct 781 ms 1584 KB Output is correct
50 Correct 741 ms 1588 KB Output is correct
51 Correct 912 ms 1360 KB Output is correct
52 Correct 901 ms 1336 KB Output is correct
53 Correct 532 ms 848 KB Output is correct
54 Correct 737 ms 1552 KB Output is correct
55 Correct 895 ms 1408 KB Output is correct
56 Correct 898 ms 1360 KB Output is correct
57 Correct 919 ms 1232 KB Output is correct
58 Correct 903 ms 2256 KB Output is correct
59 Correct 700 ms 1968 KB Output is correct
60 Correct 853 ms 1960 KB Output is correct
61 Correct 910 ms 1104 KB Output is correct
62 Correct 886 ms 1080 KB Output is correct
63 Correct 915 ms 1108 KB Output is correct
64 Correct 840 ms 1280 KB Output is correct
65 Correct 428 ms 720 KB Output is correct
66 Correct 918 ms 1332 KB Output is correct
67 Correct 924 ms 1332 KB Output is correct
68 Correct 780 ms 1328 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 464 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 0 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 464 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 592 KB Output is correct
19 Correct 1 ms 592 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 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 336 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 464 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 592 KB Output is correct
37 Correct 1 ms 612 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 464 KB Output is correct
41 Correct 1 ms 464 KB Output is correct
42 Correct 1 ms 464 KB Output is correct
43 Correct 615 ms 8528 KB Output is correct
44 Correct 836 ms 16712 KB Output is correct
45 Correct 919 ms 16680 KB Output is correct
46 Correct 862 ms 16712 KB Output is correct
47 Correct 789 ms 8552 KB Output is correct
48 Correct 954 ms 16692 KB Output is correct
49 Correct 1074 ms 16672 KB Output is correct
50 Correct 980 ms 16716 KB Output is correct
51 Correct 447 ms 808 KB Output is correct
52 Correct 896 ms 1332 KB Output is correct
53 Correct 878 ms 1324 KB Output is correct
54 Correct 793 ms 1324 KB Output is correct
55 Correct 1048 ms 24888 KB Output is correct
56 Correct 979 ms 25288 KB Output is correct
57 Correct 1136 ms 25288 KB Output is correct
58 Correct 776 ms 25288 KB Output is correct
59 Correct 1027 ms 39352 KB Output is correct
60 Correct 1067 ms 39404 KB Output is correct
61 Correct 654 ms 1016 KB Output is correct
62 Correct 818 ms 1064 KB Output is correct
63 Correct 815 ms 1068 KB Output is correct
64 Correct 783 ms 1588 KB Output is correct
65 Correct 866 ms 1580 KB Output is correct
66 Correct 877 ms 1580 KB Output is correct
67 Correct 781 ms 1584 KB Output is correct
68 Correct 741 ms 1588 KB Output is correct
69 Correct 912 ms 1360 KB Output is correct
70 Correct 901 ms 1336 KB Output is correct
71 Correct 532 ms 848 KB Output is correct
72 Correct 737 ms 1552 KB Output is correct
73 Correct 895 ms 1408 KB Output is correct
74 Correct 898 ms 1360 KB Output is correct
75 Correct 919 ms 1232 KB Output is correct
76 Correct 903 ms 2256 KB Output is correct
77 Correct 700 ms 1968 KB Output is correct
78 Correct 853 ms 1960 KB Output is correct
79 Correct 910 ms 1104 KB Output is correct
80 Correct 886 ms 1080 KB Output is correct
81 Correct 915 ms 1108 KB Output is correct
82 Correct 840 ms 1280 KB Output is correct
83 Correct 428 ms 720 KB Output is correct
84 Correct 918 ms 1332 KB Output is correct
85 Correct 924 ms 1332 KB Output is correct
86 Correct 780 ms 1328 KB Output is correct
87 Correct 0 ms 208 KB Output is correct
88 Correct 566 ms 22288 KB Output is correct
89 Correct 938 ms 14808 KB Output is correct
90 Correct 1002 ms 15688 KB Output is correct
91 Correct 1000 ms 25908 KB Output is correct
92 Correct 982 ms 25928 KB Output is correct
93 Correct 1032 ms 25928 KB Output is correct
94 Correct 1022 ms 25928 KB Output is correct
95 Correct 1065 ms 26080 KB Output is correct
96 Correct 801 ms 18444 KB Output is correct
97 Correct 882 ms 18400 KB Output is correct
98 Correct 861 ms 12752 KB Output is correct
99 Correct 1008 ms 25392 KB Output is correct
100 Correct 949 ms 22428 KB Output is correct
101 Correct 1071 ms 20812 KB Output is correct
102 Correct 941 ms 18504 KB Output is correct
103 Correct 939 ms 39448 KB Output is correct
104 Correct 912 ms 30848 KB Output is correct
105 Correct 835 ms 30868 KB Output is correct
106 Correct 931 ms 15372 KB Output is correct
107 Correct 963 ms 15176 KB Output is correct
108 Correct 1012 ms 16840 KB Output is correct
109 Correct 982 ms 18720 KB Output is correct