Submission #1105500

# Submission time Handle Problem Language Result Execution time Memory
1105500 2024-10-26T14:42:46 Z Zero_OP Digital Circuit (IOI22_circuit) C++17
100 / 100
1008 ms 39884 KB
#include <bits/stdc++.h>

#ifndef LOCAL
    #include "circuit.h"
#endif // LOCAL

using namespace std;

#define rep(i, l, r) for(int i = (l), _r = (r); i < _r; ++i)
#define FOR(i, l, r) for(int i = (l), _r = (r); i <= _r; ++i)
#define ROF(i, r, l) for(int i = (r), _l = (l); i >= _l; --i)
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define sz(v) (int)v.size()
#define dbg(x) "[" #x " = " << (x) << "]"
#define file(name) if(fopen(name".inp", "r")) freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);

template<typename T>
bool minimize(T& a, const T& b){
    if(a > b) return a = b, true; return false;
}

template<typename T>
bool maximize(T& a, const T& b){
    if(a < b) return a = b, true; return false;
}

using ll = long long;
using ld = long double;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T random_int(T l, T r){ return uniform_int_distribution<T>(l, r)(rng); }
template<typename T> T random_real(T l, T r){ return uniform_real_distribution<T>(l, r)(rng); }

const int MAX = 2e5 + 5;
const int mod = 1e9 + 2022;

int n, m;
vector<int> adj[MAX];
bool state[MAX];

int dp[MAX][2], total_ways[MAX], eval[MAX];

int add(int a, int b){
    return a + b < mod ? a + b : a + b - mod;
}

int multiply(int a, int b){
    return 1LL * a * b % mod;
}

int subtract(int a, int b){
    return a < b ? a - b + mod : a - b;
}

bool isLeaf(int u){
    return n <= u;
}

struct node{
    int total, one;
    node(int total = 0, int one = 0) : total(total), one(one) {}
    void flip(){ one = subtract(total, one); }
    friend node operator + (const node& a, const node& b){
        return node(add(a.total, b.total), add(a.one, b.one));
    }
};

namespace SegmentTree{
    node st[MAX << 1];
    int lazy[MAX << 1];

    void build(int id, int l, int r){
        if(l == r){
            st[id] = node(eval[l], (int)state[l] * eval[l]);
        } else{
            int mid = l + r >> 1;
            build(id << 1, l, mid);
            build(id << 1 | 1, mid + 1, r);
            st[id] = st[id << 1] + st[id << 1 | 1];
        }
    }

    void apply(int id){
        st[id].flip();
        lazy[id] ^= 1;
    }

    void lazyDown(int id){
        if(lazy[id]){
            apply(id << 1);
            apply(id << 1 | 1);
            lazy[id] = 0;
        }
    }

    void update(int id, int l, int r, int u, int v){
        if(u <= l && r <= v) apply(id);
        else{
            int mid = l + r >> 1;
            lazyDown(id);
            if(u <= mid) update(id << 1, l, mid, u, v);
            if(mid < v) update(id << 1 | 1, mid + 1, r, u, v);
            st[id] = st[id << 1] + st[id << 1 | 1];
        }
    }

    int get(){
        return st[1].one;
    }
}

void predfs(int u){
    if(isLeaf(u)){
        total_ways[u] = 1;
        return;
    }

    total_ways[u] = sz(adj[u]);
    for(int v : adj[u]){
        predfs(v);
        total_ways[u] = multiply(total_ways[u], total_ways[v]);
    }
}

void dfs(int u, int mult){
    if(isLeaf(u)){
        eval[u - n] = mult;
        return;
    }

    int k = sz(adj[u]);
    vector<int> pref(k), suff(k);
    for(int i = 0; i < k; ++i){
        pref[i] = suff[i] = total_ways[adj[u][i]];
    }

    for(int i = 1; i < k; ++i){
        pref[i] = multiply(pref[i - 1], pref[i]);
    }

    for(int i = k - 2; i >= 0; --i){
        suff[i] = multiply(suff[i], suff[i + 1]);
    }

    for(int i = 0; i < k; ++i){
        int cur = multiply(i > 0 ? pref[i - 1] : 1, i + 1 < k ? suff[i + 1] : 1);
        dfs(adj[u][i], multiply(mult, cur));
    }
}

void init(int N, int M, vector<int> P, vector<int> A){
    n = N;
    m = M;
    rep(i, 1, n + m){
        adj[P[i]].push_back(i);
    }

    rep(i, 0, m){
        state[i] = A[i];
    }

    predfs(0);
    dfs(0, 1);
    SegmentTree::build(1, 0, m - 1);
}

int count_ways(int L, int R){
    L -= n; R -= n;
    SegmentTree::update(1, 0, m - 1, L, R);
    return SegmentTree::get();
}

#ifdef LOCAL
int main(){
    srand(time(nullptr));
    mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

    auto random = [&](int l, int r){
        return uniform_int_distribution<int>(l, r)(rng);
    };

    for(int itest = 1; itest <= 1; ++itest){
        //random testcase
//        int N = random(1, 6), M = random(1, 20), Q = random(1, 5);
//        cout << "N = " << N << ", M = " << M << ", Q = " << Q << '\n';
//        vector<int> P(N + M, -1);
//        rep(i, 1, N + M){
//            P[i] = random(0, min(i - 1, N - 1));
//        }
//
//        vector<int> A(M);
//        rep(i, 0, M){
//            A[i] = random(0, 1);
//        }
//        cout << "Array P : "; rep(i, 0, N + M) cout << P[i] << ' '; cout << '\n';
//        cout << "Array A : "; rep(i, 0, M) cout << A[i] << ' '; cout << '\n';
//        init(N, M, P, A);
//        rep(i, 0, Q){
//            int L = random(0, M - 1), R = random(0, M - 1);
//            if(L > R) swap(L, R);
//            cout << "query : " << L << ' ' << R << '\n';
//            cout << count_ways(L, R) << '\n';
//        }

        //example testcase
        int N = 3, M = 4;
        vector<int> P(N + M);
        P = {-1, 0, 1, 2, 1, 1, 0};
        vector<int> A(M);
        A = {1, 0, 1, 0};
        init(N, M, P, A);
        cout << count_ways(3, 4) << '\n'; //should be 2
        cout << count_ways(4, 5) << '\n'; //should be 0
        cout << count_ways(3, 6) << '\n'; //should 6


    }

    return 0;
}
#endif //LOCAL

Compilation message

circuit.cpp: In function 'bool minimize(T&, const T&)':
circuit.cpp:20:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   20 |     if(a > b) return a = b, true; return false;
      |     ^~
circuit.cpp:20:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   20 |     if(a > b) return a = b, true; return false;
      |                                   ^~~~~~
circuit.cpp: In function 'bool maximize(T&, const T&)':
circuit.cpp:25:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   25 |     if(a < b) return a = b, true; return false;
      |     ^~
circuit.cpp:25:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   25 |     if(a < b) return a = b, true; return false;
      |                                   ^~~~~~
circuit.cpp: In function 'void SegmentTree::build(int, int, int)':
circuit.cpp:77:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   77 |             int mid = l + r >> 1;
      |                       ~~^~~
circuit.cpp: In function 'void SegmentTree::update(int, int, int, int, int)':
circuit.cpp:100:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  100 |             int mid = l + r >> 1;
      |                       ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12880 KB Output is correct
2 Correct 2 ms 12880 KB Output is correct
3 Correct 2 ms 12880 KB Output is correct
4 Correct 3 ms 12880 KB Output is correct
5 Correct 2 ms 12880 KB Output is correct
6 Correct 2 ms 12880 KB Output is correct
7 Correct 3 ms 12880 KB Output is correct
8 Correct 2 ms 12880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12880 KB Output is correct
2 Correct 3 ms 12880 KB Output is correct
3 Correct 3 ms 12880 KB Output is correct
4 Correct 3 ms 13024 KB Output is correct
5 Correct 3 ms 13136 KB Output is correct
6 Correct 3 ms 13008 KB Output is correct
7 Correct 3 ms 12880 KB Output is correct
8 Correct 3 ms 12880 KB Output is correct
9 Correct 3 ms 12880 KB Output is correct
10 Correct 3 ms 13136 KB Output is correct
11 Correct 4 ms 13136 KB Output is correct
12 Correct 3 ms 12972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12880 KB Output is correct
2 Correct 2 ms 12880 KB Output is correct
3 Correct 2 ms 12880 KB Output is correct
4 Correct 3 ms 12880 KB Output is correct
5 Correct 2 ms 12880 KB Output is correct
6 Correct 2 ms 12880 KB Output is correct
7 Correct 3 ms 12880 KB Output is correct
8 Correct 2 ms 12880 KB Output is correct
9 Correct 3 ms 12880 KB Output is correct
10 Correct 3 ms 12880 KB Output is correct
11 Correct 3 ms 12880 KB Output is correct
12 Correct 3 ms 13024 KB Output is correct
13 Correct 3 ms 13136 KB Output is correct
14 Correct 3 ms 13008 KB Output is correct
15 Correct 3 ms 12880 KB Output is correct
16 Correct 3 ms 12880 KB Output is correct
17 Correct 3 ms 12880 KB Output is correct
18 Correct 3 ms 13136 KB Output is correct
19 Correct 4 ms 13136 KB Output is correct
20 Correct 3 ms 12972 KB Output is correct
21 Correct 4 ms 12880 KB Output is correct
22 Correct 3 ms 12880 KB Output is correct
23 Correct 3 ms 12880 KB Output is correct
24 Correct 3 ms 13048 KB Output is correct
25 Correct 3 ms 12880 KB Output is correct
26 Correct 3 ms 12880 KB Output is correct
27 Correct 3 ms 12880 KB Output is correct
28 Correct 3 ms 12880 KB Output is correct
29 Correct 3 ms 12880 KB Output is correct
30 Correct 3 ms 12880 KB Output is correct
31 Correct 3 ms 13136 KB Output is correct
32 Correct 3 ms 12880 KB Output is correct
33 Correct 3 ms 12880 KB Output is correct
34 Correct 3 ms 12880 KB Output is correct
35 Correct 3 ms 12880 KB Output is correct
36 Correct 4 ms 13136 KB Output is correct
37 Correct 3 ms 13136 KB Output is correct
38 Correct 3 ms 13136 KB Output is correct
39 Correct 3 ms 12880 KB Output is correct
40 Correct 3 ms 12892 KB Output is correct
41 Correct 4 ms 13048 KB Output is correct
42 Correct 3 ms 12880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 608 ms 14672 KB Output is correct
2 Correct 806 ms 16464 KB Output is correct
3 Correct 859 ms 16456 KB Output is correct
4 Correct 896 ms 16464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 608 ms 14672 KB Output is correct
2 Correct 806 ms 16464 KB Output is correct
3 Correct 859 ms 16456 KB Output is correct
4 Correct 896 ms 16464 KB Output is correct
5 Correct 769 ms 14672 KB Output is correct
6 Correct 985 ms 16464 KB Output is correct
7 Correct 885 ms 16464 KB Output is correct
8 Correct 819 ms 16464 KB Output is correct
9 Correct 423 ms 12880 KB Output is correct
10 Correct 876 ms 13136 KB Output is correct
11 Correct 856 ms 13136 KB Output is correct
12 Correct 845 ms 13136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12880 KB Output is correct
2 Correct 3 ms 12880 KB Output is correct
3 Correct 3 ms 12880 KB Output is correct
4 Correct 3 ms 13024 KB Output is correct
5 Correct 3 ms 13136 KB Output is correct
6 Correct 3 ms 13008 KB Output is correct
7 Correct 3 ms 12880 KB Output is correct
8 Correct 3 ms 12880 KB Output is correct
9 Correct 3 ms 12880 KB Output is correct
10 Correct 3 ms 13136 KB Output is correct
11 Correct 4 ms 13136 KB Output is correct
12 Correct 3 ms 12972 KB Output is correct
13 Correct 608 ms 14672 KB Output is correct
14 Correct 806 ms 16464 KB Output is correct
15 Correct 859 ms 16456 KB Output is correct
16 Correct 896 ms 16464 KB Output is correct
17 Correct 769 ms 14672 KB Output is correct
18 Correct 985 ms 16464 KB Output is correct
19 Correct 885 ms 16464 KB Output is correct
20 Correct 819 ms 16464 KB Output is correct
21 Correct 423 ms 12880 KB Output is correct
22 Correct 876 ms 13136 KB Output is correct
23 Correct 856 ms 13136 KB Output is correct
24 Correct 845 ms 13136 KB Output is correct
25 Correct 844 ms 18280 KB Output is correct
26 Correct 912 ms 18464 KB Output is correct
27 Correct 970 ms 18248 KB Output is correct
28 Correct 753 ms 18256 KB Output is correct
29 Correct 1008 ms 38728 KB Output is correct
30 Correct 935 ms 38624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12880 KB Output is correct
2 Correct 2 ms 12880 KB Output is correct
3 Correct 2 ms 12880 KB Output is correct
4 Correct 3 ms 12880 KB Output is correct
5 Correct 2 ms 12880 KB Output is correct
6 Correct 2 ms 12880 KB Output is correct
7 Correct 3 ms 12880 KB Output is correct
8 Correct 2 ms 12880 KB Output is correct
9 Correct 3 ms 12880 KB Output is correct
10 Correct 3 ms 12880 KB Output is correct
11 Correct 3 ms 12880 KB Output is correct
12 Correct 3 ms 13024 KB Output is correct
13 Correct 3 ms 13136 KB Output is correct
14 Correct 3 ms 13008 KB Output is correct
15 Correct 3 ms 12880 KB Output is correct
16 Correct 3 ms 12880 KB Output is correct
17 Correct 3 ms 12880 KB Output is correct
18 Correct 3 ms 13136 KB Output is correct
19 Correct 4 ms 13136 KB Output is correct
20 Correct 3 ms 12972 KB Output is correct
21 Correct 4 ms 12880 KB Output is correct
22 Correct 3 ms 12880 KB Output is correct
23 Correct 3 ms 12880 KB Output is correct
24 Correct 3 ms 13048 KB Output is correct
25 Correct 3 ms 12880 KB Output is correct
26 Correct 3 ms 12880 KB Output is correct
27 Correct 3 ms 12880 KB Output is correct
28 Correct 3 ms 12880 KB Output is correct
29 Correct 3 ms 12880 KB Output is correct
30 Correct 3 ms 12880 KB Output is correct
31 Correct 3 ms 13136 KB Output is correct
32 Correct 3 ms 12880 KB Output is correct
33 Correct 3 ms 12880 KB Output is correct
34 Correct 3 ms 12880 KB Output is correct
35 Correct 3 ms 12880 KB Output is correct
36 Correct 4 ms 13136 KB Output is correct
37 Correct 3 ms 13136 KB Output is correct
38 Correct 3 ms 13136 KB Output is correct
39 Correct 3 ms 12880 KB Output is correct
40 Correct 3 ms 12892 KB Output is correct
41 Correct 4 ms 13048 KB Output is correct
42 Correct 3 ms 12880 KB Output is correct
43 Correct 582 ms 13136 KB Output is correct
44 Correct 862 ms 13136 KB Output is correct
45 Correct 860 ms 13136 KB Output is correct
46 Correct 870 ms 13136 KB Output is correct
47 Correct 877 ms 13136 KB Output is correct
48 Correct 842 ms 13136 KB Output is correct
49 Correct 836 ms 13136 KB Output is correct
50 Correct 858 ms 13136 KB Output is correct
51 Correct 847 ms 13136 KB Output is correct
52 Correct 879 ms 13136 KB Output is correct
53 Correct 797 ms 14160 KB Output is correct
54 Correct 837 ms 13136 KB Output is correct
55 Correct 891 ms 13232 KB Output is correct
56 Correct 909 ms 13136 KB Output is correct
57 Correct 871 ms 13136 KB Output is correct
58 Correct 850 ms 14160 KB Output is correct
59 Correct 849 ms 14160 KB Output is correct
60 Correct 813 ms 14160 KB Output is correct
61 Correct 833 ms 13392 KB Output is correct
62 Correct 879 ms 13136 KB Output is correct
63 Correct 843 ms 13136 KB Output is correct
64 Correct 843 ms 13136 KB Output is correct
65 Correct 452 ms 12880 KB Output is correct
66 Correct 893 ms 13136 KB Output is correct
67 Correct 851 ms 13136 KB Output is correct
68 Correct 885 ms 13136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12880 KB Output is correct
2 Correct 2 ms 12880 KB Output is correct
3 Correct 2 ms 12880 KB Output is correct
4 Correct 3 ms 12880 KB Output is correct
5 Correct 2 ms 12880 KB Output is correct
6 Correct 2 ms 12880 KB Output is correct
7 Correct 3 ms 12880 KB Output is correct
8 Correct 2 ms 12880 KB Output is correct
9 Correct 3 ms 12880 KB Output is correct
10 Correct 3 ms 12880 KB Output is correct
11 Correct 3 ms 12880 KB Output is correct
12 Correct 3 ms 13024 KB Output is correct
13 Correct 3 ms 13136 KB Output is correct
14 Correct 3 ms 13008 KB Output is correct
15 Correct 3 ms 12880 KB Output is correct
16 Correct 3 ms 12880 KB Output is correct
17 Correct 3 ms 12880 KB Output is correct
18 Correct 3 ms 13136 KB Output is correct
19 Correct 4 ms 13136 KB Output is correct
20 Correct 3 ms 12972 KB Output is correct
21 Correct 4 ms 12880 KB Output is correct
22 Correct 3 ms 12880 KB Output is correct
23 Correct 3 ms 12880 KB Output is correct
24 Correct 3 ms 13048 KB Output is correct
25 Correct 3 ms 12880 KB Output is correct
26 Correct 3 ms 12880 KB Output is correct
27 Correct 3 ms 12880 KB Output is correct
28 Correct 3 ms 12880 KB Output is correct
29 Correct 3 ms 12880 KB Output is correct
30 Correct 3 ms 12880 KB Output is correct
31 Correct 3 ms 13136 KB Output is correct
32 Correct 3 ms 12880 KB Output is correct
33 Correct 3 ms 12880 KB Output is correct
34 Correct 3 ms 12880 KB Output is correct
35 Correct 3 ms 12880 KB Output is correct
36 Correct 4 ms 13136 KB Output is correct
37 Correct 3 ms 13136 KB Output is correct
38 Correct 3 ms 13136 KB Output is correct
39 Correct 3 ms 12880 KB Output is correct
40 Correct 3 ms 12892 KB Output is correct
41 Correct 4 ms 13048 KB Output is correct
42 Correct 3 ms 12880 KB Output is correct
43 Correct 608 ms 14672 KB Output is correct
44 Correct 806 ms 16464 KB Output is correct
45 Correct 859 ms 16456 KB Output is correct
46 Correct 896 ms 16464 KB Output is correct
47 Correct 769 ms 14672 KB Output is correct
48 Correct 985 ms 16464 KB Output is correct
49 Correct 885 ms 16464 KB Output is correct
50 Correct 819 ms 16464 KB Output is correct
51 Correct 423 ms 12880 KB Output is correct
52 Correct 876 ms 13136 KB Output is correct
53 Correct 856 ms 13136 KB Output is correct
54 Correct 845 ms 13136 KB Output is correct
55 Correct 844 ms 18280 KB Output is correct
56 Correct 912 ms 18464 KB Output is correct
57 Correct 970 ms 18248 KB Output is correct
58 Correct 753 ms 18256 KB Output is correct
59 Correct 1008 ms 38728 KB Output is correct
60 Correct 935 ms 38624 KB Output is correct
61 Correct 582 ms 13136 KB Output is correct
62 Correct 862 ms 13136 KB Output is correct
63 Correct 860 ms 13136 KB Output is correct
64 Correct 870 ms 13136 KB Output is correct
65 Correct 877 ms 13136 KB Output is correct
66 Correct 842 ms 13136 KB Output is correct
67 Correct 836 ms 13136 KB Output is correct
68 Correct 858 ms 13136 KB Output is correct
69 Correct 847 ms 13136 KB Output is correct
70 Correct 879 ms 13136 KB Output is correct
71 Correct 797 ms 14160 KB Output is correct
72 Correct 837 ms 13136 KB Output is correct
73 Correct 891 ms 13232 KB Output is correct
74 Correct 909 ms 13136 KB Output is correct
75 Correct 871 ms 13136 KB Output is correct
76 Correct 850 ms 14160 KB Output is correct
77 Correct 849 ms 14160 KB Output is correct
78 Correct 813 ms 14160 KB Output is correct
79 Correct 833 ms 13392 KB Output is correct
80 Correct 879 ms 13136 KB Output is correct
81 Correct 843 ms 13136 KB Output is correct
82 Correct 843 ms 13136 KB Output is correct
83 Correct 452 ms 12880 KB Output is correct
84 Correct 893 ms 13136 KB Output is correct
85 Correct 851 ms 13136 KB Output is correct
86 Correct 885 ms 13136 KB Output is correct
87 Correct 2 ms 12880 KB Output is correct
88 Correct 512 ms 17844 KB Output is correct
89 Correct 891 ms 16712 KB Output is correct
90 Correct 971 ms 16512 KB Output is correct
91 Correct 985 ms 18516 KB Output is correct
92 Correct 971 ms 18512 KB Output is correct
93 Correct 986 ms 18504 KB Output is correct
94 Correct 898 ms 18320 KB Output is correct
95 Correct 949 ms 18504 KB Output is correct
96 Correct 918 ms 15816 KB Output is correct
97 Correct 951 ms 15816 KB Output is correct
98 Correct 870 ms 37152 KB Output is correct
99 Correct 909 ms 18248 KB Output is correct
100 Correct 1000 ms 16464 KB Output is correct
101 Correct 953 ms 15952 KB Output is correct
102 Correct 965 ms 14928 KB Output is correct
103 Correct 935 ms 38728 KB Output is correct
104 Correct 911 ms 39880 KB Output is correct
105 Correct 937 ms 39884 KB Output is correct
106 Correct 941 ms 21072 KB Output is correct
107 Correct 944 ms 15460 KB Output is correct
108 Correct 923 ms 15440 KB Output is correct
109 Correct 878 ms 15184 KB Output is correct