Submission #767455

# Submission time Handle Problem Language Result Execution time Memory
767455 2023-06-26T19:22:03 Z t6twotwo Digital Circuit (IOI22_circuit) C++17
2 / 100
12 ms 5660 KB
#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int mod = 1000002022;
int add(int x, int y) {
    int z = x + y;
    if (z >= mod) {
        z -= mod;
    }
    return z;
}
int sub(int x, int y) {
    int z = x - y;
    if (z < 0) {
        z += mod;
    }
    return z;
}
int mul(int x, int y) {
    return (ll)x * y % mod;
}
int N, M, K;
vector<int> P, A, tot, lazy, S, T;
void apply(int p, int v) {
    if (v) {
        S[p] = sub(T[p], S[p]);
        lazy[p] ^= 1;
    }
}
void push(int p) {
    apply(p * 2 + 1, lazy[p]);
    apply(p * 2 + 2, lazy[p]);
    lazy[p] = 0;
}
void pull(int p) {
    S[p] = add(S[p * 2 + 1], S[p * 2 + 2]);
}
void update(int p, int l, int r, int L, int R) {
    if (R <= l || r <= L) {
        return;
    }
    if (L <= l && r <= R) {
        apply(p, 1);
        return;
    }
    int m = (l + r + 1) / 2;
    push(p);
    update(p * 2 + 1, l, m, L, R);
    update(p * 2 + 2, m, r, L, R);
    pull(p);
}
void init(int n, int m, vector<int> p, vector<int> a) {
    N = n, M = m;
    P = p, A = a;
    vector<vector<int>> ch(N);
    for (int i = 1; i < N + M; i++) {
        ch[P[i]].push_back(i);
    }
    bool bintree = 1;
    for (int i = 0; i < N; i++) {
        if (ch[i].size() != 2) {
            bintree = 0;
        }
    }
    if (bintree) {
        tot.resize(N + M);
        for (int i = N; i < N + M; i++) {
            tot[i] = 1;
        }
        for (int i = N - 1; i >= 0; i--) {
            tot[i] = mul(2, mul(tot[i * 2 + 1], tot[i * 2 + 2]));
        }
        vector<int> f(N + M);
        f[0] = 1;
        for (int i = 1; i < N + M; i++) {
            int p = (i - 1) / 2;
            f[i] = mul(f[p], tot[ch[p][0] ^ ch[p][1] ^ i]);
        }
        K = 1 << __lg(M);
        S.resize(2 * K - 1);
        T.resize(2 * K - 1);
        for (int i = 0; i < M; i++) {
            if (A[i] == 1) {
                S[i + K - 1] = f[i];
            }
            T[i + K - 1] = f[i];
        }
        for (int i = K - 2; i >= 0; i--) {
            S[i] = add(S[i * 2 + 1], S[i * 2 + 2]);
            T[i] = add(T[i * 2 + 1], T[i * 2 + 2]);
        }
    }
}
int count_ways(int L, int R) {
    if (N <= 1000 && M <= 1000) {
        vector dp(N, vector{1});
        vector<int> on(N + M), off(N + M), tot(N + M, 1);
        for (int i = L; i <= R; i++) {
            A[i - N] ^= 1;
        }
        for (int i = N + M - 1; i >= 0; i--) {
            if (i >= N) {
                on[i] = A[i - N];
                off[i] = tot[i] - on[i];
            } else {
                int sum = 0;
                for (int j = dp[i].size() - 1; j; j--) {
                    sum = add(sum, dp[i][j]);
                    on[i] = add(on[i], sum);
                }
                tot[i] = mul(tot[i], (int)dp[i].size() - 1);
                off[i] = sub(tot[i], on[i]);
            }
            if (i) {
                int K = dp[P[i]].size();
                dp[P[i]].resize(K + 1);
                for (int j = K; j; j--) {
                    dp[P[i]][j] = add(mul(dp[P[i]][j], off[i]), mul(dp[P[i]][j - 1], on[i]));
                }
                dp[P[i]][0] = mul(dp[P[i]][0], off[i]);
                tot[P[i]] = mul(tot[P[i]], tot[i]);
            }
        }
        return on[0];
    }
    update(0, 0, K, L - N, R - N + 1);
    return S[0];
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 10 ms 208 KB Output is correct
4 Correct 11 ms 336 KB Output is correct
5 Correct 10 ms 336 KB Output is correct
6 Correct 10 ms 332 KB Output is correct
7 Correct 10 ms 208 KB Output is correct
8 Correct 10 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 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 Runtime error 1 ms 592 KB Execution killed with signal 6
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 10 ms 208 KB Output is correct
4 Correct 11 ms 336 KB Output is correct
5 Correct 10 ms 336 KB Output is correct
6 Correct 10 ms 332 KB Output is correct
7 Correct 10 ms 208 KB Output is correct
8 Correct 10 ms 208 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 1 ms 208 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 Runtime error 1 ms 592 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 5660 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 5660 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 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 Runtime error 1 ms 592 KB Execution killed with signal 6
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 10 ms 208 KB Output is correct
4 Correct 11 ms 336 KB Output is correct
5 Correct 10 ms 336 KB Output is correct
6 Correct 10 ms 332 KB Output is correct
7 Correct 10 ms 208 KB Output is correct
8 Correct 10 ms 208 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 1 ms 208 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 Runtime error 1 ms 592 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 10 ms 208 KB Output is correct
4 Correct 11 ms 336 KB Output is correct
5 Correct 10 ms 336 KB Output is correct
6 Correct 10 ms 332 KB Output is correct
7 Correct 10 ms 208 KB Output is correct
8 Correct 10 ms 208 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 1 ms 208 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 Runtime error 1 ms 592 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -