Submission #736779

# Submission time Handle Problem Language Result Execution time Memory
736779 2023-05-06T08:15:55 Z onlk97 Digital Circuit (IOI22_circuit) C++17
100 / 100
1170 ms 31064 KB
#include "circuit.h"
 
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const long long mod=1e9+2022LL;
vector <long long> g[200010];
long long sz[200010],d[200010],ac[200010],n,dif;
long long weight[200010],a[200010],weightps[200010],seg[800040],laz[800040];
bool marked[800040];
void dfs1(int cur){
    sz[cur]=g[cur].size();
    sz[cur]=max(sz[cur],1LL);
    sz[cur]%=mod;
    for (int i:g[cur]){
        dfs1(i);
        sz[cur]=sz[cur]*sz[i]%mod;
    }
    if (!g[cur].size()){
        return;
    } else {
        long long fr[g[cur].size()],ba[g[cur].size()];
        for (int i=0; i<g[cur].size(); i++){
            if (!i) fr[i]=sz[g[cur][i]]%mod;
            else fr[i]=fr[i-1]*sz[g[cur][i]]%mod;
        }
        for (int i=g[cur].size()-1; i>=0; i--){
            if (i==g[cur].size()-1) ba[i]=sz[g[cur][i]]%mod;
            else ba[i]=ba[i+1]*sz[g[cur][i]]%mod;
        }
        for (int i=0; i<g[cur].size(); i++){
            int t=1;
            if (i) t=t*fr[i-1]%mod;
            if (i+1<g[cur].size()) t=t*ba[i+1]%mod;
            d[g[cur][i]]=t%mod;
        }
    }
}
void dfs2(int cur){
    for (int i:g[cur]){
        ac[i]=ac[cur]%mod*d[i]%mod;
        dfs2(i);
    }
}
void build(int id,int tl,int tr){
    if (tl==tr){
        seg[id]=a[tl]*weight[tl]%mod;
        return;
    }
    int tm=(tl+tr)/2;
    build(2*id,tl,tm);
    build(2*id+1,tm+1,tr);
    seg[id]=(seg[2*id]+seg[2*id+1])%mod;
}
void pushdown(int id,int tl,int tr){
    if (!marked[id]) return;
    int tm=(tl+tr)/2;
    if (laz[id]%2==1){
        seg[2*id]=(weightps[tm]-weightps[tl-1]+mod-seg[2*id]+mod)%mod;
        seg[2*id+1]=(weightps[tr]-weightps[tm]+mod-seg[2*id+1]+mod)%mod;
    }
    marked[2*id]=1; laz[2*id]+=laz[id];
    marked[2*id+1]=1; laz[2*id+1]+=laz[id];
    marked[id]=0; laz[id]=0;
}
void update(int id,int tl,int tr,int l,int r){
    if (l>r) return;
    if (l<=tl&&tr<=r){
        seg[id]=(weightps[tr]-weightps[tl-1]+mod-seg[id]+mod)%mod;
        laz[id]++;
        marked[id]=1;
        return;
    }
    pushdown(id,tl,tr);
    int tm=(tl+tr)/2;
    update(2*id,tl,tm,l,min(r,tm));
    update(2*id+1,tm+1,tr,max(tm+1,l),r);
    seg[id]=(seg[2*id]+seg[2*id+1])%mod;
}
long long query(int id,int tl,int tr,int l,int r){
    if (l>r) return 0;
    if (l<=tl&&tr<=r) return seg[id];
    pushdown(id,tl,tr);
    int tm=(tl+tr)/2;
    long long lx=query(2*id,tl,tm,l,min(r,tm));
    long long rx=query(2*id+1,tm+1,tr,max(l,tm+1),r);
    return (lx+rx)%mod;
}
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
    n=M; dif=N-1;
    for (int i=1; i<N+M; i++) g[P[i]].push_back(i);
    dfs1(0);
    ac[0]=1;
    dfs2(0);
    for (int i=1; i<=n; i++) weight[i]=ac[i+N-1]%mod;
    for (int i=1; i<=n; i++) weightps[i]=(weightps[i-1]+weight[i])%mod;
    for (int i=1; i<=n; i++) a[i]=A[i-1];
    build(1,1,n);
}
 
int count_ways(int L, int R) {
    L-=dif; R-=dif;
    update(1,1,n,L,R);
    return query(1,1,n,1,n)%mod;
}

Compilation message

circuit.cpp: In function 'void dfs1(int)':
circuit.cpp:23:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |         for (int i=0; i<g[cur].size(); i++){
      |                       ~^~~~~~~~~~~~~~
circuit.cpp:28:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |             if (i==g[cur].size()-1) ba[i]=sz[g[cur][i]]%mod;
      |                 ~^~~~~~~~~~~~~~~~~
circuit.cpp:31:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |         for (int i=0; i<g[cur].size(); i++){
      |                       ~^~~~~~~~~~~~~~
circuit.cpp:34:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |             if (i+1<g[cur].size()) t=t*ba[i+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 4 ms 5072 KB Output is correct
5 Correct 3 ms 5180 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
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 4 ms 5108 KB Output is correct
3 Correct 4 ms 5072 KB Output is correct
4 Correct 4 ms 5140 KB Output is correct
5 Correct 3 ms 5072 KB Output is correct
6 Correct 3 ms 5176 KB Output is correct
7 Correct 4 ms 5200 KB Output is correct
8 Correct 5 ms 5112 KB Output is correct
9 Correct 4 ms 5204 KB Output is correct
10 Correct 5 ms 5200 KB Output is correct
11 Correct 5 ms 5200 KB Output is correct
12 Correct 4 ms 5200 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 4 ms 5072 KB Output is correct
5 Correct 3 ms 5180 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 4944 KB Output is correct
10 Correct 4 ms 5108 KB Output is correct
11 Correct 4 ms 5072 KB Output is correct
12 Correct 4 ms 5140 KB Output is correct
13 Correct 3 ms 5072 KB Output is correct
14 Correct 3 ms 5176 KB Output is correct
15 Correct 4 ms 5200 KB Output is correct
16 Correct 5 ms 5112 KB Output is correct
17 Correct 4 ms 5204 KB Output is correct
18 Correct 5 ms 5200 KB Output is correct
19 Correct 5 ms 5200 KB Output is correct
20 Correct 4 ms 5200 KB Output is correct
21 Correct 4 ms 5196 KB Output is correct
22 Correct 5 ms 5072 KB Output is correct
23 Correct 4 ms 5200 KB Output is correct
24 Correct 4 ms 5200 KB Output is correct
25 Correct 4 ms 5200 KB Output is correct
26 Correct 3 ms 5200 KB Output is correct
27 Correct 4 ms 5200 KB Output is correct
28 Correct 4 ms 5148 KB Output is correct
29 Correct 4 ms 5072 KB Output is correct
30 Correct 3 ms 5180 KB Output is correct
31 Correct 3 ms 5200 KB Output is correct
32 Correct 3 ms 5212 KB Output is correct
33 Correct 3 ms 5200 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 3 ms 5108 KB Output is correct
36 Correct 3 ms 5200 KB Output is correct
37 Correct 3 ms 5200 KB Output is correct
38 Correct 3 ms 5200 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 4 ms 5172 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 609 ms 9604 KB Output is correct
2 Correct 1028 ms 14232 KB Output is correct
3 Correct 1049 ms 14216 KB Output is correct
4 Correct 1058 ms 14200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 609 ms 9604 KB Output is correct
2 Correct 1028 ms 14232 KB Output is correct
3 Correct 1049 ms 14216 KB Output is correct
4 Correct 1058 ms 14200 KB Output is correct
5 Correct 893 ms 9868 KB Output is correct
6 Correct 936 ms 14672 KB Output is correct
7 Correct 1030 ms 14684 KB Output is correct
8 Correct 736 ms 14220 KB Output is correct
9 Correct 500 ms 5368 KB Output is correct
10 Correct 737 ms 5688 KB Output is correct
11 Correct 886 ms 5712 KB Output is correct
12 Correct 752 ms 5584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 4 ms 5108 KB Output is correct
3 Correct 4 ms 5072 KB Output is correct
4 Correct 4 ms 5140 KB Output is correct
5 Correct 3 ms 5072 KB Output is correct
6 Correct 3 ms 5176 KB Output is correct
7 Correct 4 ms 5200 KB Output is correct
8 Correct 5 ms 5112 KB Output is correct
9 Correct 4 ms 5204 KB Output is correct
10 Correct 5 ms 5200 KB Output is correct
11 Correct 5 ms 5200 KB Output is correct
12 Correct 4 ms 5200 KB Output is correct
13 Correct 609 ms 9604 KB Output is correct
14 Correct 1028 ms 14232 KB Output is correct
15 Correct 1049 ms 14216 KB Output is correct
16 Correct 1058 ms 14200 KB Output is correct
17 Correct 893 ms 9868 KB Output is correct
18 Correct 936 ms 14672 KB Output is correct
19 Correct 1030 ms 14684 KB Output is correct
20 Correct 736 ms 14220 KB Output is correct
21 Correct 500 ms 5368 KB Output is correct
22 Correct 737 ms 5688 KB Output is correct
23 Correct 886 ms 5712 KB Output is correct
24 Correct 752 ms 5584 KB Output is correct
25 Correct 952 ms 20564 KB Output is correct
26 Correct 981 ms 20752 KB Output is correct
27 Correct 1142 ms 20748 KB Output is correct
28 Correct 898 ms 19488 KB Output is correct
29 Correct 1170 ms 28564 KB Output is correct
30 Correct 944 ms 27356 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 4 ms 5072 KB Output is correct
5 Correct 3 ms 5180 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 4944 KB Output is correct
10 Correct 4 ms 5108 KB Output is correct
11 Correct 4 ms 5072 KB Output is correct
12 Correct 4 ms 5140 KB Output is correct
13 Correct 3 ms 5072 KB Output is correct
14 Correct 3 ms 5176 KB Output is correct
15 Correct 4 ms 5200 KB Output is correct
16 Correct 5 ms 5112 KB Output is correct
17 Correct 4 ms 5204 KB Output is correct
18 Correct 5 ms 5200 KB Output is correct
19 Correct 5 ms 5200 KB Output is correct
20 Correct 4 ms 5200 KB Output is correct
21 Correct 4 ms 5196 KB Output is correct
22 Correct 5 ms 5072 KB Output is correct
23 Correct 4 ms 5200 KB Output is correct
24 Correct 4 ms 5200 KB Output is correct
25 Correct 4 ms 5200 KB Output is correct
26 Correct 3 ms 5200 KB Output is correct
27 Correct 4 ms 5200 KB Output is correct
28 Correct 4 ms 5148 KB Output is correct
29 Correct 4 ms 5072 KB Output is correct
30 Correct 3 ms 5180 KB Output is correct
31 Correct 3 ms 5200 KB Output is correct
32 Correct 3 ms 5212 KB Output is correct
33 Correct 3 ms 5200 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 3 ms 5108 KB Output is correct
36 Correct 3 ms 5200 KB Output is correct
37 Correct 3 ms 5200 KB Output is correct
38 Correct 3 ms 5200 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 4 ms 5172 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
43 Correct 708 ms 5456 KB Output is correct
44 Correct 860 ms 5584 KB Output is correct
45 Correct 1006 ms 5584 KB Output is correct
46 Correct 633 ms 5840 KB Output is correct
47 Correct 736 ms 5968 KB Output is correct
48 Correct 983 ms 5968 KB Output is correct
49 Correct 928 ms 5968 KB Output is correct
50 Correct 1122 ms 5840 KB Output is correct
51 Correct 984 ms 5712 KB Output is correct
52 Correct 874 ms 5712 KB Output is correct
53 Correct 904 ms 5712 KB Output is correct
54 Correct 926 ms 5840 KB Output is correct
55 Correct 1033 ms 5840 KB Output is correct
56 Correct 866 ms 5712 KB Output is correct
57 Correct 694 ms 5584 KB Output is correct
58 Correct 852 ms 6212 KB Output is correct
59 Correct 1079 ms 6524 KB Output is correct
60 Correct 814 ms 6404 KB Output is correct
61 Correct 783 ms 5584 KB Output is correct
62 Correct 837 ms 5456 KB Output is correct
63 Correct 928 ms 5456 KB Output is correct
64 Correct 933 ms 5692 KB Output is correct
65 Correct 432 ms 5328 KB Output is correct
66 Correct 829 ms 5584 KB Output is correct
67 Correct 927 ms 5660 KB Output is correct
68 Correct 652 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 3 ms 5072 KB Output is correct
4 Correct 4 ms 5072 KB Output is correct
5 Correct 3 ms 5180 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 4944 KB Output is correct
10 Correct 4 ms 5108 KB Output is correct
11 Correct 4 ms 5072 KB Output is correct
12 Correct 4 ms 5140 KB Output is correct
13 Correct 3 ms 5072 KB Output is correct
14 Correct 3 ms 5176 KB Output is correct
15 Correct 4 ms 5200 KB Output is correct
16 Correct 5 ms 5112 KB Output is correct
17 Correct 4 ms 5204 KB Output is correct
18 Correct 5 ms 5200 KB Output is correct
19 Correct 5 ms 5200 KB Output is correct
20 Correct 4 ms 5200 KB Output is correct
21 Correct 4 ms 5196 KB Output is correct
22 Correct 5 ms 5072 KB Output is correct
23 Correct 4 ms 5200 KB Output is correct
24 Correct 4 ms 5200 KB Output is correct
25 Correct 4 ms 5200 KB Output is correct
26 Correct 3 ms 5200 KB Output is correct
27 Correct 4 ms 5200 KB Output is correct
28 Correct 4 ms 5148 KB Output is correct
29 Correct 4 ms 5072 KB Output is correct
30 Correct 3 ms 5180 KB Output is correct
31 Correct 3 ms 5200 KB Output is correct
32 Correct 3 ms 5212 KB Output is correct
33 Correct 3 ms 5200 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 3 ms 5108 KB Output is correct
36 Correct 3 ms 5200 KB Output is correct
37 Correct 3 ms 5200 KB Output is correct
38 Correct 3 ms 5200 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 4 ms 5172 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
43 Correct 609 ms 9604 KB Output is correct
44 Correct 1028 ms 14232 KB Output is correct
45 Correct 1049 ms 14216 KB Output is correct
46 Correct 1058 ms 14200 KB Output is correct
47 Correct 893 ms 9868 KB Output is correct
48 Correct 936 ms 14672 KB Output is correct
49 Correct 1030 ms 14684 KB Output is correct
50 Correct 736 ms 14220 KB Output is correct
51 Correct 500 ms 5368 KB Output is correct
52 Correct 737 ms 5688 KB Output is correct
53 Correct 886 ms 5712 KB Output is correct
54 Correct 752 ms 5584 KB Output is correct
55 Correct 952 ms 20564 KB Output is correct
56 Correct 981 ms 20752 KB Output is correct
57 Correct 1142 ms 20748 KB Output is correct
58 Correct 898 ms 19488 KB Output is correct
59 Correct 1170 ms 28564 KB Output is correct
60 Correct 944 ms 27356 KB Output is correct
61 Correct 708 ms 5456 KB Output is correct
62 Correct 860 ms 5584 KB Output is correct
63 Correct 1006 ms 5584 KB Output is correct
64 Correct 633 ms 5840 KB Output is correct
65 Correct 736 ms 5968 KB Output is correct
66 Correct 983 ms 5968 KB Output is correct
67 Correct 928 ms 5968 KB Output is correct
68 Correct 1122 ms 5840 KB Output is correct
69 Correct 984 ms 5712 KB Output is correct
70 Correct 874 ms 5712 KB Output is correct
71 Correct 904 ms 5712 KB Output is correct
72 Correct 926 ms 5840 KB Output is correct
73 Correct 1033 ms 5840 KB Output is correct
74 Correct 866 ms 5712 KB Output is correct
75 Correct 694 ms 5584 KB Output is correct
76 Correct 852 ms 6212 KB Output is correct
77 Correct 1079 ms 6524 KB Output is correct
78 Correct 814 ms 6404 KB Output is correct
79 Correct 783 ms 5584 KB Output is correct
80 Correct 837 ms 5456 KB Output is correct
81 Correct 928 ms 5456 KB Output is correct
82 Correct 933 ms 5692 KB Output is correct
83 Correct 432 ms 5328 KB Output is correct
84 Correct 829 ms 5584 KB Output is correct
85 Correct 927 ms 5660 KB Output is correct
86 Correct 652 ms 5584 KB Output is correct
87 Correct 3 ms 4944 KB Output is correct
88 Correct 454 ms 19888 KB Output is correct
89 Correct 1018 ms 14536 KB Output is correct
90 Correct 946 ms 14640 KB Output is correct
91 Correct 991 ms 20784 KB Output is correct
92 Correct 976 ms 21360 KB Output is correct
93 Correct 1006 ms 21496 KB Output is correct
94 Correct 1062 ms 21372 KB Output is correct
95 Correct 1102 ms 20168 KB Output is correct
96 Correct 774 ms 15752 KB Output is correct
97 Correct 1119 ms 17364 KB Output is correct
98 Correct 804 ms 19016 KB Output is correct
99 Correct 979 ms 20748 KB Output is correct
100 Correct 1010 ms 18824 KB Output is correct
101 Correct 933 ms 17688 KB Output is correct
102 Correct 1067 ms 15856 KB Output is correct
103 Correct 970 ms 28564 KB Output is correct
104 Correct 924 ms 31064 KB Output is correct
105 Correct 891 ms 29800 KB Output is correct
106 Correct 946 ms 15876 KB Output is correct
107 Correct 1067 ms 15948 KB Output is correct
108 Correct 1052 ms 16368 KB Output is correct
109 Correct 1033 ms 16036 KB Output is correct