Submission #720974

# Submission time Handle Problem Language Result Execution time Memory
720974 2023-04-10T00:58:10 Z lam Digital Circuit (IOI22_circuit) C++17
100 / 100
1172 ms 39400 KB
#include "circuit.h"

#include <vector>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
typedef pair<ll,ll> ii;
#define ff first
#define ss second
const int maxn = 2e5 + 10;
const ll mod =  1000002022LL;
int n,m;
int a[maxn];
vector <int> adj[maxn];
ll s[maxn],dp[maxn],sdp[maxn];
void dfs(int x)
{
    s[x] = 1LL;
    if (adj[x].empty()) return;
    for (int i:adj[x])
    {
        dfs(i);
        s[x]=s[x]*s[i]%mod;
    }
    s[x] = s[x]*(ll)adj[x].size()%mod;
}
void dfs2(int x, ll val)
{
//    cerr<<x<<" :: "<<val<<" & "<<s[x]<<endl;
    if (x>=n)
    {
        dp[x-n+1] = val;
        return;
    }
    vector <ll> pre,suf;
    for (int i:adj[x])
    {
        pre.push_back(s[i]);
        suf.push_back(s[i]);
    }
    for (int i=1; i<pre.size(); i++) pre[i]=pre[i]*pre[i-1]%mod;
    for (int i=suf.size()-2; i>=0; i--) suf[i]=suf[i]*suf[i+1]%mod;
    int cnt=0;
//    for (int i:pre) cerr<<i<<' '; cerr<<endl;
//    for (int i:suf) cerr<<i<<' '; cerr<<endl;
//    cerr<<"------------"<<endl;
    for (int i:adj[x])
    {
        ll val2 = val;
        if (cnt>0) val2=val2*pre[cnt-1]%mod;
        if (cnt<suf.size()-1) val2=val2*suf[cnt+1]%mod;
        cnt++;
        dfs2(i,val2);
    }

}
ll f[4*maxn]; bool rev[4*maxn];
void pushrev(int x, int lx, int rx)
{
//    cerr<<"rev "<<lx<<' '<<rx<<endl;
//    cerr<<f[x]<<" ";
    f[x] = (sdp[rx]-sdp[lx-1]+mod-f[x]+mod)%mod;
//    cerr<<"--> "<<f[x]<<endl;
    rev[x]^=1;
}
void down(int x, int lx, int rx)
{
    if (lx==rx) return;
    int mid=(lx+rx)/2;
    if (rev[x])
    {
        pushrev(2*x,lx,mid);
        pushrev(2*x+1,mid+1,rx);
        rev[x]=0;
    }
}
void add(int x, int lx, int rx, int l, int r)
{
    if (lx>r||rx<l) return;
    if (lx>=l&&rx<=r)
    {
        pushrev(x,lx,rx);
        return;
    }
    int mid=(lx+rx)/2;
    down(x,lx,rx);
    add(2*x,lx,mid,l,r);
    add(2*x+1,mid+1,rx,l,r);
    f[x]=(f[2*x]+f[2*x+1])%mod;
}
void build(int x, int lx, int rx)
{
    rev[x]=0;
    if (lx==rx)
    {
        f[x]=0LL;
        if (a[lx]) f[x] = dp[lx];
//        cerr<<lx<<" := "<<f[x]<<endl;
        return;
    }
    int mid=(lx+rx)/2;
    build(2*x,lx,mid); build(2*x+1,mid+1,rx);
    f[x]=(f[2*x]+f[2*x+1])%mod;
}
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++) adj[i].clear();
    for (int i=1; i<n+m; i++)
    {
        int u=i; int v=P[i];
        adj[v].push_back(u);
    }

    for (int i=n; i<n+m; i++) a[i-n+1]=A[i-n];
    dfs(0); dfs2(0,1LL);
//    for (int i=1; i<=m; i++) cerr<<a[i]<<' '; cerr<<endl;
//    for (int i=1; i<=m; i++) cerr<<dp[i]<<' '; cerr<<endl;
    sdp[0]=0LL;
    for (int i=1; i<=m; i++) sdp[i]=(dp[i]+sdp[i-1])%mod;
    build(1,1,m);
}

int count_ways(int L, int R) {
    L-=n; R-=n;
    L++; R++;
//    cerr<<L<<" - "<<R<<endl;
    add(1,1,m,L,R);
    int ans = (f[1])%mod;
    return ans;
}

Compilation message

circuit.cpp: In function 'void dfs2(int, long long int)':
circuit.cpp:42:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for (int i=1; i<pre.size(); i++) pre[i]=pre[i]*pre[i-1]%mod;
      |                   ~^~~~~~~~~~~
circuit.cpp:52:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |         if (cnt<suf.size()-1) val2=val2*suf[cnt+1]%mod;
      |             ~~~^~~~~~~~~~~~~
# 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 3 ms 5072 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 5120 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 4944 KB Output is correct
3 Correct 3 ms 5072 KB Output is correct
4 Correct 3 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 4 ms 5072 KB Output is correct
8 Correct 4 ms 5072 KB Output is correct
9 Correct 3 ms 5072 KB Output is correct
10 Correct 3 ms 5308 KB Output is correct
11 Correct 5 ms 5260 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 3 ms 5072 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 5120 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 4944 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 4 ms 5072 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 4 ms 5072 KB Output is correct
16 Correct 4 ms 5072 KB Output is correct
17 Correct 3 ms 5072 KB Output is correct
18 Correct 3 ms 5308 KB Output is correct
19 Correct 5 ms 5260 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 3 ms 5072 KB Output is correct
22 Correct 4 ms 5072 KB Output is correct
23 Correct 5 ms 5072 KB Output is correct
24 Correct 4 ms 5072 KB Output is correct
25 Correct 5 ms 5072 KB Output is correct
26 Correct 4 ms 5072 KB Output is correct
27 Correct 4 ms 5072 KB Output is correct
28 Correct 5 ms 5072 KB Output is correct
29 Correct 4 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 4 ms 5200 KB Output is correct
32 Correct 4 ms 5072 KB Output is correct
33 Correct 4 ms 5072 KB Output is correct
34 Correct 4 ms 5012 KB Output is correct
35 Correct 3 ms 5072 KB Output is correct
36 Correct 4 ms 5264 KB Output is correct
37 Correct 4 ms 5328 KB Output is correct
38 Correct 3 ms 5328 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 5 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 652 ms 8552 KB Output is correct
2 Correct 806 ms 12032 KB Output is correct
3 Correct 773 ms 12104 KB Output is correct
4 Correct 877 ms 12048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 652 ms 8552 KB Output is correct
2 Correct 806 ms 12032 KB Output is correct
3 Correct 773 ms 12104 KB Output is correct
4 Correct 877 ms 12048 KB Output is correct
5 Correct 798 ms 8524 KB Output is correct
6 Correct 1077 ms 12036 KB Output is correct
7 Correct 1095 ms 12036 KB Output is correct
8 Correct 1006 ms 12068 KB Output is correct
9 Correct 488 ms 5196 KB Output is correct
10 Correct 948 ms 5388 KB Output is correct
11 Correct 827 ms 5448 KB Output is correct
12 Correct 950 ms 5376 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 3 ms 5072 KB Output is correct
4 Correct 3 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 4 ms 5072 KB Output is correct
8 Correct 4 ms 5072 KB Output is correct
9 Correct 3 ms 5072 KB Output is correct
10 Correct 3 ms 5308 KB Output is correct
11 Correct 5 ms 5260 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 652 ms 8552 KB Output is correct
14 Correct 806 ms 12032 KB Output is correct
15 Correct 773 ms 12104 KB Output is correct
16 Correct 877 ms 12048 KB Output is correct
17 Correct 798 ms 8524 KB Output is correct
18 Correct 1077 ms 12036 KB Output is correct
19 Correct 1095 ms 12036 KB Output is correct
20 Correct 1006 ms 12068 KB Output is correct
21 Correct 488 ms 5196 KB Output is correct
22 Correct 948 ms 5388 KB Output is correct
23 Correct 827 ms 5448 KB Output is correct
24 Correct 950 ms 5376 KB Output is correct
25 Correct 889 ms 16132 KB Output is correct
26 Correct 1143 ms 16264 KB Output is correct
27 Correct 947 ms 16304 KB Output is correct
28 Correct 640 ms 16308 KB Output is correct
29 Correct 1000 ms 38176 KB Output is correct
30 Correct 927 ms 38216 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 3 ms 5072 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 5120 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 4944 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 4 ms 5072 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 4 ms 5072 KB Output is correct
16 Correct 4 ms 5072 KB Output is correct
17 Correct 3 ms 5072 KB Output is correct
18 Correct 3 ms 5308 KB Output is correct
19 Correct 5 ms 5260 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 3 ms 5072 KB Output is correct
22 Correct 4 ms 5072 KB Output is correct
23 Correct 5 ms 5072 KB Output is correct
24 Correct 4 ms 5072 KB Output is correct
25 Correct 5 ms 5072 KB Output is correct
26 Correct 4 ms 5072 KB Output is correct
27 Correct 4 ms 5072 KB Output is correct
28 Correct 5 ms 5072 KB Output is correct
29 Correct 4 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 4 ms 5200 KB Output is correct
32 Correct 4 ms 5072 KB Output is correct
33 Correct 4 ms 5072 KB Output is correct
34 Correct 4 ms 5012 KB Output is correct
35 Correct 3 ms 5072 KB Output is correct
36 Correct 4 ms 5264 KB Output is correct
37 Correct 4 ms 5328 KB Output is correct
38 Correct 3 ms 5328 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 5 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
43 Correct 626 ms 5328 KB Output is correct
44 Correct 818 ms 5388 KB Output is correct
45 Correct 944 ms 5296 KB Output is correct
46 Correct 676 ms 5584 KB Output is correct
47 Correct 809 ms 5516 KB Output is correct
48 Correct 778 ms 5512 KB Output is correct
49 Correct 802 ms 5520 KB Output is correct
50 Correct 737 ms 5572 KB Output is correct
51 Correct 991 ms 5552 KB Output is correct
52 Correct 676 ms 5548 KB Output is correct
53 Correct 676 ms 6284 KB Output is correct
54 Correct 907 ms 5516 KB Output is correct
55 Correct 925 ms 5380 KB Output is correct
56 Correct 853 ms 5388 KB Output is correct
57 Correct 876 ms 5328 KB Output is correct
58 Correct 821 ms 6668 KB Output is correct
59 Correct 845 ms 6868 KB Output is correct
60 Correct 856 ms 6748 KB Output is correct
61 Correct 1055 ms 5584 KB Output is correct
62 Correct 744 ms 5328 KB Output is correct
63 Correct 797 ms 5264 KB Output is correct
64 Correct 829 ms 5328 KB Output is correct
65 Correct 368 ms 5200 KB Output is correct
66 Correct 850 ms 5456 KB Output is correct
67 Correct 854 ms 5580 KB Output is correct
68 Correct 901 ms 5380 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 3 ms 5072 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 5120 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 4944 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 4 ms 5072 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 4 ms 5072 KB Output is correct
16 Correct 4 ms 5072 KB Output is correct
17 Correct 3 ms 5072 KB Output is correct
18 Correct 3 ms 5308 KB Output is correct
19 Correct 5 ms 5260 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 3 ms 5072 KB Output is correct
22 Correct 4 ms 5072 KB Output is correct
23 Correct 5 ms 5072 KB Output is correct
24 Correct 4 ms 5072 KB Output is correct
25 Correct 5 ms 5072 KB Output is correct
26 Correct 4 ms 5072 KB Output is correct
27 Correct 4 ms 5072 KB Output is correct
28 Correct 5 ms 5072 KB Output is correct
29 Correct 4 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 4 ms 5200 KB Output is correct
32 Correct 4 ms 5072 KB Output is correct
33 Correct 4 ms 5072 KB Output is correct
34 Correct 4 ms 5012 KB Output is correct
35 Correct 3 ms 5072 KB Output is correct
36 Correct 4 ms 5264 KB Output is correct
37 Correct 4 ms 5328 KB Output is correct
38 Correct 3 ms 5328 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 5 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
43 Correct 652 ms 8552 KB Output is correct
44 Correct 806 ms 12032 KB Output is correct
45 Correct 773 ms 12104 KB Output is correct
46 Correct 877 ms 12048 KB Output is correct
47 Correct 798 ms 8524 KB Output is correct
48 Correct 1077 ms 12036 KB Output is correct
49 Correct 1095 ms 12036 KB Output is correct
50 Correct 1006 ms 12068 KB Output is correct
51 Correct 488 ms 5196 KB Output is correct
52 Correct 948 ms 5388 KB Output is correct
53 Correct 827 ms 5448 KB Output is correct
54 Correct 950 ms 5376 KB Output is correct
55 Correct 889 ms 16132 KB Output is correct
56 Correct 1143 ms 16264 KB Output is correct
57 Correct 947 ms 16304 KB Output is correct
58 Correct 640 ms 16308 KB Output is correct
59 Correct 1000 ms 38176 KB Output is correct
60 Correct 927 ms 38216 KB Output is correct
61 Correct 626 ms 5328 KB Output is correct
62 Correct 818 ms 5388 KB Output is correct
63 Correct 944 ms 5296 KB Output is correct
64 Correct 676 ms 5584 KB Output is correct
65 Correct 809 ms 5516 KB Output is correct
66 Correct 778 ms 5512 KB Output is correct
67 Correct 802 ms 5520 KB Output is correct
68 Correct 737 ms 5572 KB Output is correct
69 Correct 991 ms 5552 KB Output is correct
70 Correct 676 ms 5548 KB Output is correct
71 Correct 676 ms 6284 KB Output is correct
72 Correct 907 ms 5516 KB Output is correct
73 Correct 925 ms 5380 KB Output is correct
74 Correct 853 ms 5388 KB Output is correct
75 Correct 876 ms 5328 KB Output is correct
76 Correct 821 ms 6668 KB Output is correct
77 Correct 845 ms 6868 KB Output is correct
78 Correct 856 ms 6748 KB Output is correct
79 Correct 1055 ms 5584 KB Output is correct
80 Correct 744 ms 5328 KB Output is correct
81 Correct 797 ms 5264 KB Output is correct
82 Correct 829 ms 5328 KB Output is correct
83 Correct 368 ms 5200 KB Output is correct
84 Correct 850 ms 5456 KB Output is correct
85 Correct 854 ms 5580 KB Output is correct
86 Correct 901 ms 5380 KB Output is correct
87 Correct 3 ms 4944 KB Output is correct
88 Correct 637 ms 15312 KB Output is correct
89 Correct 940 ms 11972 KB Output is correct
90 Correct 987 ms 11732 KB Output is correct
91 Correct 929 ms 16328 KB Output is correct
92 Correct 1047 ms 16392 KB Output is correct
93 Correct 908 ms 16328 KB Output is correct
94 Correct 1049 ms 16328 KB Output is correct
95 Correct 1033 ms 16328 KB Output is correct
96 Correct 897 ms 12828 KB Output is correct
97 Correct 976 ms 12924 KB Output is correct
98 Correct 797 ms 31616 KB Output is correct
99 Correct 975 ms 16260 KB Output is correct
100 Correct 959 ms 13916 KB Output is correct
101 Correct 915 ms 13256 KB Output is correct
102 Correct 1172 ms 12148 KB Output is correct
103 Correct 1046 ms 38148 KB Output is correct
104 Correct 928 ms 39392 KB Output is correct
105 Correct 982 ms 39400 KB Output is correct
106 Correct 1053 ms 16384 KB Output is correct
107 Correct 995 ms 12040 KB Output is correct
108 Correct 807 ms 12132 KB Output is correct
109 Correct 769 ms 12292 KB Output is correct