Submission #1074963

# Submission time Handle Problem Language Result Execution time Memory
1074963 2024-08-25T17:07:21 Z c2zi6 Digital Circuit (IOI22_circuit) C++17
61 / 100
3000 ms 31064 KB
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "circuit.h"

const ll MOD = 1000002022;

VL contr;
struct SEGTREE {
    int n;
    VL tree1, tree2;
    VI lazy;
    SEGTREE(){}
    SEGTREE(VI val) {
        n = 1;
        while (n < val.size()) n *= 2;
        tree1 = tree2 = VL(2*n);
        lazy = VI(2*n);
        rep(i, val.size()) {
            if (val[i]) tree1[i + n-1] = contr[i];
            else tree2[i + n-1] = contr[i];
        }
        build(0, 0, n-1);
    }
    void build(int N, int L, int R) {
        if (L == R) return;
        int M = (L + R) / 2;
        build(2*N+1, L, M);
        build(2*N+2, M+1, R);
        tree1[N] = (tree1[2*N+1] + tree1[2*N+2]) % MOD;
        tree2[N] = (tree2[2*N+1] + tree2[2*N+2]) % MOD;
    }
    void push(int N) {
        if (lazy[N]%2) swap(tree1[N], tree2[N]);
        if (2*N+2 < 2*n) {
            lazy[2*N+1] += lazy[N];
            lazy[2*N+2] += lazy[N];
        }
        lazy[N] = 0;
    }
    void upd(int N, int L, int R, int l, int r) {
        push(N);
        if (R < l || L > r) return;
        if (l <= L && R <= r) {
            lazy[N]++;
            push(N);
            return;
        }
        int M = (L + R) / 2;
        upd(2*N+1, L, M, l, r);
        upd(2*N+2, M+1, R, l, r);
        tree1[N] = (tree1[2*N+1] + tree1[2*N+2]) % MOD;
        tree2[N] = (tree2[2*N+1] + tree2[2*N+2]) % MOD;
    }
    void upd(int l, int r) {
        upd(0, 0, n-1, l, r);
    }
    ll get() {
        return tree1[0];
    }
    void print() {
        return;
        rep(u, 2*n-1) {
            cout << tree1[u] << " ";
            if (set<int>{0, 2, 6, 14, 30}.count(u)) cout << endl;
        }
        rep(u, 2*n-1) {
            cout << tree2[u] << " ";
            if (set<int>{0, 2, 6, 14, 30}.count(u)) cout << endl;
        }
        rep(u, 2*n-1) {
            cout << lazy[u] << " ";
            if (set<int>{0, 2, 6, 14, 30}.count(u)) cout << endl;
        }
        cout << "-------------------------------" << endl;
    }
} seg;

bool test6;

int n, sz;
VVI gp;
VI val;

VL dpa; // sev
VL dpb; // spitak
VI depth;


void dfs2(int u = 0) {
    for (int v : gp[u]) {
        depth[v] = depth[u]+1;
        dfs2(v);
    }
}

void init(int N, int M, VI P, VI A) {
    n = N + M;
    sz = M;
    gp = VVI(n);
    rep(u, n) if (u) gp[P[u]].pb(u);
    val = VI(n);
    rep(u, M) val[u+N] = A[u];
    depth = VI(n);
    test6 = true;
    rep(u, N) if (gp[u].size() != 2) test6 = false;
    if (test6) {
        /*cout << "TEST 6" << endl;*/
        dfs2();
        contr = VL(n);
        VL twopow(n+1);
        twopow[0] = 1;
        replr(i, 1, n) twopow[i] = twopow[i-1]*2 % MOD;
        rep(u, n) contr[u] = twopow[N - depth[u]];
        seg = SEGTREE(val);
        /*for (ll x : contr) cout << x << " "; cout << endl;*/
        /*cout << "-------------------------------" << endl;*/
        /*seg.print();*/
    }
}

VL subsets(VL& a, VL& b) {
    int n = a.size();
    VVL dp(n+1, VL(n+1));
    dp[0][0] = 1;
    replr(j, 1, n) dp[j][0] = dp[j-1][0]*b[j-1]%MOD;
    replr(i, 1, n) replr(j, 1, n) {
        dp[i][j] = dp[i-1][j-1]*a[i-1]%MOD + dp[i-1][j]*b[i-1]%MOD;
        dp[i][j] %= MOD;
    }
    VL ret(n+1);
    replr(i, 0, n) ret[i] = dp[n][i];
    return ret;
}
void dfs1(int u = 0) {
    for (int v : gp[u]) dfs1(v);
    dpa[u] = dpb[u] = 0;
    if (gp[u].size() == 0) {
        if (val[u]) dpa[u] = 1;
        else dpb[u] = 1;
        return;
    }
    VL a, b;
    for (int v : gp[u]) {
        a.pb(dpa[v]);
        b.pb(dpb[v]);
    }
    int c = gp[u].size();
    VL s = subsets(a, b);
    replr(i, 0, c) {
        dpa[u] += s[i]*i % MOD;
        dpa[u] %= MOD;
        dpb[u] += s[i]*(c-i) % MOD;
        dpb[u] %= MOD;
    }
}

int count_ways(int L, int R) {
    if (test6) {
        seg.upd(L, R);
        seg.print();
        return seg.get();
        /*cout << seg.get() << " == ";*/
        /*return seg.get();*/
        /*replr(i, L, R) val[i] = !val[i];*/
        /*ll ans = 0;*/
        /*rep(i, n) if (val[i]) ans += contr[i];*/
        /*return ans;*/
    }
    replr(i, L, R) val[i] = !val[i];
    dpa = dpb = VL(n);
    dfs1();
    return dpa[0];
}




Compilation message

circuit.cpp: In constructor 'SEGTREE::SEGTREE(VI)':
circuit.cpp:44:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |         while (n < val.size()) n *= 2;
      |                ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 31 ms 7972 KB Output is correct
4 Correct 33 ms 8380 KB Output is correct
5 Correct 36 ms 8384 KB Output is correct
6 Correct 37 ms 8688 KB Output is correct
7 Correct 35 ms 8380 KB Output is correct
8 Correct 33 ms 8380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 696 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 31 ms 7972 KB Output is correct
4 Correct 33 ms 8380 KB Output is correct
5 Correct 36 ms 8384 KB Output is correct
6 Correct 37 ms 8688 KB Output is correct
7 Correct 35 ms 8380 KB Output is correct
8 Correct 33 ms 8380 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 600 KB Output is correct
18 Correct 1 ms 696 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 0 ms 600 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 2 ms 344 KB Output is correct
25 Correct 2 ms 344 KB Output is correct
26 Correct 2 ms 344 KB Output is correct
27 Correct 2 ms 600 KB Output is correct
28 Correct 2 ms 344 KB Output is correct
29 Correct 34 ms 8380 KB Output is correct
30 Correct 36 ms 8380 KB Output is correct
31 Correct 2 ms 600 KB Output is correct
32 Correct 1 ms 540 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 6 ms 872 KB Output is correct
36 Correct 2 ms 600 KB Output is correct
37 Correct 34 ms 8688 KB Output is correct
38 Correct 36 ms 8636 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 455 ms 8004 KB Output is correct
2 Correct 673 ms 15696 KB Output is correct
3 Correct 698 ms 15704 KB Output is correct
4 Correct 748 ms 15704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 455 ms 8004 KB Output is correct
2 Correct 673 ms 15696 KB Output is correct
3 Correct 698 ms 15704 KB Output is correct
4 Correct 748 ms 15704 KB Output is correct
5 Correct 595 ms 8024 KB Output is correct
6 Correct 765 ms 15704 KB Output is correct
7 Correct 744 ms 15696 KB Output is correct
8 Correct 708 ms 15696 KB Output is correct
9 Correct 290 ms 908 KB Output is correct
10 Correct 665 ms 1384 KB Output is correct
11 Correct 675 ms 1388 KB Output is correct
12 Correct 663 ms 1388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 696 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
13 Correct 455 ms 8004 KB Output is correct
14 Correct 673 ms 15696 KB Output is correct
15 Correct 698 ms 15704 KB Output is correct
16 Correct 748 ms 15704 KB Output is correct
17 Correct 595 ms 8024 KB Output is correct
18 Correct 765 ms 15704 KB Output is correct
19 Correct 744 ms 15696 KB Output is correct
20 Correct 708 ms 15696 KB Output is correct
21 Correct 290 ms 908 KB Output is correct
22 Correct 665 ms 1384 KB Output is correct
23 Correct 675 ms 1388 KB Output is correct
24 Correct 663 ms 1388 KB Output is correct
25 Correct 731 ms 25936 KB Output is correct
26 Correct 764 ms 26180 KB Output is correct
27 Correct 823 ms 26192 KB Output is correct
28 Correct 607 ms 26188 KB Output is correct
29 Correct 736 ms 31064 KB Output is correct
30 Correct 694 ms 31052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 31 ms 7972 KB Output is correct
4 Correct 33 ms 8380 KB Output is correct
5 Correct 36 ms 8384 KB Output is correct
6 Correct 37 ms 8688 KB Output is correct
7 Correct 35 ms 8380 KB Output is correct
8 Correct 33 ms 8380 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 600 KB Output is correct
18 Correct 1 ms 696 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 0 ms 600 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 2 ms 344 KB Output is correct
25 Correct 2 ms 344 KB Output is correct
26 Correct 2 ms 344 KB Output is correct
27 Correct 2 ms 600 KB Output is correct
28 Correct 2 ms 344 KB Output is correct
29 Correct 34 ms 8380 KB Output is correct
30 Correct 36 ms 8380 KB Output is correct
31 Correct 2 ms 600 KB Output is correct
32 Correct 1 ms 540 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 6 ms 872 KB Output is correct
36 Correct 2 ms 600 KB Output is correct
37 Correct 34 ms 8688 KB Output is correct
38 Correct 36 ms 8636 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 2 ms 344 KB Output is correct
43 Execution timed out 3016 ms 856 KB Time limit exceeded
44 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 31 ms 7972 KB Output is correct
4 Correct 33 ms 8380 KB Output is correct
5 Correct 36 ms 8384 KB Output is correct
6 Correct 37 ms 8688 KB Output is correct
7 Correct 35 ms 8380 KB Output is correct
8 Correct 33 ms 8380 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 600 KB Output is correct
18 Correct 1 ms 696 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 0 ms 600 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 2 ms 344 KB Output is correct
25 Correct 2 ms 344 KB Output is correct
26 Correct 2 ms 344 KB Output is correct
27 Correct 2 ms 600 KB Output is correct
28 Correct 2 ms 344 KB Output is correct
29 Correct 34 ms 8380 KB Output is correct
30 Correct 36 ms 8380 KB Output is correct
31 Correct 2 ms 600 KB Output is correct
32 Correct 1 ms 540 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 6 ms 872 KB Output is correct
36 Correct 2 ms 600 KB Output is correct
37 Correct 34 ms 8688 KB Output is correct
38 Correct 36 ms 8636 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 2 ms 344 KB Output is correct
43 Correct 455 ms 8004 KB Output is correct
44 Correct 673 ms 15696 KB Output is correct
45 Correct 698 ms 15704 KB Output is correct
46 Correct 748 ms 15704 KB Output is correct
47 Correct 595 ms 8024 KB Output is correct
48 Correct 765 ms 15704 KB Output is correct
49 Correct 744 ms 15696 KB Output is correct
50 Correct 708 ms 15696 KB Output is correct
51 Correct 290 ms 908 KB Output is correct
52 Correct 665 ms 1384 KB Output is correct
53 Correct 675 ms 1388 KB Output is correct
54 Correct 663 ms 1388 KB Output is correct
55 Correct 731 ms 25936 KB Output is correct
56 Correct 764 ms 26180 KB Output is correct
57 Correct 823 ms 26192 KB Output is correct
58 Correct 607 ms 26188 KB Output is correct
59 Correct 736 ms 31064 KB Output is correct
60 Correct 694 ms 31052 KB Output is correct
61 Execution timed out 3016 ms 856 KB Time limit exceeded
62 Halted 0 ms 0 KB -