Submission #963107

# Submission time Handle Problem Language Result Execution time Memory
963107 2024-04-14T14:20:20 Z anango Digital Circuit (IOI22_circuit) C++17
100 / 100
779 ms 50368 KB
#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
int MOD=1000002022;
vector<int> allprod; //for each node, product of all threshold totals in subtree of the node
vector<int> subt_values; //for each node, product of all threshold totals not on path from node to root
vector<vector<int>> children;
vector<int> ison;
int dfs2currentprod = 1; //the current product of the dfs2 values   
int curS = 0;
int n,m;

void dfs1(int node) {
    allprod[node] = 1;
    for (int j:children[node]) {
        dfs1(j);
        allprod[node] *= allprod[j];
        allprod[node]%=MOD;
    }
    if (children[node].size()) allprod[node]*=children[node].size();
    allprod[node]%=MOD;
}
void dfs2(int node) {
    //calculate the product of all threshold totals not on the path from node to root, for each leaf
    if (!children[node].size()) {
        subt_values[node] = dfs2currentprod;
        return;
    }
    int oldprod = dfs2currentprod;
    int k=children[node].size();
    vector<int> prefprod(k+1,1); //product of sub product for all children with index<i
    for (int i=1; i<=k; i++) {
        prefprod[i] = prefprod[i-1]*allprod[children[node][i-1]];
        prefprod[i]%=MOD;
    }
    vector<int> suffprod(k+1,1); //product of sub product for all children with index>=i
    for (int i=k-1; i>=0; i--) {
        suffprod[i] = suffprod[i+1]*allprod[children[node][i]];
        suffprod[i]%=MOD;
    }
    /*cout << "DFS " << node << " " << dfs2currentprod;
    for (int i=0; i<k+1; i++) {
        cout << i <<" " << prefprod[i] <<" " << suffprod[i] << endl;
    }*/
    for (int i=0; i<k; i++) {
        int totprod=prefprod[i]*suffprod[i+1];
        totprod%=MOD;
        totprod*=oldprod;
        totprod%=MOD;
        dfs2currentprod = totprod;
        dfs2(children[node][i]);
    }
}
vector<int> prefsums; //prefsums[j] = sum of normal contribution BEFORE j
//so do like prefsums[r+1]-prefsums[l]
int maxn=262144;
int MAXN=maxn;
vector<int> tree;
vector<int> lazy;

int getcont(int tl, int tr) {
   // cout << "jam1" << endl;
    int ans=prefsums[tr+1]-prefsums[tl];
   // cout << "jam2" << endl;
    return ans;
}
void push(int node, int tl, int tr) {
    if (lazy[node]==0) {
        return;
    }
    if (lazy[node]==1) {
        tree[node] = ((getcont(tl,tr)-tree[node])%MOD+MOD)%MOD;
        if (tl<tr) {
            lazy[node*2] ^= lazy[node];
            lazy[node*2+1]^=lazy[node];
        }
        lazy[node] = 0;
    }
    
}
void update(int node, int l, int r, int tl, int tr) {
    //cout << "updating " << node <<" " << l <<" "<<r <<" "<< tl <<" " << tr << endl;
    //flip all bits between l and r inclusive
    push(node,tl,tr);
    if (l>tr || r<tl) {
        return;
    }
    if (l<=tl && tr<=r) {
        //full overlap
        tree[node] = getcont(tl, tr) - tree[node];
        tree[node] += MOD;
        tree[node]%=MOD;
        if (tl<tr) {
            lazy[node*2] ^= 1;
            lazy[node*2+1] ^= 1;
        }
        return;
    }
    int m=(tl+tr)/2;
    update(node*2, l, r, tl, m);
    update(node*2+1, l, r, m+1, tr);
    tree[node] = tree[node*2]+tree[node*2+1];
    tree[node]+=MOD;
    tree[node]%=MOD;
    //cout << node <<" "<< tree[node] << " " << tl <<" "<< tr << endl;
    //cout << node <<" " <<tree[node*2] <<" " << tree[node*2+1] << endl;
    //1113
    //1010
    //0110
    //0000
    return;

}
int query(int node, int l, int r, int tl, int tr) {
    //cout << "querying " << node <<" " << l <<" "<<r <<" "<< tl <<" " << tr << endl;

    push(node,tl,tr);
    if (l>tr || r<tl) {
        return 0;
    }
    if (l<=tl && tr<=r) {
        return tree[node];
    }
    int m=(tl+tr)/2;
    int q1 = query(node*2, l, r, tl, m);
    int q2 = query(node*2+1, l, r, m+1, tr);
    int qsum = q1+q2;
    return qsum%MOD;
}

void init(int32_t N, int32_t M, vector<int32_t> P, vector<int32_t> A) {
    n=N;
    m=M;
    allprod = vector<int>(N+M, 1);
    subt_values = vector<int>(N+M, 1);
    ison = vector<int>(M, 1);
    children=vector<vector<int>>(N+M);
    tree=vector<int>(4*maxn,0);
    lazy=vector<int>(4*maxn,0); //bit
    for (int i=1; i<N+M; i++) {
        children[P[i]].push_back(i);
    }
    dfs1(0);
    dfs2(0);
    /*for (int i=0; i<N+M; i++) {
        cout << i <<" " << allprod[i] << endl;
    }
    for (int i=0; i<N+M; i++) {
        cout << i <<" " << subt_values[i] << endl;
    }*/
    prefsums=vector<int>(MAXN+1, 0);
    prefsums[0] = 0; /*3, 4, [-1, 0, 1, 2, 1, 1, 0], [1, 0, 1, 0]*/
    for (int i=1; i<MAXN+1; i++) {
        //cout << i << endl;
        int sv;
        if (i>M) {
            sv=0;
        }
        else {
            sv=subt_values[i-1+N];
        }
        prefsums[i] = (prefsums[i-1]+sv)%MOD;
    } 
    for (int i=0; i<M; i++) {
        //cout << subt_values[i+N] << " ";
    }
    cout << endl;
    //cout << "after init" << endl;
    for (int i=N; i<N+M; i++) {
        ison[i-N] = A[i-N];
        if (A[i-N]==1) {
            
            update(1, i-N, i-N, 0, maxn-1);
        }
        //cout << i << " " << i-N << endl;
    }
    
    

}
/*3 6 5
-1 0 0 1 1 1 2 2 0
0 0 0 0 0 0
3 8*/
int32_t count_ways(int32_t L, int32_t R) {
   /**for (int i=L; i<=R; i++) {
        ison[i-n] = 1-ison[i-n];
    }
    int su=0;
    for (int i=0; i<m; i++) {
        //cout << i <<" " << subt_values[i+n] << endl;
        su+=ison[i]*subt_values[i+n];
        su%=MOD;
    }*/
    update(1,L-n, R-n, 0, maxn-1);
    int su=query(1, 0, maxn-1, 0, maxn-1);
    return su;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 18776 KB Output is correct
2 Correct 7 ms 19176 KB Output is correct
3 Correct 6 ms 18776 KB Output is correct
4 Correct 6 ms 18776 KB Output is correct
5 Correct 8 ms 19032 KB Output is correct
6 Correct 8 ms 19020 KB Output is correct
7 Correct 6 ms 18776 KB Output is correct
8 Correct 7 ms 18776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 18776 KB Output is correct
2 Correct 6 ms 18776 KB Output is correct
3 Correct 6 ms 18776 KB Output is correct
4 Correct 6 ms 18776 KB Output is correct
5 Correct 6 ms 19000 KB Output is correct
6 Correct 6 ms 19032 KB Output is correct
7 Correct 7 ms 19068 KB Output is correct
8 Correct 6 ms 19032 KB Output is correct
9 Correct 7 ms 18864 KB Output is correct
10 Correct 7 ms 19032 KB Output is correct
11 Correct 7 ms 19008 KB Output is correct
12 Correct 6 ms 19032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 18776 KB Output is correct
2 Correct 7 ms 19176 KB Output is correct
3 Correct 6 ms 18776 KB Output is correct
4 Correct 6 ms 18776 KB Output is correct
5 Correct 8 ms 19032 KB Output is correct
6 Correct 8 ms 19020 KB Output is correct
7 Correct 6 ms 18776 KB Output is correct
8 Correct 7 ms 18776 KB Output is correct
9 Correct 5 ms 18776 KB Output is correct
10 Correct 6 ms 18776 KB Output is correct
11 Correct 6 ms 18776 KB Output is correct
12 Correct 6 ms 18776 KB Output is correct
13 Correct 6 ms 19000 KB Output is correct
14 Correct 6 ms 19032 KB Output is correct
15 Correct 7 ms 19068 KB Output is correct
16 Correct 6 ms 19032 KB Output is correct
17 Correct 7 ms 18864 KB Output is correct
18 Correct 7 ms 19032 KB Output is correct
19 Correct 7 ms 19008 KB Output is correct
20 Correct 6 ms 19032 KB Output is correct
21 Correct 6 ms 19032 KB Output is correct
22 Correct 6 ms 19020 KB Output is correct
23 Correct 6 ms 19016 KB Output is correct
24 Correct 6 ms 19032 KB Output is correct
25 Correct 6 ms 19032 KB Output is correct
26 Correct 6 ms 19032 KB Output is correct
27 Correct 6 ms 19032 KB Output is correct
28 Correct 6 ms 19032 KB Output is correct
29 Correct 6 ms 18776 KB Output is correct
30 Correct 6 ms 18776 KB Output is correct
31 Correct 6 ms 19032 KB Output is correct
32 Correct 6 ms 19032 KB Output is correct
33 Correct 6 ms 19032 KB Output is correct
34 Correct 6 ms 19144 KB Output is correct
35 Correct 6 ms 18776 KB Output is correct
36 Correct 6 ms 19032 KB Output is correct
37 Correct 6 ms 19032 KB Output is correct
38 Correct 7 ms 19032 KB Output is correct
39 Correct 6 ms 19032 KB Output is correct
40 Correct 6 ms 18776 KB Output is correct
41 Correct 6 ms 18972 KB Output is correct
42 Correct 6 ms 18776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 467 ms 23360 KB Output is correct
2 Correct 642 ms 28092 KB Output is correct
3 Correct 633 ms 28188 KB Output is correct
4 Correct 614 ms 28076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 467 ms 23360 KB Output is correct
2 Correct 642 ms 28092 KB Output is correct
3 Correct 633 ms 28188 KB Output is correct
4 Correct 614 ms 28076 KB Output is correct
5 Correct 549 ms 23416 KB Output is correct
6 Correct 698 ms 28096 KB Output is correct
7 Correct 714 ms 28096 KB Output is correct
8 Correct 608 ms 28172 KB Output is correct
9 Correct 297 ms 19188 KB Output is correct
10 Correct 605 ms 19488 KB Output is correct
11 Correct 626 ms 19484 KB Output is correct
12 Correct 613 ms 19480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 18776 KB Output is correct
2 Correct 6 ms 18776 KB Output is correct
3 Correct 6 ms 18776 KB Output is correct
4 Correct 6 ms 18776 KB Output is correct
5 Correct 6 ms 19000 KB Output is correct
6 Correct 6 ms 19032 KB Output is correct
7 Correct 7 ms 19068 KB Output is correct
8 Correct 6 ms 19032 KB Output is correct
9 Correct 7 ms 18864 KB Output is correct
10 Correct 7 ms 19032 KB Output is correct
11 Correct 7 ms 19008 KB Output is correct
12 Correct 6 ms 19032 KB Output is correct
13 Correct 467 ms 23360 KB Output is correct
14 Correct 642 ms 28092 KB Output is correct
15 Correct 633 ms 28188 KB Output is correct
16 Correct 614 ms 28076 KB Output is correct
17 Correct 549 ms 23416 KB Output is correct
18 Correct 698 ms 28096 KB Output is correct
19 Correct 714 ms 28096 KB Output is correct
20 Correct 608 ms 28172 KB Output is correct
21 Correct 297 ms 19188 KB Output is correct
22 Correct 605 ms 19488 KB Output is correct
23 Correct 626 ms 19484 KB Output is correct
24 Correct 613 ms 19480 KB Output is correct
25 Correct 754 ms 32744 KB Output is correct
26 Correct 749 ms 32824 KB Output is correct
27 Correct 756 ms 33008 KB Output is correct
28 Correct 563 ms 33108 KB Output is correct
29 Correct 727 ms 48176 KB Output is correct
30 Correct 671 ms 48164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 18776 KB Output is correct
2 Correct 7 ms 19176 KB Output is correct
3 Correct 6 ms 18776 KB Output is correct
4 Correct 6 ms 18776 KB Output is correct
5 Correct 8 ms 19032 KB Output is correct
6 Correct 8 ms 19020 KB Output is correct
7 Correct 6 ms 18776 KB Output is correct
8 Correct 7 ms 18776 KB Output is correct
9 Correct 5 ms 18776 KB Output is correct
10 Correct 6 ms 18776 KB Output is correct
11 Correct 6 ms 18776 KB Output is correct
12 Correct 6 ms 18776 KB Output is correct
13 Correct 6 ms 19000 KB Output is correct
14 Correct 6 ms 19032 KB Output is correct
15 Correct 7 ms 19068 KB Output is correct
16 Correct 6 ms 19032 KB Output is correct
17 Correct 7 ms 18864 KB Output is correct
18 Correct 7 ms 19032 KB Output is correct
19 Correct 7 ms 19008 KB Output is correct
20 Correct 6 ms 19032 KB Output is correct
21 Correct 6 ms 19032 KB Output is correct
22 Correct 6 ms 19020 KB Output is correct
23 Correct 6 ms 19016 KB Output is correct
24 Correct 6 ms 19032 KB Output is correct
25 Correct 6 ms 19032 KB Output is correct
26 Correct 6 ms 19032 KB Output is correct
27 Correct 6 ms 19032 KB Output is correct
28 Correct 6 ms 19032 KB Output is correct
29 Correct 6 ms 18776 KB Output is correct
30 Correct 6 ms 18776 KB Output is correct
31 Correct 6 ms 19032 KB Output is correct
32 Correct 6 ms 19032 KB Output is correct
33 Correct 6 ms 19032 KB Output is correct
34 Correct 6 ms 19144 KB Output is correct
35 Correct 6 ms 18776 KB Output is correct
36 Correct 6 ms 19032 KB Output is correct
37 Correct 6 ms 19032 KB Output is correct
38 Correct 7 ms 19032 KB Output is correct
39 Correct 6 ms 19032 KB Output is correct
40 Correct 6 ms 18776 KB Output is correct
41 Correct 6 ms 18972 KB Output is correct
42 Correct 6 ms 18776 KB Output is correct
43 Correct 426 ms 19328 KB Output is correct
44 Correct 592 ms 19384 KB Output is correct
45 Correct 636 ms 19376 KB Output is correct
46 Correct 554 ms 19648 KB Output is correct
47 Correct 634 ms 19652 KB Output is correct
48 Correct 599 ms 19644 KB Output is correct
49 Correct 655 ms 19644 KB Output is correct
50 Correct 578 ms 19648 KB Output is correct
51 Correct 577 ms 19356 KB Output is correct
52 Correct 640 ms 19360 KB Output is correct
53 Correct 565 ms 20156 KB Output is correct
54 Correct 603 ms 19616 KB Output is correct
55 Correct 614 ms 19540 KB Output is correct
56 Correct 624 ms 19408 KB Output is correct
57 Correct 623 ms 19288 KB Output is correct
58 Correct 603 ms 20460 KB Output is correct
59 Correct 614 ms 20584 KB Output is correct
60 Correct 581 ms 20584 KB Output is correct
61 Correct 597 ms 19572 KB Output is correct
62 Correct 588 ms 19308 KB Output is correct
63 Correct 608 ms 19304 KB Output is correct
64 Correct 590 ms 19324 KB Output is correct
65 Correct 293 ms 19196 KB Output is correct
66 Correct 615 ms 19480 KB Output is correct
67 Correct 600 ms 19484 KB Output is correct
68 Correct 605 ms 19480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 18776 KB Output is correct
2 Correct 7 ms 19176 KB Output is correct
3 Correct 6 ms 18776 KB Output is correct
4 Correct 6 ms 18776 KB Output is correct
5 Correct 8 ms 19032 KB Output is correct
6 Correct 8 ms 19020 KB Output is correct
7 Correct 6 ms 18776 KB Output is correct
8 Correct 7 ms 18776 KB Output is correct
9 Correct 5 ms 18776 KB Output is correct
10 Correct 6 ms 18776 KB Output is correct
11 Correct 6 ms 18776 KB Output is correct
12 Correct 6 ms 18776 KB Output is correct
13 Correct 6 ms 19000 KB Output is correct
14 Correct 6 ms 19032 KB Output is correct
15 Correct 7 ms 19068 KB Output is correct
16 Correct 6 ms 19032 KB Output is correct
17 Correct 7 ms 18864 KB Output is correct
18 Correct 7 ms 19032 KB Output is correct
19 Correct 7 ms 19008 KB Output is correct
20 Correct 6 ms 19032 KB Output is correct
21 Correct 6 ms 19032 KB Output is correct
22 Correct 6 ms 19020 KB Output is correct
23 Correct 6 ms 19016 KB Output is correct
24 Correct 6 ms 19032 KB Output is correct
25 Correct 6 ms 19032 KB Output is correct
26 Correct 6 ms 19032 KB Output is correct
27 Correct 6 ms 19032 KB Output is correct
28 Correct 6 ms 19032 KB Output is correct
29 Correct 6 ms 18776 KB Output is correct
30 Correct 6 ms 18776 KB Output is correct
31 Correct 6 ms 19032 KB Output is correct
32 Correct 6 ms 19032 KB Output is correct
33 Correct 6 ms 19032 KB Output is correct
34 Correct 6 ms 19144 KB Output is correct
35 Correct 6 ms 18776 KB Output is correct
36 Correct 6 ms 19032 KB Output is correct
37 Correct 6 ms 19032 KB Output is correct
38 Correct 7 ms 19032 KB Output is correct
39 Correct 6 ms 19032 KB Output is correct
40 Correct 6 ms 18776 KB Output is correct
41 Correct 6 ms 18972 KB Output is correct
42 Correct 6 ms 18776 KB Output is correct
43 Correct 467 ms 23360 KB Output is correct
44 Correct 642 ms 28092 KB Output is correct
45 Correct 633 ms 28188 KB Output is correct
46 Correct 614 ms 28076 KB Output is correct
47 Correct 549 ms 23416 KB Output is correct
48 Correct 698 ms 28096 KB Output is correct
49 Correct 714 ms 28096 KB Output is correct
50 Correct 608 ms 28172 KB Output is correct
51 Correct 297 ms 19188 KB Output is correct
52 Correct 605 ms 19488 KB Output is correct
53 Correct 626 ms 19484 KB Output is correct
54 Correct 613 ms 19480 KB Output is correct
55 Correct 754 ms 32744 KB Output is correct
56 Correct 749 ms 32824 KB Output is correct
57 Correct 756 ms 33008 KB Output is correct
58 Correct 563 ms 33108 KB Output is correct
59 Correct 727 ms 48176 KB Output is correct
60 Correct 671 ms 48164 KB Output is correct
61 Correct 426 ms 19328 KB Output is correct
62 Correct 592 ms 19384 KB Output is correct
63 Correct 636 ms 19376 KB Output is correct
64 Correct 554 ms 19648 KB Output is correct
65 Correct 634 ms 19652 KB Output is correct
66 Correct 599 ms 19644 KB Output is correct
67 Correct 655 ms 19644 KB Output is correct
68 Correct 578 ms 19648 KB Output is correct
69 Correct 577 ms 19356 KB Output is correct
70 Correct 640 ms 19360 KB Output is correct
71 Correct 565 ms 20156 KB Output is correct
72 Correct 603 ms 19616 KB Output is correct
73 Correct 614 ms 19540 KB Output is correct
74 Correct 624 ms 19408 KB Output is correct
75 Correct 623 ms 19288 KB Output is correct
76 Correct 603 ms 20460 KB Output is correct
77 Correct 614 ms 20584 KB Output is correct
78 Correct 581 ms 20584 KB Output is correct
79 Correct 597 ms 19572 KB Output is correct
80 Correct 588 ms 19308 KB Output is correct
81 Correct 608 ms 19304 KB Output is correct
82 Correct 590 ms 19324 KB Output is correct
83 Correct 293 ms 19196 KB Output is correct
84 Correct 615 ms 19480 KB Output is correct
85 Correct 600 ms 19484 KB Output is correct
86 Correct 605 ms 19480 KB Output is correct
87 Correct 9 ms 18928 KB Output is correct
88 Correct 455 ms 31644 KB Output is correct
89 Correct 712 ms 28132 KB Output is correct
90 Correct 701 ms 28008 KB Output is correct
91 Correct 687 ms 33596 KB Output is correct
92 Correct 773 ms 33376 KB Output is correct
93 Correct 733 ms 33408 KB Output is correct
94 Correct 753 ms 33496 KB Output is correct
95 Correct 695 ms 33592 KB Output is correct
96 Correct 688 ms 26088 KB Output is correct
97 Correct 723 ms 26096 KB Output is correct
98 Correct 640 ms 41768 KB Output is correct
99 Correct 760 ms 32992 KB Output is correct
100 Correct 779 ms 30056 KB Output is correct
101 Correct 699 ms 28456 KB Output is correct
102 Correct 721 ms 26052 KB Output is correct
103 Correct 776 ms 48048 KB Output is correct
104 Correct 773 ms 50356 KB Output is correct
105 Correct 699 ms 50368 KB Output is correct
106 Correct 716 ms 31568 KB Output is correct
107 Correct 699 ms 26412 KB Output is correct
108 Correct 704 ms 26980 KB Output is correct
109 Correct 721 ms 26324 KB Output is correct