Submission #1069004

# Submission time Handle Problem Language Result Execution time Memory
1069004 2024-08-21T14:22:58 Z beaconmc Digital Circuit (IOI22_circuit) C++17
100 / 100
863 ms 61776 KB
#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define FOR(i,x,y) for(ll i=x; i<y; i++)
#define FORNEG(i,x,y) for(ll i=x; i>y; i--)

const ll maxn = 200005;

basic_string<ll> edges[maxn];
ll sub[maxn];
ll realsub[maxn];
ll par[maxn];
ll prod[maxn];
ll prefs[maxn];
ll pos[maxn];
vector<ll> pref[maxn], suff[maxn];

void dfs(ll a){
    prod[a] = 1;
    for (auto&i : edges[a]){
        dfs(i);

        prod[a] *= prod[i];
        prod[a] %= 1000002022;
        pref[a].push_back(prod[a]);
    }
    reverse(edges[a].begin(), edges[a].end());
    ll temp = 1;
    for (auto&i : edges[a]){

        temp *= prod[i];
        temp %= 1000002022;
        suff[a].push_back(temp);
    }
    reverse(suff[a].begin(), suff[a].end());
    reverse(edges[a].begin(), edges[a].end());
    if ( edges[a].size() != 0) prod[a] *= edges[a].size();
    prod[a] %= 1000002022;
}

ll cache[maxn];

ll dp(ll a){

    if (cache[a] != -1) return cache[a];
    if (a==0) return 1;


    cache[a] = dp(par[a]);

    if (pos[a] != 1) cache[a] *= pref[par[a]][pos[a]-2];
    cache[a] %= 1000002022;
    if (pos[a] != suff[par[a]].size()) cache[a] *= suff[par[a]][pos[a]];
    cache[a] %= 1000002022;


    return cache[a] %= 1000002022;
}
ll states[maxn];
ll vals[maxn];

ll ans = 0;
ll n;

ll tree[maxn*4];
ll lazy[maxn*4];


ll calc(ll k, ll x, ll y){
    if (lazy[k]%2==1){
        return prefs[y] - prefs[x] + vals[x] - tree[k];
    }else{
        return tree[k];
    }
}
void push(ll k, ll x, ll y){

    lazy[k*2] += lazy[k];
    lazy[k*2+1] += lazy[k];

    tree[k] = prefs[y] - prefs[x] + vals[x] - tree[k];

    lazy[k] = 0;
}
void update(ll a, ll b, ll k=1, ll x =0, ll y = n-1){
    if (y<a || b<x) return;
    if (a<=x && y<=b){
        lazy[k]++;
        return;
    }

    push(k,x,y);
    ll mid = (x+y)/2;

    update(a,b,k*2,x,mid);
    update(a,b,k*2+1,mid+1,y);

    tree[k] = calc(k*2,x,mid) + calc(k*2+1, mid+1, y);
}
void sets(ll a, ll b, ll val, ll k=1, ll x =0, ll y = n-1){
    if (y<a || b<x) return;
    if (a<=x && y<=b){
        tree[k] = val;
        return;
    }
    ll mid = (x+y)/2;

    sets(a,b,val,k*2,x,mid);
    sets(a,b,val,k*2+1,mid+1,y);

    tree[k] = calc(k*2,x,mid) + calc(k*2+1, mid+1, y);
}

ll query(ll a, ll b, ll k=1, ll x =0, ll y = n-1){
 
    if (y<a || b<x) return 0;
    if (a<=x && y<=b){

        return calc(k,x,y);
    }
    push(k,x,y);
    ll mid = (x+y)/2;
    return query(a,b,k*2,x,mid) + query(a,b,k*2+1,mid+1,y);
}


void init(int N, int M, std::vector<int> P, std::vector<int> A) {
    n = N+M;
    FOR(i,0,maxn) cache[i] = -1, prefs[i] = 0;
    FOR(i,0,4*maxn) tree[i] = 0, lazy[i] = 0;
    par[0] = -1;
    FOR(i,1,N+M){
        edges[P[i]].push_back(i);
        pos[i] = edges[P[i]].size();
        par[i] = P[i];
    }
    dfs(0);

    FOR(i,0,M){
        vals[N+i] = dp(N+i);
        sets(N+i, N+i, vals[N+i]);
    }


    FOR(i,0,N) prefs[i] = 0;
    FOR(i,N,N+M){
        prefs[i] = prefs[i-1] + vals[i];
    }
    FOR(i,0,M){
        if (A[i] == 0) update(N+i,N+i);
    }

}



int count_ways(int L, int R) {

    update(L, R);
    return (query(0,n-1)+1000002022)%1000002022;
}

Compilation message

circuit.cpp: In function 'll dp(ll)':
circuit.cpp:55:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |     if (pos[a] != suff[par[a]].size()) cache[a] *= suff[par[a]][pos[a]];
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 38488 KB Output is correct
2 Correct 6 ms 38488 KB Output is correct
3 Correct 6 ms 38652 KB Output is correct
4 Correct 5 ms 40536 KB Output is correct
5 Correct 6 ms 40536 KB Output is correct
6 Correct 6 ms 40536 KB Output is correct
7 Correct 6 ms 40536 KB Output is correct
8 Correct 6 ms 40536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 40536 KB Output is correct
2 Correct 5 ms 40536 KB Output is correct
3 Correct 6 ms 40660 KB Output is correct
4 Correct 5 ms 40536 KB Output is correct
5 Correct 5 ms 40536 KB Output is correct
6 Correct 7 ms 40668 KB Output is correct
7 Correct 6 ms 40536 KB Output is correct
8 Correct 6 ms 40536 KB Output is correct
9 Correct 6 ms 40536 KB Output is correct
10 Correct 6 ms 40792 KB Output is correct
11 Correct 5 ms 40792 KB Output is correct
12 Correct 5 ms 40536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 38488 KB Output is correct
2 Correct 6 ms 38488 KB Output is correct
3 Correct 6 ms 38652 KB Output is correct
4 Correct 5 ms 40536 KB Output is correct
5 Correct 6 ms 40536 KB Output is correct
6 Correct 6 ms 40536 KB Output is correct
7 Correct 6 ms 40536 KB Output is correct
8 Correct 6 ms 40536 KB Output is correct
9 Correct 5 ms 40536 KB Output is correct
10 Correct 5 ms 40536 KB Output is correct
11 Correct 6 ms 40660 KB Output is correct
12 Correct 5 ms 40536 KB Output is correct
13 Correct 5 ms 40536 KB Output is correct
14 Correct 7 ms 40668 KB Output is correct
15 Correct 6 ms 40536 KB Output is correct
16 Correct 6 ms 40536 KB Output is correct
17 Correct 6 ms 40536 KB Output is correct
18 Correct 6 ms 40792 KB Output is correct
19 Correct 5 ms 40792 KB Output is correct
20 Correct 5 ms 40536 KB Output is correct
21 Correct 6 ms 40536 KB Output is correct
22 Correct 6 ms 40536 KB Output is correct
23 Correct 6 ms 40536 KB Output is correct
24 Correct 7 ms 40536 KB Output is correct
25 Correct 6 ms 40536 KB Output is correct
26 Correct 6 ms 40536 KB Output is correct
27 Correct 6 ms 40536 KB Output is correct
28 Correct 6 ms 40740 KB Output is correct
29 Correct 5 ms 40536 KB Output is correct
30 Correct 6 ms 40536 KB Output is correct
31 Correct 6 ms 40704 KB Output is correct
32 Correct 6 ms 40536 KB Output is correct
33 Correct 6 ms 40536 KB Output is correct
34 Correct 6 ms 40664 KB Output is correct
35 Correct 5 ms 40536 KB Output is correct
36 Correct 6 ms 40664 KB Output is correct
37 Correct 8 ms 40668 KB Output is correct
38 Correct 6 ms 40792 KB Output is correct
39 Correct 5 ms 40536 KB Output is correct
40 Correct 7 ms 40536 KB Output is correct
41 Correct 6 ms 40536 KB Output is correct
42 Correct 7 ms 40536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 470 ms 44376 KB Output is correct
2 Correct 725 ms 48208 KB Output is correct
3 Correct 675 ms 48224 KB Output is correct
4 Correct 701 ms 48208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 470 ms 44376 KB Output is correct
2 Correct 725 ms 48208 KB Output is correct
3 Correct 675 ms 48224 KB Output is correct
4 Correct 701 ms 48208 KB Output is correct
5 Correct 534 ms 44376 KB Output is correct
6 Correct 725 ms 48208 KB Output is correct
7 Correct 735 ms 48268 KB Output is correct
8 Correct 694 ms 48124 KB Output is correct
9 Correct 321 ms 40792 KB Output is correct
10 Correct 684 ms 41048 KB Output is correct
11 Correct 688 ms 41048 KB Output is correct
12 Correct 669 ms 41048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 40536 KB Output is correct
2 Correct 5 ms 40536 KB Output is correct
3 Correct 6 ms 40660 KB Output is correct
4 Correct 5 ms 40536 KB Output is correct
5 Correct 5 ms 40536 KB Output is correct
6 Correct 7 ms 40668 KB Output is correct
7 Correct 6 ms 40536 KB Output is correct
8 Correct 6 ms 40536 KB Output is correct
9 Correct 6 ms 40536 KB Output is correct
10 Correct 6 ms 40792 KB Output is correct
11 Correct 5 ms 40792 KB Output is correct
12 Correct 5 ms 40536 KB Output is correct
13 Correct 470 ms 44376 KB Output is correct
14 Correct 725 ms 48208 KB Output is correct
15 Correct 675 ms 48224 KB Output is correct
16 Correct 701 ms 48208 KB Output is correct
17 Correct 534 ms 44376 KB Output is correct
18 Correct 725 ms 48208 KB Output is correct
19 Correct 735 ms 48268 KB Output is correct
20 Correct 694 ms 48124 KB Output is correct
21 Correct 321 ms 40792 KB Output is correct
22 Correct 684 ms 41048 KB Output is correct
23 Correct 688 ms 41048 KB Output is correct
24 Correct 669 ms 41048 KB Output is correct
25 Correct 799 ms 52012 KB Output is correct
26 Correct 828 ms 52212 KB Output is correct
27 Correct 779 ms 52304 KB Output is correct
28 Correct 661 ms 52316 KB Output is correct
29 Correct 758 ms 61776 KB Output is correct
30 Correct 798 ms 61776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 38488 KB Output is correct
2 Correct 6 ms 38488 KB Output is correct
3 Correct 6 ms 38652 KB Output is correct
4 Correct 5 ms 40536 KB Output is correct
5 Correct 6 ms 40536 KB Output is correct
6 Correct 6 ms 40536 KB Output is correct
7 Correct 6 ms 40536 KB Output is correct
8 Correct 6 ms 40536 KB Output is correct
9 Correct 5 ms 40536 KB Output is correct
10 Correct 5 ms 40536 KB Output is correct
11 Correct 6 ms 40660 KB Output is correct
12 Correct 5 ms 40536 KB Output is correct
13 Correct 5 ms 40536 KB Output is correct
14 Correct 7 ms 40668 KB Output is correct
15 Correct 6 ms 40536 KB Output is correct
16 Correct 6 ms 40536 KB Output is correct
17 Correct 6 ms 40536 KB Output is correct
18 Correct 6 ms 40792 KB Output is correct
19 Correct 5 ms 40792 KB Output is correct
20 Correct 5 ms 40536 KB Output is correct
21 Correct 6 ms 40536 KB Output is correct
22 Correct 6 ms 40536 KB Output is correct
23 Correct 6 ms 40536 KB Output is correct
24 Correct 7 ms 40536 KB Output is correct
25 Correct 6 ms 40536 KB Output is correct
26 Correct 6 ms 40536 KB Output is correct
27 Correct 6 ms 40536 KB Output is correct
28 Correct 6 ms 40740 KB Output is correct
29 Correct 5 ms 40536 KB Output is correct
30 Correct 6 ms 40536 KB Output is correct
31 Correct 6 ms 40704 KB Output is correct
32 Correct 6 ms 40536 KB Output is correct
33 Correct 6 ms 40536 KB Output is correct
34 Correct 6 ms 40664 KB Output is correct
35 Correct 5 ms 40536 KB Output is correct
36 Correct 6 ms 40664 KB Output is correct
37 Correct 8 ms 40668 KB Output is correct
38 Correct 6 ms 40792 KB Output is correct
39 Correct 5 ms 40536 KB Output is correct
40 Correct 7 ms 40536 KB Output is correct
41 Correct 6 ms 40536 KB Output is correct
42 Correct 7 ms 40536 KB Output is correct
43 Correct 484 ms 40792 KB Output is correct
44 Correct 642 ms 41048 KB Output is correct
45 Correct 647 ms 40792 KB Output is correct
46 Correct 672 ms 41048 KB Output is correct
47 Correct 666 ms 41048 KB Output is correct
48 Correct 677 ms 41048 KB Output is correct
49 Correct 635 ms 41048 KB Output is correct
50 Correct 610 ms 41048 KB Output is correct
51 Correct 658 ms 40928 KB Output is correct
52 Correct 663 ms 40924 KB Output is correct
53 Correct 627 ms 41304 KB Output is correct
54 Correct 609 ms 39256 KB Output is correct
55 Correct 574 ms 41048 KB Output is correct
56 Correct 490 ms 40792 KB Output is correct
57 Correct 569 ms 40848 KB Output is correct
58 Correct 520 ms 41560 KB Output is correct
59 Correct 570 ms 41668 KB Output is correct
60 Correct 470 ms 41656 KB Output is correct
61 Correct 555 ms 41048 KB Output is correct
62 Correct 561 ms 40792 KB Output is correct
63 Correct 540 ms 40792 KB Output is correct
64 Correct 500 ms 40792 KB Output is correct
65 Correct 292 ms 40696 KB Output is correct
66 Correct 579 ms 41048 KB Output is correct
67 Correct 551 ms 40988 KB Output is correct
68 Correct 519 ms 41048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 38488 KB Output is correct
2 Correct 6 ms 38488 KB Output is correct
3 Correct 6 ms 38652 KB Output is correct
4 Correct 5 ms 40536 KB Output is correct
5 Correct 6 ms 40536 KB Output is correct
6 Correct 6 ms 40536 KB Output is correct
7 Correct 6 ms 40536 KB Output is correct
8 Correct 6 ms 40536 KB Output is correct
9 Correct 5 ms 40536 KB Output is correct
10 Correct 5 ms 40536 KB Output is correct
11 Correct 6 ms 40660 KB Output is correct
12 Correct 5 ms 40536 KB Output is correct
13 Correct 5 ms 40536 KB Output is correct
14 Correct 7 ms 40668 KB Output is correct
15 Correct 6 ms 40536 KB Output is correct
16 Correct 6 ms 40536 KB Output is correct
17 Correct 6 ms 40536 KB Output is correct
18 Correct 6 ms 40792 KB Output is correct
19 Correct 5 ms 40792 KB Output is correct
20 Correct 5 ms 40536 KB Output is correct
21 Correct 6 ms 40536 KB Output is correct
22 Correct 6 ms 40536 KB Output is correct
23 Correct 6 ms 40536 KB Output is correct
24 Correct 7 ms 40536 KB Output is correct
25 Correct 6 ms 40536 KB Output is correct
26 Correct 6 ms 40536 KB Output is correct
27 Correct 6 ms 40536 KB Output is correct
28 Correct 6 ms 40740 KB Output is correct
29 Correct 5 ms 40536 KB Output is correct
30 Correct 6 ms 40536 KB Output is correct
31 Correct 6 ms 40704 KB Output is correct
32 Correct 6 ms 40536 KB Output is correct
33 Correct 6 ms 40536 KB Output is correct
34 Correct 6 ms 40664 KB Output is correct
35 Correct 5 ms 40536 KB Output is correct
36 Correct 6 ms 40664 KB Output is correct
37 Correct 8 ms 40668 KB Output is correct
38 Correct 6 ms 40792 KB Output is correct
39 Correct 5 ms 40536 KB Output is correct
40 Correct 7 ms 40536 KB Output is correct
41 Correct 6 ms 40536 KB Output is correct
42 Correct 7 ms 40536 KB Output is correct
43 Correct 470 ms 44376 KB Output is correct
44 Correct 725 ms 48208 KB Output is correct
45 Correct 675 ms 48224 KB Output is correct
46 Correct 701 ms 48208 KB Output is correct
47 Correct 534 ms 44376 KB Output is correct
48 Correct 725 ms 48208 KB Output is correct
49 Correct 735 ms 48268 KB Output is correct
50 Correct 694 ms 48124 KB Output is correct
51 Correct 321 ms 40792 KB Output is correct
52 Correct 684 ms 41048 KB Output is correct
53 Correct 688 ms 41048 KB Output is correct
54 Correct 669 ms 41048 KB Output is correct
55 Correct 799 ms 52012 KB Output is correct
56 Correct 828 ms 52212 KB Output is correct
57 Correct 779 ms 52304 KB Output is correct
58 Correct 661 ms 52316 KB Output is correct
59 Correct 758 ms 61776 KB Output is correct
60 Correct 798 ms 61776 KB Output is correct
61 Correct 484 ms 40792 KB Output is correct
62 Correct 642 ms 41048 KB Output is correct
63 Correct 647 ms 40792 KB Output is correct
64 Correct 672 ms 41048 KB Output is correct
65 Correct 666 ms 41048 KB Output is correct
66 Correct 677 ms 41048 KB Output is correct
67 Correct 635 ms 41048 KB Output is correct
68 Correct 610 ms 41048 KB Output is correct
69 Correct 658 ms 40928 KB Output is correct
70 Correct 663 ms 40924 KB Output is correct
71 Correct 627 ms 41304 KB Output is correct
72 Correct 609 ms 39256 KB Output is correct
73 Correct 574 ms 41048 KB Output is correct
74 Correct 490 ms 40792 KB Output is correct
75 Correct 569 ms 40848 KB Output is correct
76 Correct 520 ms 41560 KB Output is correct
77 Correct 570 ms 41668 KB Output is correct
78 Correct 470 ms 41656 KB Output is correct
79 Correct 555 ms 41048 KB Output is correct
80 Correct 561 ms 40792 KB Output is correct
81 Correct 540 ms 40792 KB Output is correct
82 Correct 500 ms 40792 KB Output is correct
83 Correct 292 ms 40696 KB Output is correct
84 Correct 579 ms 41048 KB Output is correct
85 Correct 551 ms 40988 KB Output is correct
86 Correct 519 ms 41048 KB Output is correct
87 Correct 6 ms 40536 KB Output is correct
88 Correct 460 ms 51280 KB Output is correct
89 Correct 748 ms 48464 KB Output is correct
90 Correct 719 ms 48252 KB Output is correct
91 Correct 766 ms 52560 KB Output is correct
92 Correct 816 ms 52616 KB Output is correct
93 Correct 863 ms 52648 KB Output is correct
94 Correct 835 ms 52560 KB Output is correct
95 Correct 761 ms 52560 KB Output is correct
96 Correct 662 ms 45136 KB Output is correct
97 Correct 794 ms 45136 KB Output is correct
98 Correct 756 ms 56920 KB Output is correct
99 Correct 821 ms 52232 KB Output is correct
100 Correct 775 ms 49488 KB Output is correct
101 Correct 770 ms 48264 KB Output is correct
102 Correct 791 ms 45132 KB Output is correct
103 Correct 799 ms 61524 KB Output is correct
104 Correct 802 ms 60876 KB Output is correct
105 Correct 724 ms 61004 KB Output is correct
106 Correct 744 ms 50000 KB Output is correct
107 Correct 815 ms 46752 KB Output is correct
108 Correct 748 ms 46672 KB Output is correct
109 Correct 687 ms 45564 KB Output is correct