Submission #705587

# Submission time Handle Problem Language Result Execution time Memory
705587 2023-03-04T17:23:56 Z danikoynov Digital Circuit (IOI22_circuit) C++17
100 / 100
1393 ms 48364 KB
#include "circuit.h"

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll mod = 1000002022;
const int maxn = 2e5 + 10;

int n, m, p[maxn], a[maxn];
vector < int > children[maxn];
ll dp[maxn][2], temp[maxn];

void update_state(int v, bool type)
{
    dp[v][0] = dp[v][1] = 0;

    if (v >= n)
    {
        dp[v][1] = a[v - n];
        dp[v][0] = 1 - dp[v][1];
        return;
    }

    int c = children[v].size();
    for (int i = 0; i <= c; i ++)
        temp[i] = 0;


    temp[0] = 1;

    for (int u : children[v])
    {
        int i = c;
        if (type == false)
            i = 1;
        for (i; i >= 0; i --)
        {
            temp[i] = (temp[i] * dp[u][0]) % mod;
            if (i > 0)
                temp[i] = (temp[i] + temp[i - 1] * dp[u][1]) % mod;
        }
    }



    for (int i = 0; i <= c; i ++)
    {
        dp[v][0] = (dp[v][0] + temp[i] * (ll)(c - i)) % mod;
        dp[v][1] = (dp[v][1] + temp[i] * (ll)(i)) % mod;
    }
    ///cout << v << " " << dp[v][0] << " " << dp[v][1] << endl;

}


void solve_state(int v)
{


    for (int u : children[v])
    {
        solve_state(u);
    }

    update_state(v, false);



}
ll spec_val[maxn], depth[maxn];
void fix_state(int v)
{

    if (v >= n)
    {
        return;
    }

int c = children[v].size();
    vector < ll > exp;
    for (int u : children[v])
    {

        exp.push_back(dp[u][0]);
    }

    vector < ll > pref[2];
    pref[0].resize(c);
    pref[1].resize(c);
    pref[0][0] = exp[0];
    for (int i = 1; i < c; i ++)
    {
        pref[0][i] = (pref[0][i - 1] * exp[i]) % mod;
    }
    pref[1][c - 1] = exp[c - 1];
    for (int i = c - 2; i >= 0; i --)
    {
        pref[1][i] = (pref[1][i + 1] * exp[i]) % mod;
    }

    for (int i = 0; i < children[v].size(); i ++)
    {
        int u = children[v][i];
        ll cur = 1;
        if (i > 0)
            cur = (cur * pref[0][i - 1]) % mod;

        if (i + 1 < c)
        {

            cur = (cur * pref[1][i + 1]) % mod;
        }
        ///cout << v << " : " << i << " : " << u << " " << cur << endl;
        depth[u] = (depth[v] * cur) % mod;
    }

    for (int u : children[v])
        fix_state(u);



}

ll pw[maxn], pref[maxn];
ll con[maxn];
int act = 0, logM = 0;
int tree[4 * maxn], lazy[4 * maxn];

void build_tree(int root, int left, int right)
{
    if (left == right)
    {
        tree[root] = (con[left] * a[left]) % mod;
        return;
    }

    int mid = (left + right) / 2;
    build_tree(root * 2, left, mid);
    build_tree(root * 2 + 1, mid + 1, right);

    tree[root] = (tree[root * 2] + tree[root * 2 + 1]) % mod;
}

ll get_sum(int left, int right)
{
    ll sum = pref[right];
    if (left != 0)
        sum = sum - pref[left - 1];
    if (sum < 0)
        sum += mod;
    return sum;
}

void push_lazy(int root, int left, int right)
{
    if (lazy[root] == 0)
        return;

    tree[root] = (get_sum(left, right) - tree[root] + mod) % mod;
    if (left != right)
    {
        lazy[root * 2] = (lazy[root * 2] + 1) % 2;
        lazy[root * 2 + 1] = (lazy[root * 2 + 1] + 1) % 2;
    }
    lazy[root] = 0;
}

void update_range(int root, int left, int right, int qleft, int qright)
{

    push_lazy(root, left, right);
    if (left > qright || right < qleft)
        return;

    if (left >= qleft && right <= qright)
    {
        lazy[root] = 1;
        push_lazy(root, left, right);
        ///cout << left << " :: " << right << " :: " << tree[root] << endl;
        return;
    }

    int mid = (left + right) / 2;
    update_range(root * 2, left, mid, qleft, qright);
    update_range(root * 2 + 1, mid + 1, right, qleft,  qright);
    ///cout << root << " " << left << " " << right << " " << qleft << " " << qright << endl;
    tree[root] = (tree[root * 2] + tree[root * 2 + 1]) % mod;
    ///cout << tree[root] << endl;
}


ll power(ll base, ll st)
{
    ll ans = 1;
    while(st > 0)
    {
        if (st % 2 == 1)
            ans = (ans * base) % mod;
        base = (base * base) % mod;
        st /= 2;
    }
    return ans;
}
void init(int N, int M, std::vector<int> P, std::vector<int> A)
{

    n = N;
    m = M;
    for (int i = 0; i < N + M; i ++)
        p[i] = P[i];


    for (int i = 1; i < N + M; i ++)
    {
        children[p[i]].push_back(i);
    }



    depth[0] = 1;
    solve_state(0);
    fix_state(0);
    for (int i = 0; i < M; i ++)
        con[i] = depth[i + n];

    /**for (int i = 0; i < M; i ++)
    {
        a[i] = 1;
        int v = i + n;
        while(v != -1)
        {
            update_state(v, false);
            v = p[v];
        }
        ///solve_state(0);
        con[i] = dp[0][1];
        a[i] = 0;
                 v = i + n;
        while(v != -1)
        {
            update_state(v, false);
            v = p[v];
        }
    }*/
    pref[0] = con[0];
    for (int i = 1; i < M; i ++)
        pref[i] = (pref[i - 1] + con[i]) % mod;
    for (int i = 0; i < M; i ++)
        a[i] = A[i];


    build_tree(1, 0, m - 1);

}


int count_ways(int L, int R)
{
    update_range(1, 0, m - 1, L - n, R - n);
    ///for (int i = L - n; i <= R - n; i ++)
       /// a[i] = ((a[i] + 1) & 1);

    /**ll ans = 0;
    for (int i = 0; i < m; i ++)
    {

        ans = (ans + (con[i] * a[i])) % mod;
    }*/


    /**cout << "-----------" << endl;
    for (int i = 0; i < n + m; i ++)
        cout << i << " : " << dp[i][0] << " " << dp[i][1] << endl;*/

    return tree[1];
}

Compilation message

circuit.cpp: In function 'void update_state(int, bool)':
circuit.cpp:37:14: warning: statement has no effect [-Wunused-value]
   37 |         for (i; i >= 0; i --)
      |              ^
circuit.cpp: In function 'void fix_state(int)':
circuit.cpp:102:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |     for (int i = 0; i < children[v].size(); i ++)
      |                     ~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 5180 KB Output is correct
4 Correct 4 ms 5072 KB Output is correct
5 Correct 4 ms 5072 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 3 ms 5072 KB Output is correct
3 Correct 3 ms 5008 KB Output is correct
4 Correct 4 ms 5072 KB Output is correct
5 Correct 4 ms 5072 KB Output is correct
6 Correct 4 ms 5072 KB Output is correct
7 Correct 4 ms 5200 KB Output is correct
8 Correct 3 ms 5200 KB Output is correct
9 Correct 4 ms 5200 KB Output is correct
10 Correct 4 ms 5456 KB Output is correct
11 Correct 4 ms 5456 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 5180 KB Output is correct
4 Correct 4 ms 5072 KB Output is correct
5 Correct 4 ms 5072 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5072 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 3 ms 5072 KB Output is correct
11 Correct 3 ms 5008 KB Output is correct
12 Correct 4 ms 5072 KB Output is correct
13 Correct 4 ms 5072 KB Output is correct
14 Correct 4 ms 5072 KB Output is correct
15 Correct 4 ms 5200 KB Output is correct
16 Correct 3 ms 5200 KB Output is correct
17 Correct 4 ms 5200 KB Output is correct
18 Correct 4 ms 5456 KB Output is correct
19 Correct 4 ms 5456 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 3 ms 5192 KB Output is correct
22 Correct 3 ms 5072 KB Output is correct
23 Correct 3 ms 5072 KB Output is correct
24 Correct 5 ms 5200 KB Output is correct
25 Correct 4 ms 5200 KB Output is correct
26 Correct 4 ms 5248 KB Output is correct
27 Correct 5 ms 5200 KB Output is correct
28 Correct 5 ms 5248 KB Output is correct
29 Correct 5 ms 5184 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5328 KB Output is correct
32 Correct 4 ms 5200 KB Output is correct
33 Correct 3 ms 5072 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 3 ms 5160 KB Output is correct
36 Correct 9 ms 5456 KB Output is correct
37 Correct 3 ms 5456 KB Output is correct
38 Correct 4 ms 5440 KB Output is correct
39 Correct 3 ms 5200 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 4 ms 5092 KB Output is correct
42 Correct 4 ms 5160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 500 ms 9492 KB Output is correct
2 Correct 836 ms 13984 KB Output is correct
3 Correct 751 ms 13964 KB Output is correct
4 Correct 909 ms 13960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 500 ms 9492 KB Output is correct
2 Correct 836 ms 13984 KB Output is correct
3 Correct 751 ms 13964 KB Output is correct
4 Correct 909 ms 13960 KB Output is correct
5 Correct 833 ms 9480 KB Output is correct
6 Correct 1393 ms 13972 KB Output is correct
7 Correct 960 ms 14024 KB Output is correct
8 Correct 1010 ms 13944 KB Output is correct
9 Correct 486 ms 5328 KB Output is correct
10 Correct 896 ms 5584 KB Output is correct
11 Correct 862 ms 5584 KB Output is correct
12 Correct 998 ms 5584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 3 ms 5072 KB Output is correct
3 Correct 3 ms 5008 KB Output is correct
4 Correct 4 ms 5072 KB Output is correct
5 Correct 4 ms 5072 KB Output is correct
6 Correct 4 ms 5072 KB Output is correct
7 Correct 4 ms 5200 KB Output is correct
8 Correct 3 ms 5200 KB Output is correct
9 Correct 4 ms 5200 KB Output is correct
10 Correct 4 ms 5456 KB Output is correct
11 Correct 4 ms 5456 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 500 ms 9492 KB Output is correct
14 Correct 836 ms 13984 KB Output is correct
15 Correct 751 ms 13964 KB Output is correct
16 Correct 909 ms 13960 KB Output is correct
17 Correct 833 ms 9480 KB Output is correct
18 Correct 1393 ms 13972 KB Output is correct
19 Correct 960 ms 14024 KB Output is correct
20 Correct 1010 ms 13944 KB Output is correct
21 Correct 486 ms 5328 KB Output is correct
22 Correct 896 ms 5584 KB Output is correct
23 Correct 862 ms 5584 KB Output is correct
24 Correct 998 ms 5584 KB Output is correct
25 Correct 1014 ms 18692 KB Output is correct
26 Correct 1111 ms 18980 KB Output is correct
27 Correct 1059 ms 18900 KB Output is correct
28 Correct 843 ms 18932 KB Output is correct
29 Correct 1169 ms 47048 KB Output is correct
30 Correct 932 ms 47116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 5180 KB Output is correct
4 Correct 4 ms 5072 KB Output is correct
5 Correct 4 ms 5072 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5072 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 3 ms 5072 KB Output is correct
11 Correct 3 ms 5008 KB Output is correct
12 Correct 4 ms 5072 KB Output is correct
13 Correct 4 ms 5072 KB Output is correct
14 Correct 4 ms 5072 KB Output is correct
15 Correct 4 ms 5200 KB Output is correct
16 Correct 3 ms 5200 KB Output is correct
17 Correct 4 ms 5200 KB Output is correct
18 Correct 4 ms 5456 KB Output is correct
19 Correct 4 ms 5456 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 3 ms 5192 KB Output is correct
22 Correct 3 ms 5072 KB Output is correct
23 Correct 3 ms 5072 KB Output is correct
24 Correct 5 ms 5200 KB Output is correct
25 Correct 4 ms 5200 KB Output is correct
26 Correct 4 ms 5248 KB Output is correct
27 Correct 5 ms 5200 KB Output is correct
28 Correct 5 ms 5248 KB Output is correct
29 Correct 5 ms 5184 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5328 KB Output is correct
32 Correct 4 ms 5200 KB Output is correct
33 Correct 3 ms 5072 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 3 ms 5160 KB Output is correct
36 Correct 9 ms 5456 KB Output is correct
37 Correct 3 ms 5456 KB Output is correct
38 Correct 4 ms 5440 KB Output is correct
39 Correct 3 ms 5200 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 4 ms 5092 KB Output is correct
42 Correct 4 ms 5160 KB Output is correct
43 Correct 499 ms 5456 KB Output is correct
44 Correct 777 ms 5456 KB Output is correct
45 Correct 867 ms 5456 KB Output is correct
46 Correct 841 ms 5712 KB Output is correct
47 Correct 829 ms 5840 KB Output is correct
48 Correct 732 ms 5840 KB Output is correct
49 Correct 962 ms 5840 KB Output is correct
50 Correct 899 ms 5772 KB Output is correct
51 Correct 718 ms 5632 KB Output is correct
52 Correct 674 ms 5680 KB Output is correct
53 Correct 610 ms 6736 KB Output is correct
54 Correct 773 ms 5712 KB Output is correct
55 Correct 868 ms 5584 KB Output is correct
56 Correct 782 ms 5584 KB Output is correct
57 Correct 915 ms 5524 KB Output is correct
58 Correct 797 ms 7312 KB Output is correct
59 Correct 730 ms 7340 KB Output is correct
60 Correct 739 ms 7340 KB Output is correct
61 Correct 832 ms 5840 KB Output is correct
62 Correct 813 ms 5456 KB Output is correct
63 Correct 797 ms 5452 KB Output is correct
64 Correct 748 ms 5584 KB Output is correct
65 Correct 369 ms 5328 KB Output is correct
66 Correct 876 ms 5640 KB Output is correct
67 Correct 973 ms 5584 KB Output is correct
68 Correct 757 ms 5584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 5180 KB Output is correct
4 Correct 4 ms 5072 KB Output is correct
5 Correct 4 ms 5072 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5072 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 3 ms 5072 KB Output is correct
11 Correct 3 ms 5008 KB Output is correct
12 Correct 4 ms 5072 KB Output is correct
13 Correct 4 ms 5072 KB Output is correct
14 Correct 4 ms 5072 KB Output is correct
15 Correct 4 ms 5200 KB Output is correct
16 Correct 3 ms 5200 KB Output is correct
17 Correct 4 ms 5200 KB Output is correct
18 Correct 4 ms 5456 KB Output is correct
19 Correct 4 ms 5456 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 3 ms 5192 KB Output is correct
22 Correct 3 ms 5072 KB Output is correct
23 Correct 3 ms 5072 KB Output is correct
24 Correct 5 ms 5200 KB Output is correct
25 Correct 4 ms 5200 KB Output is correct
26 Correct 4 ms 5248 KB Output is correct
27 Correct 5 ms 5200 KB Output is correct
28 Correct 5 ms 5248 KB Output is correct
29 Correct 5 ms 5184 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5328 KB Output is correct
32 Correct 4 ms 5200 KB Output is correct
33 Correct 3 ms 5072 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 3 ms 5160 KB Output is correct
36 Correct 9 ms 5456 KB Output is correct
37 Correct 3 ms 5456 KB Output is correct
38 Correct 4 ms 5440 KB Output is correct
39 Correct 3 ms 5200 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 4 ms 5092 KB Output is correct
42 Correct 4 ms 5160 KB Output is correct
43 Correct 500 ms 9492 KB Output is correct
44 Correct 836 ms 13984 KB Output is correct
45 Correct 751 ms 13964 KB Output is correct
46 Correct 909 ms 13960 KB Output is correct
47 Correct 833 ms 9480 KB Output is correct
48 Correct 1393 ms 13972 KB Output is correct
49 Correct 960 ms 14024 KB Output is correct
50 Correct 1010 ms 13944 KB Output is correct
51 Correct 486 ms 5328 KB Output is correct
52 Correct 896 ms 5584 KB Output is correct
53 Correct 862 ms 5584 KB Output is correct
54 Correct 998 ms 5584 KB Output is correct
55 Correct 1014 ms 18692 KB Output is correct
56 Correct 1111 ms 18980 KB Output is correct
57 Correct 1059 ms 18900 KB Output is correct
58 Correct 843 ms 18932 KB Output is correct
59 Correct 1169 ms 47048 KB Output is correct
60 Correct 932 ms 47116 KB Output is correct
61 Correct 499 ms 5456 KB Output is correct
62 Correct 777 ms 5456 KB Output is correct
63 Correct 867 ms 5456 KB Output is correct
64 Correct 841 ms 5712 KB Output is correct
65 Correct 829 ms 5840 KB Output is correct
66 Correct 732 ms 5840 KB Output is correct
67 Correct 962 ms 5840 KB Output is correct
68 Correct 899 ms 5772 KB Output is correct
69 Correct 718 ms 5632 KB Output is correct
70 Correct 674 ms 5680 KB Output is correct
71 Correct 610 ms 6736 KB Output is correct
72 Correct 773 ms 5712 KB Output is correct
73 Correct 868 ms 5584 KB Output is correct
74 Correct 782 ms 5584 KB Output is correct
75 Correct 915 ms 5524 KB Output is correct
76 Correct 797 ms 7312 KB Output is correct
77 Correct 730 ms 7340 KB Output is correct
78 Correct 739 ms 7340 KB Output is correct
79 Correct 832 ms 5840 KB Output is correct
80 Correct 813 ms 5456 KB Output is correct
81 Correct 797 ms 5452 KB Output is correct
82 Correct 748 ms 5584 KB Output is correct
83 Correct 369 ms 5328 KB Output is correct
84 Correct 876 ms 5640 KB Output is correct
85 Correct 973 ms 5584 KB Output is correct
86 Correct 757 ms 5584 KB Output is correct
87 Correct 3 ms 5072 KB Output is correct
88 Correct 627 ms 17552 KB Output is correct
89 Correct 879 ms 13768 KB Output is correct
90 Correct 892 ms 13640 KB Output is correct
91 Correct 808 ms 19084 KB Output is correct
92 Correct 978 ms 19048 KB Output is correct
93 Correct 1253 ms 19108 KB Output is correct
94 Correct 1179 ms 19052 KB Output is correct
95 Correct 1170 ms 19004 KB Output is correct
96 Correct 1071 ms 13548 KB Output is correct
97 Correct 795 ms 13900 KB Output is correct
98 Correct 929 ms 39872 KB Output is correct
99 Correct 890 ms 18984 KB Output is correct
100 Correct 869 ms 15720 KB Output is correct
101 Correct 1112 ms 14672 KB Output is correct
102 Correct 937 ms 13152 KB Output is correct
103 Correct 1050 ms 47132 KB Output is correct
104 Correct 997 ms 48364 KB Output is correct
105 Correct 680 ms 48364 KB Output is correct
106 Correct 720 ms 19456 KB Output is correct
107 Correct 813 ms 13408 KB Output is correct
108 Correct 992 ms 13380 KB Output is correct
109 Correct 909 ms 13328 KB Output is correct