답안 #757164

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
757164 2023-06-12T18:02:32 Z boris_mihov 디지털 회로 (IOI22_circuit) C++17
100 / 100
1278 ms 48376 KB
#include "circuit.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
 
typedef long long llong;
const int MAXN = 200000 + 10;
const int MOD = 1000002022;
const int INF = 1e9;

int n, m;
struct SegmentTree
{
    struct Node
    {
        llong realVal, swapVal;
        bool lazy;

        Node()
        {
            realVal = swapVal = lazy = 0;
        }
    };

    Node tree[2*MAXN]; // 2 * MAXN * 2

    Node combine(Node left, Node right)
    {
        Node res;
        res.realVal = left.realVal + right.realVal;
        res.swapVal = left.swapVal + right.swapVal;
        if (res.realVal >= MOD) res.realVal -= MOD;
        if (res.swapVal >= MOD) res.swapVal -= MOD;
        return res;
    }

    void push(int node, int l, int r)
    {
        if (!tree[node].lazy)
        {
            return;
        }

        std::swap(tree[node].realVal, tree[node].swapVal);
        if (l < r)
        {
            tree[2*node].lazy ^= 1;
            tree[2*node + 1].lazy ^= 1;
        }

        tree[node].lazy = 0;
    }

    void build(int l, int r, int node, llong vals[], llong b[])
    {
        if (l == r)
        {
            tree[node].realVal = vals[n + l];
            tree[node].swapVal = 0;

            if (b[n + l] == 0)
            {
                std::swap(tree[node].realVal, tree[node].swapVal);   
            }

            return;
        }

        int mid = (l + r) / 2;
        build(l, mid, 2*node, vals, b);
        build(mid + 1, r, 2*node + 1, vals, b);
        tree[node] = combine(tree[2*node], tree[2*node + 1]);
    }

    void update(int l, int r, int node, int queryL, int queryR)
    {
        push(node, l, r);
        if (queryR < l || r < queryL)
        {
            return;
        }

        if (queryL <= l && r <= queryR)
        {
            tree[node].lazy ^= 1;
            push(node, l, r);
            return;
        }

        int mid = (l + r) / 2;
        update(l, mid, 2*node, queryL, queryR);
        update(mid + 1, r, 2*node + 1, queryL, queryR);
        tree[node] = combine(tree[2*node], tree[2*node + 1]);
    }

    void build(llong multBy[], llong b[])
    {
        build(1, m, 1, multBy, b);
    }

    llong query(int l, int r)
    {
        update(1, m, 1, l - n + 1, r - n + 1);
        return tree[1].realVal;
    }
};

llong w[MAXN];
llong b[MAXN];
llong pw[MAXN];
llong multBy[MAXN];
std::vector <int> g[MAXN];
std::vector <int> prefixPW[MAXN];
std::vector <int> suffixPW[MAXN];
SegmentTree tree;

void initDFS(int node)
{
    if (g[node].empty())
    {
        pw[node] = 1;
        return;
    }
    
    pw[node] = g[node].size();
    for (const int &i : g[node])
    {
        initDFS(i);
        pw[node] *= pw[i];
        pw[node] %= MOD;
    }

    prefixPW[node].resize(g[node].size());
    prefixPW[node][0] = 1;

    for (int i = 1 ; i < g[node].size() ; ++i)
    {
        prefixPW[node][i] = (prefixPW[node][i - 1] * pw[g[node][i - 1]]) % MOD;
    }

    suffixPW[node].resize(g[node].size());
    suffixPW[node][g[node].size() - 1] = 1;
    for (int i = (int)g[node].size() - 2 ; i >= 0 ; --i)
    {
        suffixPW[node][i] = (suffixPW[node][i + 1] * pw[g[node][i + 1]]) % MOD;
    }
}

void findMult(int node, llong mult)
{
    if (g[node].empty())
    {
        multBy[node] = mult;
        return;
    }
    
    for (int i = 0 ; i < g[node].size() ; ++i)
    {
        findMult(g[node][i], (((mult * prefixPW[node][i]) % MOD) * suffixPW[node][i]) % MOD);
    }
}

void init(int N, int M, std::vector <int> P, std::vector <int> A) 
{
    n = N;
    m = M;
    for (int i = 1 ; i < n + m ; ++i)
    {
        g[P[i] + 1].push_back(i + 1);
    }
 
    for (int i = n + 1 ; i <= n + m ; ++i)
    {
        if (A[i - n - 1] == 0)
        {
            w[i] = 1;
            b[i] = 0;
        } else
        {
            b[i] = 1;
            w[i] = 0;
        }
    }

    initDFS(1);
    findMult(1, 1);
    tree.build(multBy, b);
}
 
int count_ways(int L, int R) 
{
    return tree.query(L, R);
}

Compilation message

circuit.cpp: In function 'void initDFS(int)':
circuit.cpp:137:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |     for (int i = 1 ; i < g[node].size() ; ++i)
      |                      ~~^~~~~~~~~~~~~~~~
circuit.cpp: In function 'void findMult(int, llong)':
circuit.cpp:158:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |     for (int i = 0 ; i < g[node].size() ; ++i)
      |                      ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23704 KB Output is correct
2 Correct 12 ms 23760 KB Output is correct
3 Correct 12 ms 23772 KB Output is correct
4 Correct 14 ms 23760 KB Output is correct
5 Correct 12 ms 23844 KB Output is correct
6 Correct 12 ms 23760 KB Output is correct
7 Correct 15 ms 23844 KB Output is correct
8 Correct 12 ms 23832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23760 KB Output is correct
2 Correct 12 ms 23740 KB Output is correct
3 Correct 13 ms 23888 KB Output is correct
4 Correct 12 ms 23888 KB Output is correct
5 Correct 12 ms 23804 KB Output is correct
6 Correct 13 ms 23888 KB Output is correct
7 Correct 14 ms 24016 KB Output is correct
8 Correct 13 ms 23888 KB Output is correct
9 Correct 15 ms 23888 KB Output is correct
10 Correct 16 ms 24000 KB Output is correct
11 Correct 12 ms 23948 KB Output is correct
12 Correct 11 ms 23888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23704 KB Output is correct
2 Correct 12 ms 23760 KB Output is correct
3 Correct 12 ms 23772 KB Output is correct
4 Correct 14 ms 23760 KB Output is correct
5 Correct 12 ms 23844 KB Output is correct
6 Correct 12 ms 23760 KB Output is correct
7 Correct 15 ms 23844 KB Output is correct
8 Correct 12 ms 23832 KB Output is correct
9 Correct 12 ms 23760 KB Output is correct
10 Correct 12 ms 23740 KB Output is correct
11 Correct 13 ms 23888 KB Output is correct
12 Correct 12 ms 23888 KB Output is correct
13 Correct 12 ms 23804 KB Output is correct
14 Correct 13 ms 23888 KB Output is correct
15 Correct 14 ms 24016 KB Output is correct
16 Correct 13 ms 23888 KB Output is correct
17 Correct 15 ms 23888 KB Output is correct
18 Correct 16 ms 24000 KB Output is correct
19 Correct 12 ms 23948 KB Output is correct
20 Correct 11 ms 23888 KB Output is correct
21 Correct 12 ms 23856 KB Output is correct
22 Correct 12 ms 23888 KB Output is correct
23 Correct 12 ms 23888 KB Output is correct
24 Correct 13 ms 23872 KB Output is correct
25 Correct 12 ms 23888 KB Output is correct
26 Correct 12 ms 23888 KB Output is correct
27 Correct 13 ms 23924 KB Output is correct
28 Correct 12 ms 23960 KB Output is correct
29 Correct 14 ms 23748 KB Output is correct
30 Correct 14 ms 23916 KB Output is correct
31 Correct 12 ms 24000 KB Output is correct
32 Correct 17 ms 23888 KB Output is correct
33 Correct 12 ms 23888 KB Output is correct
34 Correct 12 ms 23888 KB Output is correct
35 Correct 13 ms 23816 KB Output is correct
36 Correct 12 ms 24000 KB Output is correct
37 Correct 12 ms 24016 KB Output is correct
38 Correct 16 ms 23948 KB Output is correct
39 Correct 12 ms 23864 KB Output is correct
40 Correct 14 ms 23904 KB Output is correct
41 Correct 12 ms 23888 KB Output is correct
42 Correct 12 ms 23760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 516 ms 28928 KB Output is correct
2 Correct 858 ms 34016 KB Output is correct
3 Correct 960 ms 33992 KB Output is correct
4 Correct 1099 ms 34020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 516 ms 28928 KB Output is correct
2 Correct 858 ms 34016 KB Output is correct
3 Correct 960 ms 33992 KB Output is correct
4 Correct 1099 ms 34020 KB Output is correct
5 Correct 957 ms 28872 KB Output is correct
6 Correct 925 ms 34076 KB Output is correct
7 Correct 1086 ms 34004 KB Output is correct
8 Correct 657 ms 34036 KB Output is correct
9 Correct 598 ms 24076 KB Output is correct
10 Correct 867 ms 24424 KB Output is correct
11 Correct 994 ms 24420 KB Output is correct
12 Correct 1069 ms 24400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23760 KB Output is correct
2 Correct 12 ms 23740 KB Output is correct
3 Correct 13 ms 23888 KB Output is correct
4 Correct 12 ms 23888 KB Output is correct
5 Correct 12 ms 23804 KB Output is correct
6 Correct 13 ms 23888 KB Output is correct
7 Correct 14 ms 24016 KB Output is correct
8 Correct 13 ms 23888 KB Output is correct
9 Correct 15 ms 23888 KB Output is correct
10 Correct 16 ms 24000 KB Output is correct
11 Correct 12 ms 23948 KB Output is correct
12 Correct 11 ms 23888 KB Output is correct
13 Correct 516 ms 28928 KB Output is correct
14 Correct 858 ms 34016 KB Output is correct
15 Correct 960 ms 33992 KB Output is correct
16 Correct 1099 ms 34020 KB Output is correct
17 Correct 957 ms 28872 KB Output is correct
18 Correct 925 ms 34076 KB Output is correct
19 Correct 1086 ms 34004 KB Output is correct
20 Correct 657 ms 34036 KB Output is correct
21 Correct 598 ms 24076 KB Output is correct
22 Correct 867 ms 24424 KB Output is correct
23 Correct 994 ms 24420 KB Output is correct
24 Correct 1069 ms 24400 KB Output is correct
25 Correct 1278 ms 39136 KB Output is correct
26 Correct 1037 ms 39372 KB Output is correct
27 Correct 1190 ms 39576 KB Output is correct
28 Correct 762 ms 39336 KB Output is correct
29 Correct 970 ms 47196 KB Output is correct
30 Correct 923 ms 47152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23704 KB Output is correct
2 Correct 12 ms 23760 KB Output is correct
3 Correct 12 ms 23772 KB Output is correct
4 Correct 14 ms 23760 KB Output is correct
5 Correct 12 ms 23844 KB Output is correct
6 Correct 12 ms 23760 KB Output is correct
7 Correct 15 ms 23844 KB Output is correct
8 Correct 12 ms 23832 KB Output is correct
9 Correct 12 ms 23760 KB Output is correct
10 Correct 12 ms 23740 KB Output is correct
11 Correct 13 ms 23888 KB Output is correct
12 Correct 12 ms 23888 KB Output is correct
13 Correct 12 ms 23804 KB Output is correct
14 Correct 13 ms 23888 KB Output is correct
15 Correct 14 ms 24016 KB Output is correct
16 Correct 13 ms 23888 KB Output is correct
17 Correct 15 ms 23888 KB Output is correct
18 Correct 16 ms 24000 KB Output is correct
19 Correct 12 ms 23948 KB Output is correct
20 Correct 11 ms 23888 KB Output is correct
21 Correct 12 ms 23856 KB Output is correct
22 Correct 12 ms 23888 KB Output is correct
23 Correct 12 ms 23888 KB Output is correct
24 Correct 13 ms 23872 KB Output is correct
25 Correct 12 ms 23888 KB Output is correct
26 Correct 12 ms 23888 KB Output is correct
27 Correct 13 ms 23924 KB Output is correct
28 Correct 12 ms 23960 KB Output is correct
29 Correct 14 ms 23748 KB Output is correct
30 Correct 14 ms 23916 KB Output is correct
31 Correct 12 ms 24000 KB Output is correct
32 Correct 17 ms 23888 KB Output is correct
33 Correct 12 ms 23888 KB Output is correct
34 Correct 12 ms 23888 KB Output is correct
35 Correct 13 ms 23816 KB Output is correct
36 Correct 12 ms 24000 KB Output is correct
37 Correct 12 ms 24016 KB Output is correct
38 Correct 16 ms 23948 KB Output is correct
39 Correct 12 ms 23864 KB Output is correct
40 Correct 14 ms 23904 KB Output is correct
41 Correct 12 ms 23888 KB Output is correct
42 Correct 12 ms 23760 KB Output is correct
43 Correct 647 ms 24272 KB Output is correct
44 Correct 1030 ms 24272 KB Output is correct
45 Correct 932 ms 24272 KB Output is correct
46 Correct 837 ms 24528 KB Output is correct
47 Correct 916 ms 24528 KB Output is correct
48 Correct 820 ms 24528 KB Output is correct
49 Correct 1054 ms 24528 KB Output is correct
50 Correct 1012 ms 24528 KB Output is correct
51 Correct 887 ms 24016 KB Output is correct
52 Correct 991 ms 24016 KB Output is correct
53 Correct 929 ms 24656 KB Output is correct
54 Correct 938 ms 24480 KB Output is correct
55 Correct 679 ms 24272 KB Output is correct
56 Correct 1002 ms 24144 KB Output is correct
57 Correct 868 ms 24016 KB Output is correct
58 Correct 884 ms 24960 KB Output is correct
59 Correct 881 ms 25040 KB Output is correct
60 Correct 805 ms 25040 KB Output is correct
61 Correct 913 ms 24400 KB Output is correct
62 Correct 883 ms 24204 KB Output is correct
63 Correct 757 ms 24144 KB Output is correct
64 Correct 836 ms 24028 KB Output is correct
65 Correct 326 ms 24080 KB Output is correct
66 Correct 949 ms 24400 KB Output is correct
67 Correct 866 ms 24396 KB Output is correct
68 Correct 856 ms 24400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23704 KB Output is correct
2 Correct 12 ms 23760 KB Output is correct
3 Correct 12 ms 23772 KB Output is correct
4 Correct 14 ms 23760 KB Output is correct
5 Correct 12 ms 23844 KB Output is correct
6 Correct 12 ms 23760 KB Output is correct
7 Correct 15 ms 23844 KB Output is correct
8 Correct 12 ms 23832 KB Output is correct
9 Correct 12 ms 23760 KB Output is correct
10 Correct 12 ms 23740 KB Output is correct
11 Correct 13 ms 23888 KB Output is correct
12 Correct 12 ms 23888 KB Output is correct
13 Correct 12 ms 23804 KB Output is correct
14 Correct 13 ms 23888 KB Output is correct
15 Correct 14 ms 24016 KB Output is correct
16 Correct 13 ms 23888 KB Output is correct
17 Correct 15 ms 23888 KB Output is correct
18 Correct 16 ms 24000 KB Output is correct
19 Correct 12 ms 23948 KB Output is correct
20 Correct 11 ms 23888 KB Output is correct
21 Correct 12 ms 23856 KB Output is correct
22 Correct 12 ms 23888 KB Output is correct
23 Correct 12 ms 23888 KB Output is correct
24 Correct 13 ms 23872 KB Output is correct
25 Correct 12 ms 23888 KB Output is correct
26 Correct 12 ms 23888 KB Output is correct
27 Correct 13 ms 23924 KB Output is correct
28 Correct 12 ms 23960 KB Output is correct
29 Correct 14 ms 23748 KB Output is correct
30 Correct 14 ms 23916 KB Output is correct
31 Correct 12 ms 24000 KB Output is correct
32 Correct 17 ms 23888 KB Output is correct
33 Correct 12 ms 23888 KB Output is correct
34 Correct 12 ms 23888 KB Output is correct
35 Correct 13 ms 23816 KB Output is correct
36 Correct 12 ms 24000 KB Output is correct
37 Correct 12 ms 24016 KB Output is correct
38 Correct 16 ms 23948 KB Output is correct
39 Correct 12 ms 23864 KB Output is correct
40 Correct 14 ms 23904 KB Output is correct
41 Correct 12 ms 23888 KB Output is correct
42 Correct 12 ms 23760 KB Output is correct
43 Correct 516 ms 28928 KB Output is correct
44 Correct 858 ms 34016 KB Output is correct
45 Correct 960 ms 33992 KB Output is correct
46 Correct 1099 ms 34020 KB Output is correct
47 Correct 957 ms 28872 KB Output is correct
48 Correct 925 ms 34076 KB Output is correct
49 Correct 1086 ms 34004 KB Output is correct
50 Correct 657 ms 34036 KB Output is correct
51 Correct 598 ms 24076 KB Output is correct
52 Correct 867 ms 24424 KB Output is correct
53 Correct 994 ms 24420 KB Output is correct
54 Correct 1069 ms 24400 KB Output is correct
55 Correct 1278 ms 39136 KB Output is correct
56 Correct 1037 ms 39372 KB Output is correct
57 Correct 1190 ms 39576 KB Output is correct
58 Correct 762 ms 39336 KB Output is correct
59 Correct 970 ms 47196 KB Output is correct
60 Correct 923 ms 47152 KB Output is correct
61 Correct 647 ms 24272 KB Output is correct
62 Correct 1030 ms 24272 KB Output is correct
63 Correct 932 ms 24272 KB Output is correct
64 Correct 837 ms 24528 KB Output is correct
65 Correct 916 ms 24528 KB Output is correct
66 Correct 820 ms 24528 KB Output is correct
67 Correct 1054 ms 24528 KB Output is correct
68 Correct 1012 ms 24528 KB Output is correct
69 Correct 887 ms 24016 KB Output is correct
70 Correct 991 ms 24016 KB Output is correct
71 Correct 929 ms 24656 KB Output is correct
72 Correct 938 ms 24480 KB Output is correct
73 Correct 679 ms 24272 KB Output is correct
74 Correct 1002 ms 24144 KB Output is correct
75 Correct 868 ms 24016 KB Output is correct
76 Correct 884 ms 24960 KB Output is correct
77 Correct 881 ms 25040 KB Output is correct
78 Correct 805 ms 25040 KB Output is correct
79 Correct 913 ms 24400 KB Output is correct
80 Correct 883 ms 24204 KB Output is correct
81 Correct 757 ms 24144 KB Output is correct
82 Correct 836 ms 24028 KB Output is correct
83 Correct 326 ms 24080 KB Output is correct
84 Correct 949 ms 24400 KB Output is correct
85 Correct 866 ms 24396 KB Output is correct
86 Correct 856 ms 24400 KB Output is correct
87 Correct 13 ms 23760 KB Output is correct
88 Correct 786 ms 37984 KB Output is correct
89 Correct 1050 ms 34652 KB Output is correct
90 Correct 1096 ms 33832 KB Output is correct
91 Correct 1025 ms 39508 KB Output is correct
92 Correct 1194 ms 39612 KB Output is correct
93 Correct 1105 ms 39556 KB Output is correct
94 Correct 1132 ms 39560 KB Output is correct
95 Correct 1172 ms 39524 KB Output is correct
96 Correct 939 ms 29808 KB Output is correct
97 Correct 1186 ms 29780 KB Output is correct
98 Correct 912 ms 42464 KB Output is correct
99 Correct 1176 ms 39396 KB Output is correct
100 Correct 1175 ms 33952 KB Output is correct
101 Correct 900 ms 31576 KB Output is correct
102 Correct 879 ms 29768 KB Output is correct
103 Correct 1144 ms 47232 KB Output is correct
104 Correct 1091 ms 48340 KB Output is correct
105 Correct 1147 ms 48376 KB Output is correct
106 Correct 1215 ms 35928 KB Output is correct
107 Correct 1059 ms 31332 KB Output is correct
108 Correct 1077 ms 30956 KB Output is correct
109 Correct 1167 ms 29768 KB Output is correct