Submission #866786

# Submission time Handle Problem Language Result Execution time Memory
866786 2023-10-27T04:12:05 Z Pety Digital Circuit (IOI22_circuit) C++17
100 / 100
733 ms 37828 KB
  #include <bits/stdc++.h>


  using namespace std;

  const int mod = 1000002022;
  const int N = 1e5+2;
  const int NM = 2e5+2;
  int n, m, val[NM], lazy[4*N], contrib[N];
  vector<int>G[NM];

  pair<int, int>aint[4*N];

  pair<int, int>init_contrib[NM];

  void dfs (int nod) {
    if (nod >= n) {
      val[nod] = 1;
      return;
    }
    val[nod] = G[nod].size();
    for (auto it : G[nod]) {
      dfs(it);
      val[nod] = 1ll * val[it] * val[nod] % mod;
    }
  } 

  void dfs_contrib (int nod, int c) {
    if (nod >= n) {
      contrib[nod - n] = c;
      return;
    }
    vector<int>pref(G[nod].size());
    vector<int>suf(G[nod].size());
    for (int i = 0; i < G[nod].size(); i++) {
      int child = G[nod][i];
      pref[i] = val[child];
      if (i)
        pref[i] = 1ll * pref[i - 1] * pref[i] % mod;
    }
    for (int i = G[nod].size() - 1; i >= 0; i--) {
      int child = G[nod][i];
      suf[i] = val[child];
      if (i + 1 < G[nod].size())
        suf[i] = 1ll * suf[i] * suf[i + 1] % mod;
    }
    for (int i = 0; i < G[nod].size(); i++) {
      int child = G[nod][i];
      int val = c;
      if (i > 0)
        val = 1ll * val * pref[i - 1] % mod;
      if (i + 1 < G[nod].size())
        val = 1ll * val * suf[i + 1] % mod;
      dfs_contrib(child, val);
    }
  }

  pair<int, int> operator+ (pair<int, int> a, pair<int, int>b) {
    return {(a.first + b.first) % mod, (a.second + b.second) % mod};
  }

  void build (int nod, int st, int dr) {
    if (st == dr) {
      aint[nod] = init_contrib[st];
      return;
    }
    int mid = (st + dr) / 2;
    build(2 * nod, st, mid);
    build(2 * nod + 1, mid + 1, dr);
    aint[nod] = aint[2 * nod] + aint[2 * nod + 1];
  }

  void push (int nod) {
    if (!lazy[nod])
      return;
    for (int i = 0; i < 2; i++) {
      swap(aint[2 * nod + i].first, aint[2 * nod + i].second);
      lazy[2 * nod + i] ^= 1;
    }
    lazy[nod] = 0;
  }

  void update (int nod, int st, int dr, int a, int b) {
    if (a <= st && dr <= b) {
      swap(aint[nod].first, aint[nod].second);
      lazy[nod] ^= 1;
      return;
    }
    int mid = (st + dr) / 2;
    push(nod);
    if (a <= mid)
      update(2 * nod, st, mid, a, b);
    if (b > mid)
      update(2 * nod + 1, mid + 1, dr, a, b);
    aint[nod] = aint[2 * nod] + aint[2 * nod + 1];
  }


  void init (int _n, int _m, vector<int>P, vector<int> A) {
    n = _n;
    m = _m;
    for (int i = 1; i < P.size(); i++)
      G[P[i]].push_back(i);
    dfs(0);
    dfs_contrib(0, 1);
    for (int i = 0; i < m; i++) {
      pair<int, int>c = {0, contrib[i]};
      if (!A[i])
        swap(c.first, c.second);
      init_contrib[i] = c;
    }
    build(1, 0, m - 1);
  }

  int count_ways(int l, int r) {
    update(1, 0, m - 1, l - n, r - n);
    return aint[1].second;
  }

Compilation message

circuit.cpp: In function 'void dfs_contrib(int, int)':
circuit.cpp:35:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |     for (int i = 0; i < G[nod].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
circuit.cpp:44:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |       if (i + 1 < G[nod].size())
      |           ~~~~~~^~~~~~~~~~~~~~~
circuit.cpp:47:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |     for (int i = 0; i < G[nod].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
circuit.cpp:52:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |       if (i + 1 < G[nod].size())
      |           ~~~~~~^~~~~~~~~~~~~~~
circuit.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>)':
circuit.cpp:102:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |     for (int i = 1; i < P.size(); i++)
      |                     ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 2 ms 8536 KB Output is correct
4 Correct 2 ms 8536 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 2 ms 8536 KB Output is correct
7 Correct 1 ms 8536 KB Output is correct
8 Correct 2 ms 8632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 1 ms 8792 KB Output is correct
4 Correct 2 ms 8536 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 2 ms 8536 KB Output is correct
7 Correct 2 ms 8536 KB Output is correct
8 Correct 2 ms 8536 KB Output is correct
9 Correct 2 ms 8536 KB Output is correct
10 Correct 2 ms 8792 KB Output is correct
11 Correct 2 ms 8792 KB Output is correct
12 Correct 2 ms 8536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 2 ms 8536 KB Output is correct
4 Correct 2 ms 8536 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 2 ms 8536 KB Output is correct
7 Correct 1 ms 8536 KB Output is correct
8 Correct 2 ms 8632 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 1 ms 8536 KB Output is correct
11 Correct 1 ms 8792 KB Output is correct
12 Correct 2 ms 8536 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 2 ms 8536 KB Output is correct
15 Correct 2 ms 8536 KB Output is correct
16 Correct 2 ms 8536 KB Output is correct
17 Correct 2 ms 8536 KB Output is correct
18 Correct 2 ms 8792 KB Output is correct
19 Correct 2 ms 8792 KB Output is correct
20 Correct 2 ms 8536 KB Output is correct
21 Correct 2 ms 8536 KB Output is correct
22 Correct 2 ms 8536 KB Output is correct
23 Correct 2 ms 8536 KB Output is correct
24 Correct 2 ms 8536 KB Output is correct
25 Correct 2 ms 8536 KB Output is correct
26 Correct 2 ms 8536 KB Output is correct
27 Correct 2 ms 8536 KB Output is correct
28 Correct 2 ms 8792 KB Output is correct
29 Correct 1 ms 8536 KB Output is correct
30 Correct 2 ms 8536 KB Output is correct
31 Correct 2 ms 8792 KB Output is correct
32 Correct 2 ms 8536 KB Output is correct
33 Correct 2 ms 8536 KB Output is correct
34 Correct 2 ms 8536 KB Output is correct
35 Correct 2 ms 8536 KB Output is correct
36 Correct 2 ms 8792 KB Output is correct
37 Correct 2 ms 8792 KB Output is correct
38 Correct 2 ms 8792 KB Output is correct
39 Correct 2 ms 8536 KB Output is correct
40 Correct 2 ms 8536 KB Output is correct
41 Correct 2 ms 8536 KB Output is correct
42 Correct 2 ms 8536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 358 ms 10836 KB Output is correct
2 Correct 547 ms 14796 KB Output is correct
3 Correct 542 ms 14928 KB Output is correct
4 Correct 547 ms 14804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 358 ms 10836 KB Output is correct
2 Correct 547 ms 14796 KB Output is correct
3 Correct 542 ms 14928 KB Output is correct
4 Correct 547 ms 14804 KB Output is correct
5 Correct 434 ms 10840 KB Output is correct
6 Correct 540 ms 14796 KB Output is correct
7 Correct 573 ms 14928 KB Output is correct
8 Correct 535 ms 14804 KB Output is correct
9 Correct 267 ms 8792 KB Output is correct
10 Correct 527 ms 8792 KB Output is correct
11 Correct 532 ms 8792 KB Output is correct
12 Correct 508 ms 8792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 1 ms 8792 KB Output is correct
4 Correct 2 ms 8536 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 2 ms 8536 KB Output is correct
7 Correct 2 ms 8536 KB Output is correct
8 Correct 2 ms 8536 KB Output is correct
9 Correct 2 ms 8536 KB Output is correct
10 Correct 2 ms 8792 KB Output is correct
11 Correct 2 ms 8792 KB Output is correct
12 Correct 2 ms 8536 KB Output is correct
13 Correct 358 ms 10836 KB Output is correct
14 Correct 547 ms 14796 KB Output is correct
15 Correct 542 ms 14928 KB Output is correct
16 Correct 547 ms 14804 KB Output is correct
17 Correct 434 ms 10840 KB Output is correct
18 Correct 540 ms 14796 KB Output is correct
19 Correct 573 ms 14928 KB Output is correct
20 Correct 535 ms 14804 KB Output is correct
21 Correct 267 ms 8792 KB Output is correct
22 Correct 527 ms 8792 KB Output is correct
23 Correct 532 ms 8792 KB Output is correct
24 Correct 508 ms 8792 KB Output is correct
25 Correct 580 ms 17040 KB Output is correct
26 Correct 573 ms 16996 KB Output is correct
27 Correct 629 ms 17060 KB Output is correct
28 Correct 475 ms 16956 KB Output is correct
29 Correct 603 ms 37628 KB Output is correct
30 Correct 566 ms 37464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 2 ms 8536 KB Output is correct
4 Correct 2 ms 8536 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 2 ms 8536 KB Output is correct
7 Correct 1 ms 8536 KB Output is correct
8 Correct 2 ms 8632 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 1 ms 8536 KB Output is correct
11 Correct 1 ms 8792 KB Output is correct
12 Correct 2 ms 8536 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 2 ms 8536 KB Output is correct
15 Correct 2 ms 8536 KB Output is correct
16 Correct 2 ms 8536 KB Output is correct
17 Correct 2 ms 8536 KB Output is correct
18 Correct 2 ms 8792 KB Output is correct
19 Correct 2 ms 8792 KB Output is correct
20 Correct 2 ms 8536 KB Output is correct
21 Correct 2 ms 8536 KB Output is correct
22 Correct 2 ms 8536 KB Output is correct
23 Correct 2 ms 8536 KB Output is correct
24 Correct 2 ms 8536 KB Output is correct
25 Correct 2 ms 8536 KB Output is correct
26 Correct 2 ms 8536 KB Output is correct
27 Correct 2 ms 8536 KB Output is correct
28 Correct 2 ms 8792 KB Output is correct
29 Correct 1 ms 8536 KB Output is correct
30 Correct 2 ms 8536 KB Output is correct
31 Correct 2 ms 8792 KB Output is correct
32 Correct 2 ms 8536 KB Output is correct
33 Correct 2 ms 8536 KB Output is correct
34 Correct 2 ms 8536 KB Output is correct
35 Correct 2 ms 8536 KB Output is correct
36 Correct 2 ms 8792 KB Output is correct
37 Correct 2 ms 8792 KB Output is correct
38 Correct 2 ms 8792 KB Output is correct
39 Correct 2 ms 8536 KB Output is correct
40 Correct 2 ms 8536 KB Output is correct
41 Correct 2 ms 8536 KB Output is correct
42 Correct 2 ms 8536 KB Output is correct
43 Correct 374 ms 8792 KB Output is correct
44 Correct 527 ms 8792 KB Output is correct
45 Correct 513 ms 8792 KB Output is correct
46 Correct 493 ms 8792 KB Output is correct
47 Correct 526 ms 8792 KB Output is correct
48 Correct 527 ms 8792 KB Output is correct
49 Correct 546 ms 8792 KB Output is correct
50 Correct 514 ms 8792 KB Output is correct
51 Correct 507 ms 8792 KB Output is correct
52 Correct 515 ms 8792 KB Output is correct
53 Correct 494 ms 9816 KB Output is correct
54 Correct 527 ms 8792 KB Output is correct
55 Correct 540 ms 8792 KB Output is correct
56 Correct 518 ms 8792 KB Output is correct
57 Correct 546 ms 8792 KB Output is correct
58 Correct 533 ms 9816 KB Output is correct
59 Correct 580 ms 10072 KB Output is correct
60 Correct 544 ms 10072 KB Output is correct
61 Correct 556 ms 9048 KB Output is correct
62 Correct 525 ms 8792 KB Output is correct
63 Correct 557 ms 8792 KB Output is correct
64 Correct 551 ms 8792 KB Output is correct
65 Correct 301 ms 8792 KB Output is correct
66 Correct 555 ms 8792 KB Output is correct
67 Correct 575 ms 8792 KB Output is correct
68 Correct 510 ms 8792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 2 ms 8536 KB Output is correct
4 Correct 2 ms 8536 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 2 ms 8536 KB Output is correct
7 Correct 1 ms 8536 KB Output is correct
8 Correct 2 ms 8632 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 1 ms 8536 KB Output is correct
11 Correct 1 ms 8792 KB Output is correct
12 Correct 2 ms 8536 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 2 ms 8536 KB Output is correct
15 Correct 2 ms 8536 KB Output is correct
16 Correct 2 ms 8536 KB Output is correct
17 Correct 2 ms 8536 KB Output is correct
18 Correct 2 ms 8792 KB Output is correct
19 Correct 2 ms 8792 KB Output is correct
20 Correct 2 ms 8536 KB Output is correct
21 Correct 2 ms 8536 KB Output is correct
22 Correct 2 ms 8536 KB Output is correct
23 Correct 2 ms 8536 KB Output is correct
24 Correct 2 ms 8536 KB Output is correct
25 Correct 2 ms 8536 KB Output is correct
26 Correct 2 ms 8536 KB Output is correct
27 Correct 2 ms 8536 KB Output is correct
28 Correct 2 ms 8792 KB Output is correct
29 Correct 1 ms 8536 KB Output is correct
30 Correct 2 ms 8536 KB Output is correct
31 Correct 2 ms 8792 KB Output is correct
32 Correct 2 ms 8536 KB Output is correct
33 Correct 2 ms 8536 KB Output is correct
34 Correct 2 ms 8536 KB Output is correct
35 Correct 2 ms 8536 KB Output is correct
36 Correct 2 ms 8792 KB Output is correct
37 Correct 2 ms 8792 KB Output is correct
38 Correct 2 ms 8792 KB Output is correct
39 Correct 2 ms 8536 KB Output is correct
40 Correct 2 ms 8536 KB Output is correct
41 Correct 2 ms 8536 KB Output is correct
42 Correct 2 ms 8536 KB Output is correct
43 Correct 358 ms 10836 KB Output is correct
44 Correct 547 ms 14796 KB Output is correct
45 Correct 542 ms 14928 KB Output is correct
46 Correct 547 ms 14804 KB Output is correct
47 Correct 434 ms 10840 KB Output is correct
48 Correct 540 ms 14796 KB Output is correct
49 Correct 573 ms 14928 KB Output is correct
50 Correct 535 ms 14804 KB Output is correct
51 Correct 267 ms 8792 KB Output is correct
52 Correct 527 ms 8792 KB Output is correct
53 Correct 532 ms 8792 KB Output is correct
54 Correct 508 ms 8792 KB Output is correct
55 Correct 580 ms 17040 KB Output is correct
56 Correct 573 ms 16996 KB Output is correct
57 Correct 629 ms 17060 KB Output is correct
58 Correct 475 ms 16956 KB Output is correct
59 Correct 603 ms 37628 KB Output is correct
60 Correct 566 ms 37464 KB Output is correct
61 Correct 374 ms 8792 KB Output is correct
62 Correct 527 ms 8792 KB Output is correct
63 Correct 513 ms 8792 KB Output is correct
64 Correct 493 ms 8792 KB Output is correct
65 Correct 526 ms 8792 KB Output is correct
66 Correct 527 ms 8792 KB Output is correct
67 Correct 546 ms 8792 KB Output is correct
68 Correct 514 ms 8792 KB Output is correct
69 Correct 507 ms 8792 KB Output is correct
70 Correct 515 ms 8792 KB Output is correct
71 Correct 494 ms 9816 KB Output is correct
72 Correct 527 ms 8792 KB Output is correct
73 Correct 540 ms 8792 KB Output is correct
74 Correct 518 ms 8792 KB Output is correct
75 Correct 546 ms 8792 KB Output is correct
76 Correct 533 ms 9816 KB Output is correct
77 Correct 580 ms 10072 KB Output is correct
78 Correct 544 ms 10072 KB Output is correct
79 Correct 556 ms 9048 KB Output is correct
80 Correct 525 ms 8792 KB Output is correct
81 Correct 557 ms 8792 KB Output is correct
82 Correct 551 ms 8792 KB Output is correct
83 Correct 301 ms 8792 KB Output is correct
84 Correct 555 ms 8792 KB Output is correct
85 Correct 575 ms 8792 KB Output is correct
86 Correct 510 ms 8792 KB Output is correct
87 Correct 2 ms 8536 KB Output is correct
88 Correct 375 ms 16588 KB Output is correct
89 Correct 593 ms 15212 KB Output is correct
90 Correct 605 ms 14748 KB Output is correct
91 Correct 621 ms 17056 KB Output is correct
92 Correct 684 ms 17252 KB Output is correct
93 Correct 652 ms 17244 KB Output is correct
94 Correct 733 ms 17236 KB Output is correct
95 Correct 597 ms 17236 KB Output is correct
96 Correct 589 ms 13416 KB Output is correct
97 Correct 647 ms 13248 KB Output is correct
98 Correct 565 ms 33108 KB Output is correct
99 Correct 688 ms 16948 KB Output is correct
100 Correct 631 ms 14768 KB Output is correct
101 Correct 605 ms 14376 KB Output is correct
102 Correct 607 ms 13284 KB Output is correct
103 Correct 634 ms 37476 KB Output is correct
104 Correct 644 ms 37824 KB Output is correct
105 Correct 653 ms 37828 KB Output is correct
106 Correct 607 ms 19272 KB Output is correct
107 Correct 638 ms 14628 KB Output is correct
108 Correct 616 ms 13716 KB Output is correct
109 Correct 599 ms 13280 KB Output is correct