답안 #640375

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
640375 2022-09-14T11:53:35 Z arnold518 디지털 회로 (IOI22_circuit) C++17
100 / 100
1122 ms 67088 KB
#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 3e5;
const ll MOD = 1e9+2022;

int N, M;
vector<int> adj[MAXN+10];
ll phi[MAXN+10];
vector<ll> PV[MAXN+10], SV[MAXN+10];
int S[MAXN+10];
ll A[MAXN+10];

void dfs(int now)
{
    if(adj[now].empty())
    {
        phi[now]=1;
        return;
    }
    phi[now]=adj[now].size();
    for(auto nxt : adj[now])
    {
        dfs(nxt);
        PV[now].push_back(phi[nxt]);
        SV[now].push_back(phi[nxt]);
        phi[now]=phi[now]*phi[nxt]%MOD;
    }
    for(int i=1; i<PV[now].size(); i++) PV[now][i]=PV[now][i-1]*PV[now][i]%MOD;
    for(int i=SV[now].size()-2; i>=0; i--) SV[now][i]=SV[now][i+1]*SV[now][i]%MOD;
}

void dfs2(int now, ll val)
{
    if(adj[now].empty())
    {
        A[now]=val;
        return;
    }
    for(int i=0; i<adj[now].size(); i++)
    {
        int nxt=adj[now][i];
        ll t=1;
        if(i-1>=0) t=t*PV[now][i-1]%MOD;
        if(i+1<SV[now].size()) t=t*SV[now][i+1]%MOD;
        dfs2(nxt, val*t%MOD);
    }
}

pll operator + (const pll &p, const pll &q) 
{
    return {(p.first+q.first)%MOD, (p.second+q.second)%MOD};
}

struct SEG
{
    pll tree[MAXN*4+10];
    int lazy[MAXN*4+10];

    void init(int node, int tl, int tr)
    {
        lazy[node]=0;
        if(tl==tr)
        {
            tree[node]=pll(0, A[tl]);
            if(S[tl]) swap(tree[node].first, tree[node].second);
            return;
        }
        int mid=tl+tr>>1;
        init(node*2, tl, mid);
        init(node*2+1, mid+1, tr);
        tree[node]=tree[node*2]+tree[node*2+1];
    }

    void busy(int node, int tl, int tr)
    {
        if(lazy[node]) swap(tree[node].first, tree[node].second);
        if(tl!=tr) lazy[node*2]^=lazy[node], lazy[node*2+1]^=lazy[node];
        lazy[node]=0;
    }

    void update(int node, int tl, int tr, int l, int r)
    {
        busy(node, tl, tr);
        if(l<=tl && tr<=r)
        {
            lazy[node]=1;
            busy(node, tl, tr);
            return;
        }
        if(r<tl || tr<l) return;
        int mid=tl+tr>>1;
        update(node*2, tl, mid, l, r);
        update(node*2+1, mid+1, tr, l, r);
        tree[node]=tree[node*2]+tree[node*2+1];
    }

    ll query()
    {
        if(!lazy[1]) return tree[1].first;
        else return tree[1].second;
    }
}seg;


void init(int _N, int _M, vector<int> _P, vector<int> _Q)
{
    N=_N; M=_M;
    for(int i=1; i<N+M; i++) adj[_P[i]].push_back(i);
    for(int i=N; i<N+M; i++) S[i]=_Q[i-N];

    dfs(0);
    dfs2(0, 1);
    
    seg.init(1, N, N+M-1);
}

int count_ways(int L, int R)
{
    seg.update(1, N, N+M-1, L, R);
    return seg.query();
}

Compilation message

circuit.cpp: In function 'void dfs(int)':
circuit.cpp:34:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |     for(int i=1; i<PV[now].size(); i++) PV[now][i]=PV[now][i-1]*PV[now][i]%MOD;
      |                  ~^~~~~~~~~~~~~~~
circuit.cpp: In function 'void dfs2(int, ll)':
circuit.cpp:45:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     for(int i=0; i<adj[now].size(); i++)
      |                  ~^~~~~~~~~~~~~~~~
circuit.cpp:50:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |         if(i+1<SV[now].size()) t=t*SV[now][i+1]%MOD;
      |            ~~~^~~~~~~~~~~~~~~
circuit.cpp: In member function 'void SEG::init(int, int, int)':
circuit.cpp:74:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   74 |         int mid=tl+tr>>1;
      |                 ~~^~~
circuit.cpp: In member function 'void SEG::update(int, int, int, int, int)':
circuit.cpp:97:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   97 |         int mid=tl+tr>>1;
      |                 ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 40144 KB Output is correct
2 Correct 16 ms 40208 KB Output is correct
3 Correct 18 ms 40240 KB Output is correct
4 Correct 18 ms 40272 KB Output is correct
5 Correct 17 ms 40272 KB Output is correct
6 Correct 18 ms 40292 KB Output is correct
7 Correct 19 ms 40272 KB Output is correct
8 Correct 22 ms 40244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 40144 KB Output is correct
2 Correct 21 ms 40272 KB Output is correct
3 Correct 17 ms 40256 KB Output is correct
4 Correct 18 ms 40192 KB Output is correct
5 Correct 18 ms 40272 KB Output is correct
6 Correct 20 ms 40324 KB Output is correct
7 Correct 18 ms 40288 KB Output is correct
8 Correct 18 ms 40280 KB Output is correct
9 Correct 20 ms 40340 KB Output is correct
10 Correct 18 ms 40432 KB Output is correct
11 Correct 18 ms 40372 KB Output is correct
12 Correct 19 ms 40272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 40144 KB Output is correct
2 Correct 16 ms 40208 KB Output is correct
3 Correct 18 ms 40240 KB Output is correct
4 Correct 18 ms 40272 KB Output is correct
5 Correct 17 ms 40272 KB Output is correct
6 Correct 18 ms 40292 KB Output is correct
7 Correct 19 ms 40272 KB Output is correct
8 Correct 22 ms 40244 KB Output is correct
9 Correct 21 ms 40144 KB Output is correct
10 Correct 21 ms 40272 KB Output is correct
11 Correct 17 ms 40256 KB Output is correct
12 Correct 18 ms 40192 KB Output is correct
13 Correct 18 ms 40272 KB Output is correct
14 Correct 20 ms 40324 KB Output is correct
15 Correct 18 ms 40288 KB Output is correct
16 Correct 18 ms 40280 KB Output is correct
17 Correct 20 ms 40340 KB Output is correct
18 Correct 18 ms 40432 KB Output is correct
19 Correct 18 ms 40372 KB Output is correct
20 Correct 19 ms 40272 KB Output is correct
21 Correct 25 ms 40320 KB Output is correct
22 Correct 23 ms 40264 KB Output is correct
23 Correct 22 ms 40272 KB Output is correct
24 Correct 18 ms 40400 KB Output is correct
25 Correct 18 ms 40308 KB Output is correct
26 Correct 22 ms 40400 KB Output is correct
27 Correct 18 ms 40400 KB Output is correct
28 Correct 19 ms 40284 KB Output is correct
29 Correct 17 ms 40272 KB Output is correct
30 Correct 17 ms 40280 KB Output is correct
31 Correct 18 ms 40384 KB Output is correct
32 Correct 18 ms 40272 KB Output is correct
33 Correct 17 ms 40272 KB Output is correct
34 Correct 18 ms 40240 KB Output is correct
35 Correct 18 ms 40272 KB Output is correct
36 Correct 18 ms 40456 KB Output is correct
37 Correct 19 ms 40452 KB Output is correct
38 Correct 21 ms 40420 KB Output is correct
39 Correct 20 ms 40252 KB Output is correct
40 Correct 18 ms 40268 KB Output is correct
41 Correct 19 ms 40232 KB Output is correct
42 Correct 17 ms 40244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 696 ms 45208 KB Output is correct
2 Correct 861 ms 50180 KB Output is correct
3 Correct 668 ms 50352 KB Output is correct
4 Correct 670 ms 50232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 696 ms 45208 KB Output is correct
2 Correct 861 ms 50180 KB Output is correct
3 Correct 668 ms 50352 KB Output is correct
4 Correct 670 ms 50232 KB Output is correct
5 Correct 754 ms 45128 KB Output is correct
6 Correct 1044 ms 50152 KB Output is correct
7 Correct 973 ms 50212 KB Output is correct
8 Correct 759 ms 50176 KB Output is correct
9 Correct 448 ms 40572 KB Output is correct
10 Correct 952 ms 40784 KB Output is correct
11 Correct 764 ms 40820 KB Output is correct
12 Correct 700 ms 40756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 40144 KB Output is correct
2 Correct 21 ms 40272 KB Output is correct
3 Correct 17 ms 40256 KB Output is correct
4 Correct 18 ms 40192 KB Output is correct
5 Correct 18 ms 40272 KB Output is correct
6 Correct 20 ms 40324 KB Output is correct
7 Correct 18 ms 40288 KB Output is correct
8 Correct 18 ms 40280 KB Output is correct
9 Correct 20 ms 40340 KB Output is correct
10 Correct 18 ms 40432 KB Output is correct
11 Correct 18 ms 40372 KB Output is correct
12 Correct 19 ms 40272 KB Output is correct
13 Correct 696 ms 45208 KB Output is correct
14 Correct 861 ms 50180 KB Output is correct
15 Correct 668 ms 50352 KB Output is correct
16 Correct 670 ms 50232 KB Output is correct
17 Correct 754 ms 45128 KB Output is correct
18 Correct 1044 ms 50152 KB Output is correct
19 Correct 973 ms 50212 KB Output is correct
20 Correct 759 ms 50176 KB Output is correct
21 Correct 448 ms 40572 KB Output is correct
22 Correct 952 ms 40784 KB Output is correct
23 Correct 764 ms 40820 KB Output is correct
24 Correct 700 ms 40756 KB Output is correct
25 Correct 959 ms 55368 KB Output is correct
26 Correct 1045 ms 55636 KB Output is correct
27 Correct 877 ms 55696 KB Output is correct
28 Correct 891 ms 55732 KB Output is correct
29 Correct 877 ms 65036 KB Output is correct
30 Correct 872 ms 65112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 40144 KB Output is correct
2 Correct 16 ms 40208 KB Output is correct
3 Correct 18 ms 40240 KB Output is correct
4 Correct 18 ms 40272 KB Output is correct
5 Correct 17 ms 40272 KB Output is correct
6 Correct 18 ms 40292 KB Output is correct
7 Correct 19 ms 40272 KB Output is correct
8 Correct 22 ms 40244 KB Output is correct
9 Correct 21 ms 40144 KB Output is correct
10 Correct 21 ms 40272 KB Output is correct
11 Correct 17 ms 40256 KB Output is correct
12 Correct 18 ms 40192 KB Output is correct
13 Correct 18 ms 40272 KB Output is correct
14 Correct 20 ms 40324 KB Output is correct
15 Correct 18 ms 40288 KB Output is correct
16 Correct 18 ms 40280 KB Output is correct
17 Correct 20 ms 40340 KB Output is correct
18 Correct 18 ms 40432 KB Output is correct
19 Correct 18 ms 40372 KB Output is correct
20 Correct 19 ms 40272 KB Output is correct
21 Correct 25 ms 40320 KB Output is correct
22 Correct 23 ms 40264 KB Output is correct
23 Correct 22 ms 40272 KB Output is correct
24 Correct 18 ms 40400 KB Output is correct
25 Correct 18 ms 40308 KB Output is correct
26 Correct 22 ms 40400 KB Output is correct
27 Correct 18 ms 40400 KB Output is correct
28 Correct 19 ms 40284 KB Output is correct
29 Correct 17 ms 40272 KB Output is correct
30 Correct 17 ms 40280 KB Output is correct
31 Correct 18 ms 40384 KB Output is correct
32 Correct 18 ms 40272 KB Output is correct
33 Correct 17 ms 40272 KB Output is correct
34 Correct 18 ms 40240 KB Output is correct
35 Correct 18 ms 40272 KB Output is correct
36 Correct 18 ms 40456 KB Output is correct
37 Correct 19 ms 40452 KB Output is correct
38 Correct 21 ms 40420 KB Output is correct
39 Correct 20 ms 40252 KB Output is correct
40 Correct 18 ms 40268 KB Output is correct
41 Correct 19 ms 40232 KB Output is correct
42 Correct 17 ms 40244 KB Output is correct
43 Correct 503 ms 40656 KB Output is correct
44 Correct 857 ms 40784 KB Output is correct
45 Correct 901 ms 40784 KB Output is correct
46 Correct 846 ms 41040 KB Output is correct
47 Correct 893 ms 41040 KB Output is correct
48 Correct 845 ms 41040 KB Output is correct
49 Correct 921 ms 41040 KB Output is correct
50 Correct 746 ms 41100 KB Output is correct
51 Correct 881 ms 40684 KB Output is correct
52 Correct 835 ms 40684 KB Output is correct
53 Correct 619 ms 41168 KB Output is correct
54 Correct 839 ms 40912 KB Output is correct
55 Correct 975 ms 40752 KB Output is correct
56 Correct 721 ms 40656 KB Output is correct
57 Correct 799 ms 40528 KB Output is correct
58 Correct 877 ms 41424 KB Output is correct
59 Correct 826 ms 41552 KB Output is correct
60 Correct 801 ms 41508 KB Output is correct
61 Correct 853 ms 40912 KB Output is correct
62 Correct 930 ms 40520 KB Output is correct
63 Correct 833 ms 40528 KB Output is correct
64 Correct 757 ms 40608 KB Output is correct
65 Correct 375 ms 40528 KB Output is correct
66 Correct 852 ms 40784 KB Output is correct
67 Correct 654 ms 40780 KB Output is correct
68 Correct 858 ms 40784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 40144 KB Output is correct
2 Correct 16 ms 40208 KB Output is correct
3 Correct 18 ms 40240 KB Output is correct
4 Correct 18 ms 40272 KB Output is correct
5 Correct 17 ms 40272 KB Output is correct
6 Correct 18 ms 40292 KB Output is correct
7 Correct 19 ms 40272 KB Output is correct
8 Correct 22 ms 40244 KB Output is correct
9 Correct 21 ms 40144 KB Output is correct
10 Correct 21 ms 40272 KB Output is correct
11 Correct 17 ms 40256 KB Output is correct
12 Correct 18 ms 40192 KB Output is correct
13 Correct 18 ms 40272 KB Output is correct
14 Correct 20 ms 40324 KB Output is correct
15 Correct 18 ms 40288 KB Output is correct
16 Correct 18 ms 40280 KB Output is correct
17 Correct 20 ms 40340 KB Output is correct
18 Correct 18 ms 40432 KB Output is correct
19 Correct 18 ms 40372 KB Output is correct
20 Correct 19 ms 40272 KB Output is correct
21 Correct 25 ms 40320 KB Output is correct
22 Correct 23 ms 40264 KB Output is correct
23 Correct 22 ms 40272 KB Output is correct
24 Correct 18 ms 40400 KB Output is correct
25 Correct 18 ms 40308 KB Output is correct
26 Correct 22 ms 40400 KB Output is correct
27 Correct 18 ms 40400 KB Output is correct
28 Correct 19 ms 40284 KB Output is correct
29 Correct 17 ms 40272 KB Output is correct
30 Correct 17 ms 40280 KB Output is correct
31 Correct 18 ms 40384 KB Output is correct
32 Correct 18 ms 40272 KB Output is correct
33 Correct 17 ms 40272 KB Output is correct
34 Correct 18 ms 40240 KB Output is correct
35 Correct 18 ms 40272 KB Output is correct
36 Correct 18 ms 40456 KB Output is correct
37 Correct 19 ms 40452 KB Output is correct
38 Correct 21 ms 40420 KB Output is correct
39 Correct 20 ms 40252 KB Output is correct
40 Correct 18 ms 40268 KB Output is correct
41 Correct 19 ms 40232 KB Output is correct
42 Correct 17 ms 40244 KB Output is correct
43 Correct 696 ms 45208 KB Output is correct
44 Correct 861 ms 50180 KB Output is correct
45 Correct 668 ms 50352 KB Output is correct
46 Correct 670 ms 50232 KB Output is correct
47 Correct 754 ms 45128 KB Output is correct
48 Correct 1044 ms 50152 KB Output is correct
49 Correct 973 ms 50212 KB Output is correct
50 Correct 759 ms 50176 KB Output is correct
51 Correct 448 ms 40572 KB Output is correct
52 Correct 952 ms 40784 KB Output is correct
53 Correct 764 ms 40820 KB Output is correct
54 Correct 700 ms 40756 KB Output is correct
55 Correct 959 ms 55368 KB Output is correct
56 Correct 1045 ms 55636 KB Output is correct
57 Correct 877 ms 55696 KB Output is correct
58 Correct 891 ms 55732 KB Output is correct
59 Correct 877 ms 65036 KB Output is correct
60 Correct 872 ms 65112 KB Output is correct
61 Correct 503 ms 40656 KB Output is correct
62 Correct 857 ms 40784 KB Output is correct
63 Correct 901 ms 40784 KB Output is correct
64 Correct 846 ms 41040 KB Output is correct
65 Correct 893 ms 41040 KB Output is correct
66 Correct 845 ms 41040 KB Output is correct
67 Correct 921 ms 41040 KB Output is correct
68 Correct 746 ms 41100 KB Output is correct
69 Correct 881 ms 40684 KB Output is correct
70 Correct 835 ms 40684 KB Output is correct
71 Correct 619 ms 41168 KB Output is correct
72 Correct 839 ms 40912 KB Output is correct
73 Correct 975 ms 40752 KB Output is correct
74 Correct 721 ms 40656 KB Output is correct
75 Correct 799 ms 40528 KB Output is correct
76 Correct 877 ms 41424 KB Output is correct
77 Correct 826 ms 41552 KB Output is correct
78 Correct 801 ms 41508 KB Output is correct
79 Correct 853 ms 40912 KB Output is correct
80 Correct 930 ms 40520 KB Output is correct
81 Correct 833 ms 40528 KB Output is correct
82 Correct 757 ms 40608 KB Output is correct
83 Correct 375 ms 40528 KB Output is correct
84 Correct 852 ms 40784 KB Output is correct
85 Correct 654 ms 40780 KB Output is correct
86 Correct 858 ms 40784 KB Output is correct
87 Correct 18 ms 40168 KB Output is correct
88 Correct 691 ms 55396 KB Output is correct
89 Correct 990 ms 51124 KB Output is correct
90 Correct 921 ms 50792 KB Output is correct
91 Correct 971 ms 57016 KB Output is correct
92 Correct 1122 ms 57020 KB Output is correct
93 Correct 1044 ms 57032 KB Output is correct
94 Correct 859 ms 57152 KB Output is correct
95 Correct 874 ms 57088 KB Output is correct
96 Correct 721 ms 47532 KB Output is correct
97 Correct 945 ms 47520 KB Output is correct
98 Correct 824 ms 60544 KB Output is correct
99 Correct 946 ms 55712 KB Output is correct
100 Correct 912 ms 51728 KB Output is correct
101 Correct 996 ms 50244 KB Output is correct
102 Correct 814 ms 47036 KB Output is correct
103 Correct 1057 ms 65144 KB Output is correct
104 Correct 866 ms 67012 KB Output is correct
105 Correct 867 ms 67088 KB Output is correct
106 Correct 875 ms 52932 KB Output is correct
107 Correct 812 ms 48652 KB Output is correct
108 Correct 914 ms 48684 KB Output is correct
109 Correct 854 ms 47532 KB Output is correct