답안 #710789

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
710789 2023-03-15T19:41:55 Z ogibogi2004 디지털 회로 (IOI22_circuit) C++17
100 / 100
1127 ms 43000 KB
#include "circuit.h"
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll MAXN=2e5+6;
const ll mod=1000002022;
ll arr[MAXN][2];
ll fastpow(ll x,ll p)
{
    if(p==0)return 1;
    if(p==1)return x;
    ll t=fastpow(x,p/2);
    t*=t;t%=mod;
    if(p%2==1)
    {
        t*=x;
        t%=mod;
    }
    return t;
}
struct node
{
    ll val1,val2;
};
node merge_nodes(node n1,node n2)
{
    node ret;
    ret.val1=(n1.val1+n2.val1)%mod;
    ret.val2=(n1.val2+n2.val2)%mod;
    return ret;
}
struct segment_tree
{
    node tree[4*MAXN];
    ll lazy[4*MAXN];
    void build(ll l,ll r,ll idx)
    {
        lazy[idx]=0;
        if(l==r)
        {
            tree[idx].val1=arr[l][0];
            tree[idx].val2=arr[l][1];
            return;
        }
        ll mid=(l+r)/2;
        build(l,mid,idx*2);
        build(mid+1,r,idx*2+1);
        tree[idx]=merge_nodes(tree[idx*2],tree[idx*2+1]);
    }
    void push(ll l,ll r,ll idx)
    {
        if(lazy[idx]==0)return;
        swap(tree[idx].val1,tree[idx].val2);
        if(l!=r)
        {
            lazy[idx*2]^=lazy[idx];
            lazy[idx*2+1]^=lazy[idx];
        }
        lazy[idx]=0;
    }
    void update(ll idx,ll l,ll r,ll ql,ll qr)
    {
        push(l,r,idx);
        if(qr<l)return;
        if(ql>r)return;
        if(l>=ql&&r<=qr)
        {
            lazy[idx]^=1;
            push(l,r,idx);
            //cout<<"%%% "<<l<<" "<<r<<" "<<tree[idx]<<endl;
            return;
        }
        //cout<<"## "<<l<<" "<<r<<" "<<ql<<" "<<qr<<endl;
        ll mid=(l+r)/2;
        update(idx*2,l,mid,ql,qr);
        update(idx*2+1,mid+1,r,ql,qr);
        tree[idx]=merge_nodes(tree[idx*2],tree[idx*2+1]);
        //cout<<"%%% "<<l<<" "<<r<<" "<<tree[idx]<<endl;
    }
    node query(ll l,ll r,ll idx,ll ql,ll qr)
    {
        push(l,r,idx);
        if(l>qr)return {0,0};
        if(r<ql)return {0,0};
        //cout<<"%% "<<l<<" "<<r<<" "<<tree[idx]<<endl;
        if(l>=ql&&r<=qr)return tree[idx];
        ll mid=(l+r)/2;
        return merge_nodes(query(l,mid,idx*2,ql,qr),query(mid+1,r,idx*2+1,ql,qr));
    }
}tr;
ll m,n;
ll all_prod=1,sum1;
vector<ll>g[MAXN];
ll path_prod[MAXN];
ll subtree_prod[MAXN];
void dfs_subtrees(ll u)
{
    subtree_prod[u]=max(1ll,(ll)g[u].size());

    for(auto xd:g[u])
    {
        dfs_subtrees(xd);
        subtree_prod[u]*=subtree_prod[xd];
        subtree_prod[u]%=mod;
    }
}
void dfs_prods(ll u)
{
    if(g[u].size()==0)return;
    vector<ll>pref_prods;
    vector<ll>suf_prods;
    for(ll i=0;i<g[u].size();i++)
    {
        if(i==0)pref_prods.push_back(max(1ll,subtree_prod[g[u][i]]));
        else
        {
            pref_prods.push_back((pref_prods.back()*max(1ll,subtree_prod[g[u][i]]))%mod);
        }
    }
    for(ll i=g[u].size()-1;i>=0;i--)
    {
        if(i==g[u].size()-1)suf_prods.push_back(max(1ll,subtree_prod[g[u][i]]));
        else
        {
            suf_prods.push_back((suf_prods.back()*max(1ll,subtree_prod[g[u][i]]))%mod);
        }
    }
    reverse(suf_prods.begin(),suf_prods.end());
    for(ll i=0;i<g[u].size();i++)
    {
        ll prod1=1;
        if(i>0)prod1*=pref_prods[i-1];
        if(i+1<g[u].size())prod1*=suf_prods[i+1];
        prod1%=mod;
        //cout<<u<<" -> "<<g[u][i]<<" "<<path_prod[u]<<" * "<<prod1<<endl;
        path_prod[g[u][i]]=(path_prod[u]*prod1)%mod;
        dfs_prods(g[u][i]);
    }
}
vector<int>a1;
ll slow_update(int l,int r)
{
    for(int i=l;i<=r;i++)a1[i]=1-a1[i];
    ll ret=0;
    for(int i=0;i<a1.size();i++)
    {
        ret+=((ll)a1[i]*path_prod[n+i])%mod;
        ret%=mod;
    }
    return ret;
}
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
    m=M;n=N;
    a1=A;
    for(ll i=1;i<N+M;i++)
    {
        g[P[i]].push_back(i);
    }
    for(ll i=0;i<N;i++)
    {
        ll t=g[i].size();
        all_prod*=t;
        all_prod%=mod;
    }
    path_prod[0]=1;
    dfs_subtrees(0);
    dfs_prods(0);
    //cout<<all_prod<<endl;
    for(ll i=0;i<M;i++)
    {
        //arr[i]=(all_prod*fastpow(path_prod[N+i],mod-2))%mod;
        //cout<<path_prod[N+i]<<"|"<<arr[i]<<" ";
        arr[i][0]=path_prod[N+i]%mod;
        sum1+=arr[i][0];
        sum1%=mod;
        if(A[i]==0)swap(arr[i][0],arr[i][1]);
        //cout<<path_prod[N+i]<<" ";
        //if(A[i]==0)arr[i]=(mod-arr[i])%mod;
    }
    //cout<<endl;
    tr.build(0,M-1,1);
    //cout<<sum1<<" "<<tr.query(0,m-1,1,0,m-1)<<endl;
}

int count_ways(int L, int R) {
    //return slow_update(L-n, R-n);
    tr.update(1,0,m-1,L-n,R-n);
    return tr.query(0,m-1,1,0,m-1).val1;
}

Compilation message

circuit.cpp: In function 'void dfs_prods(long long int)':
circuit.cpp:112:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |     for(ll i=0;i<g[u].size();i++)
      |                ~^~~~~~~~~~~~
circuit.cpp:122:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |         if(i==g[u].size()-1)suf_prods.push_back(max(1ll,subtree_prod[g[u][i]]));
      |            ~^~~~~~~~~~~~~~~
circuit.cpp:129:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |     for(ll i=0;i<g[u].size();i++)
      |                ~^~~~~~~~~~~~
circuit.cpp:133:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |         if(i+1<g[u].size())prod1*=suf_prods[i+1];
      |            ~~~^~~~~~~~~~~~
circuit.cpp: In function 'long long int slow_update(int, int)':
circuit.cpp:145:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |     for(int i=0;i<a1.size();i++)
      |                 ~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 3 ms 5056 KB Output is correct
4 Correct 2 ms 5072 KB Output is correct
5 Correct 3 ms 5084 KB Output is correct
6 Correct 4 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 3 ms 4964 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 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 4 ms 5072 KB Output is correct
9 Correct 5 ms 5072 KB Output is correct
10 Correct 5 ms 5328 KB Output is correct
11 Correct 5 ms 5328 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 3 ms 5056 KB Output is correct
4 Correct 2 ms 5072 KB Output is correct
5 Correct 3 ms 5084 KB Output is correct
6 Correct 4 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 4964 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 3 ms 5072 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 3 ms 5072 KB Output is correct
16 Correct 4 ms 5072 KB Output is correct
17 Correct 5 ms 5072 KB Output is correct
18 Correct 5 ms 5328 KB Output is correct
19 Correct 5 ms 5328 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 3 ms 5072 KB Output is correct
24 Correct 3 ms 5072 KB Output is correct
25 Correct 3 ms 5072 KB Output is correct
26 Correct 3 ms 5072 KB Output is correct
27 Correct 4 ms 5072 KB Output is correct
28 Correct 3 ms 5072 KB Output is correct
29 Correct 3 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5200 KB Output is correct
32 Correct 3 ms 5072 KB Output is correct
33 Correct 4 ms 5072 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 3 ms 5072 KB Output is correct
36 Correct 3 ms 5328 KB Output is correct
37 Correct 4 ms 5328 KB Output is correct
38 Correct 4 ms 5328 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 4 ms 5072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 836 ms 10008 KB Output is correct
2 Correct 797 ms 14980 KB Output is correct
3 Correct 859 ms 15040 KB Output is correct
4 Correct 977 ms 14988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 836 ms 10008 KB Output is correct
2 Correct 797 ms 14980 KB Output is correct
3 Correct 859 ms 15040 KB Output is correct
4 Correct 977 ms 14988 KB Output is correct
5 Correct 769 ms 10024 KB Output is correct
6 Correct 929 ms 14984 KB Output is correct
7 Correct 927 ms 15040 KB Output is correct
8 Correct 801 ms 15048 KB Output is correct
9 Correct 356 ms 5328 KB Output is correct
10 Correct 933 ms 5584 KB Output is correct
11 Correct 903 ms 5584 KB Output is correct
12 Correct 852 ms 5584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 3 ms 4964 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 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 4 ms 5072 KB Output is correct
9 Correct 5 ms 5072 KB Output is correct
10 Correct 5 ms 5328 KB Output is correct
11 Correct 5 ms 5328 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 836 ms 10008 KB Output is correct
14 Correct 797 ms 14980 KB Output is correct
15 Correct 859 ms 15040 KB Output is correct
16 Correct 977 ms 14988 KB Output is correct
17 Correct 769 ms 10024 KB Output is correct
18 Correct 929 ms 14984 KB Output is correct
19 Correct 927 ms 15040 KB Output is correct
20 Correct 801 ms 15048 KB Output is correct
21 Correct 356 ms 5328 KB Output is correct
22 Correct 933 ms 5584 KB Output is correct
23 Correct 903 ms 5584 KB Output is correct
24 Correct 852 ms 5584 KB Output is correct
25 Correct 885 ms 21524 KB Output is correct
26 Correct 876 ms 21680 KB Output is correct
27 Correct 828 ms 21656 KB Output is correct
28 Correct 711 ms 21684 KB Output is correct
29 Correct 1028 ms 42020 KB Output is correct
30 Correct 954 ms 42056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 3 ms 5056 KB Output is correct
4 Correct 2 ms 5072 KB Output is correct
5 Correct 3 ms 5084 KB Output is correct
6 Correct 4 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 4964 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 3 ms 5072 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 3 ms 5072 KB Output is correct
16 Correct 4 ms 5072 KB Output is correct
17 Correct 5 ms 5072 KB Output is correct
18 Correct 5 ms 5328 KB Output is correct
19 Correct 5 ms 5328 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 3 ms 5072 KB Output is correct
24 Correct 3 ms 5072 KB Output is correct
25 Correct 3 ms 5072 KB Output is correct
26 Correct 3 ms 5072 KB Output is correct
27 Correct 4 ms 5072 KB Output is correct
28 Correct 3 ms 5072 KB Output is correct
29 Correct 3 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5200 KB Output is correct
32 Correct 3 ms 5072 KB Output is correct
33 Correct 4 ms 5072 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 3 ms 5072 KB Output is correct
36 Correct 3 ms 5328 KB Output is correct
37 Correct 4 ms 5328 KB Output is correct
38 Correct 4 ms 5328 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 4 ms 5072 KB Output is correct
43 Correct 643 ms 5456 KB Output is correct
44 Correct 996 ms 5456 KB Output is correct
45 Correct 836 ms 5524 KB Output is correct
46 Correct 706 ms 5968 KB Output is correct
47 Correct 615 ms 5960 KB Output is correct
48 Correct 885 ms 5968 KB Output is correct
49 Correct 959 ms 5968 KB Output is correct
50 Correct 707 ms 5840 KB Output is correct
51 Correct 874 ms 5852 KB Output is correct
52 Correct 959 ms 5840 KB Output is correct
53 Correct 704 ms 6224 KB Output is correct
54 Correct 821 ms 5840 KB Output is correct
55 Correct 908 ms 5840 KB Output is correct
56 Correct 667 ms 5760 KB Output is correct
57 Correct 947 ms 5712 KB Output is correct
58 Correct 723 ms 6864 KB Output is correct
59 Correct 844 ms 7080 KB Output is correct
60 Correct 807 ms 7008 KB Output is correct
61 Correct 766 ms 5712 KB Output is correct
62 Correct 621 ms 5468 KB Output is correct
63 Correct 800 ms 5456 KB Output is correct
64 Correct 812 ms 5712 KB Output is correct
65 Correct 463 ms 5328 KB Output is correct
66 Correct 896 ms 5584 KB Output is correct
67 Correct 857 ms 5584 KB Output is correct
68 Correct 689 ms 5584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 3 ms 5056 KB Output is correct
4 Correct 2 ms 5072 KB Output is correct
5 Correct 3 ms 5084 KB Output is correct
6 Correct 4 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 4964 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 3 ms 5072 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 3 ms 5072 KB Output is correct
16 Correct 4 ms 5072 KB Output is correct
17 Correct 5 ms 5072 KB Output is correct
18 Correct 5 ms 5328 KB Output is correct
19 Correct 5 ms 5328 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 3 ms 5072 KB Output is correct
24 Correct 3 ms 5072 KB Output is correct
25 Correct 3 ms 5072 KB Output is correct
26 Correct 3 ms 5072 KB Output is correct
27 Correct 4 ms 5072 KB Output is correct
28 Correct 3 ms 5072 KB Output is correct
29 Correct 3 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5200 KB Output is correct
32 Correct 3 ms 5072 KB Output is correct
33 Correct 4 ms 5072 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 3 ms 5072 KB Output is correct
36 Correct 3 ms 5328 KB Output is correct
37 Correct 4 ms 5328 KB Output is correct
38 Correct 4 ms 5328 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 4 ms 5072 KB Output is correct
43 Correct 836 ms 10008 KB Output is correct
44 Correct 797 ms 14980 KB Output is correct
45 Correct 859 ms 15040 KB Output is correct
46 Correct 977 ms 14988 KB Output is correct
47 Correct 769 ms 10024 KB Output is correct
48 Correct 929 ms 14984 KB Output is correct
49 Correct 927 ms 15040 KB Output is correct
50 Correct 801 ms 15048 KB Output is correct
51 Correct 356 ms 5328 KB Output is correct
52 Correct 933 ms 5584 KB Output is correct
53 Correct 903 ms 5584 KB Output is correct
54 Correct 852 ms 5584 KB Output is correct
55 Correct 885 ms 21524 KB Output is correct
56 Correct 876 ms 21680 KB Output is correct
57 Correct 828 ms 21656 KB Output is correct
58 Correct 711 ms 21684 KB Output is correct
59 Correct 1028 ms 42020 KB Output is correct
60 Correct 954 ms 42056 KB Output is correct
61 Correct 643 ms 5456 KB Output is correct
62 Correct 996 ms 5456 KB Output is correct
63 Correct 836 ms 5524 KB Output is correct
64 Correct 706 ms 5968 KB Output is correct
65 Correct 615 ms 5960 KB Output is correct
66 Correct 885 ms 5968 KB Output is correct
67 Correct 959 ms 5968 KB Output is correct
68 Correct 707 ms 5840 KB Output is correct
69 Correct 874 ms 5852 KB Output is correct
70 Correct 959 ms 5840 KB Output is correct
71 Correct 704 ms 6224 KB Output is correct
72 Correct 821 ms 5840 KB Output is correct
73 Correct 908 ms 5840 KB Output is correct
74 Correct 667 ms 5760 KB Output is correct
75 Correct 947 ms 5712 KB Output is correct
76 Correct 723 ms 6864 KB Output is correct
77 Correct 844 ms 7080 KB Output is correct
78 Correct 807 ms 7008 KB Output is correct
79 Correct 766 ms 5712 KB Output is correct
80 Correct 621 ms 5468 KB Output is correct
81 Correct 800 ms 5456 KB Output is correct
82 Correct 812 ms 5712 KB Output is correct
83 Correct 463 ms 5328 KB Output is correct
84 Correct 896 ms 5584 KB Output is correct
85 Correct 857 ms 5584 KB Output is correct
86 Correct 689 ms 5584 KB Output is correct
87 Correct 3 ms 5048 KB Output is correct
88 Correct 637 ms 20860 KB Output is correct
89 Correct 1077 ms 14836 KB Output is correct
90 Correct 881 ms 14916 KB Output is correct
91 Correct 985 ms 22344 KB Output is correct
92 Correct 788 ms 22296 KB Output is correct
93 Correct 1024 ms 22300 KB Output is correct
94 Correct 831 ms 22272 KB Output is correct
95 Correct 760 ms 22300 KB Output is correct
96 Correct 876 ms 17128 KB Output is correct
97 Correct 819 ms 17104 KB Output is correct
98 Correct 609 ms 30796 KB Output is correct
99 Correct 1020 ms 21620 KB Output is correct
100 Correct 1024 ms 19932 KB Output is correct
101 Correct 1105 ms 18940 KB Output is correct
102 Correct 998 ms 17160 KB Output is correct
103 Correct 891 ms 41996 KB Output is correct
104 Correct 1127 ms 42964 KB Output is correct
105 Correct 1055 ms 43000 KB Output is correct
106 Correct 935 ms 19040 KB Output is correct
107 Correct 876 ms 17032 KB Output is correct
108 Correct 1066 ms 17544 KB Output is correct
109 Correct 710 ms 17412 KB Output is correct