답안 #1109747

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1109747 2024-11-07T13:41:44 Z tibinyte 디지털 회로 (IOI22_circuit) C++17
100 / 100
2233 ms 43852 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

const string asafaciunstringsus = R""""(
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
)"""";

typedef long long ll;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int random(int st, int dr)
{
  uniform_int_distribution<int> dist(st, dr);
  return dist(rng);
}

template <typename t>
using ordered_set = tree<t, null_type, less<t>, rb_tree_tag, tree_order_statistics_node_update>;

const int mod = 1000002022;
struct Mint
{
  int val;
  Mint(int x = 0)
  {
    val = (x % mod + mod) % mod;
  }
  Mint(long long x)
  {
    val = x % mod;
  }
  Mint operator+(Mint oth)
  {
    return val + oth.val;
  }
  Mint operator-(Mint oth)
  {
    return val - oth.val + mod;
  }
  Mint operator*(Mint oth)
  {
    return 1ll * val * oth.val;
  }
  void operator+=(Mint oth)
  {
    val = (*this + oth).val;
  }
  void operator-=(Mint oth)
  {
    val = (*this - oth).val;
  }
  void operator*=(Mint oth)
  {
    val = (*this * oth).val;
  }
};

Mint powmod(int a, int b)
{
  if (b == 0)
  {
    return 1;
  }
  if (b % 2 == 1)
  {
    return powmod(a, b - 1) * a;
  }
  Mint p = powmod(a, b / 2);
  return p * p;
}

/*
                 .___                 __                 __           .__
  ____  ____   __| _/____     _______/  |______ ________/  |_  ______ |  |__   ___________   ____
_/ ___\/  _ \ / __ |/ __ \   /  ___/\   __\__  \\_  __ \   __\/  ___/ |  |  \_/ __ \_  __ \_/ __ \
\  \__(  <_> ) /_/ \  ___/   \___ \  |  |  / __ \|  | \/|  |  \___ \  |   Y  \  ___/|  | \/\  ___/
 \___  >____/\____ |\___  > /____  > |__| (____  /__|   |__| /____  > |___|  /\___  >__|    \___  >
     \/           \/    \/       \/            \/                 \/       \/     \/            \/
*/

struct aint
{
  vector<Mint> a;
  vector<Mint> sp;
  vector<int> lazy;
  aint() {}
  aint(int n, vector<Mint> coef)
  {
    a.resize(4 * n);
    lazy.resize(4 * n);
    sp.push_back(0);
    for (int i = 0; i < n; ++i)
    {
      sp.push_back(sp.back() + coef[i]);
    }
  }
  void prop(int node, int left, int right)
  {
    if (lazy[node])
    {
      a[node] = sp[right] - sp[left - 1] - a[node];
      if (left != right)
      {
        lazy[2 * node] ^= 1;
        lazy[2 * node + 1] ^= 1;
      }
      lazy[node] = 0;
    }
  }
  void update(int node, int left, int right, int st, int dr)
  {
    prop(node, left, right);
    if (right < st || left > dr)
    {
      return;
    }
    if (st <= left && dr >= right)
    {
      lazy[node] ^= 1;
      prop(node, left, right);
      return;
    }
    int mid = (left + right) / 2;
    update(2 * node, left, mid, st, dr);
    update(2 * node + 1, mid + 1, right, st, dr);
    a[node] = a[2 * node] + a[2 * node + 1];
  }
};

int n, m;
aint lesgo;

void init(int N, int M, std::vector<int> P, std::vector<int> A)
{
  n = N + M;
  m = M;
  vector<Mint> coef(m);
  vector<vector<int>> g(n);
  for (int i = 1; i < n; ++i)
  {
    g[P[i]].push_back(i);
  }

  vector<Mint> prod(n);

  function<void(int)> dfs_prod = [&](int node)
  {
    if (g[node].empty())
    {
      prod[node] = 1;
      return;
    }
    prod[node] = (int)g[node].size();
    for (auto it : g[node])
    {
      dfs_prod(it);
      prod[node] *= prod[it];
    }
  };
  dfs_prod(0);

  vector<Mint> ans(n, 1);

  function<void(int)> dfs = [&](int node)
  {
    if (g[node].empty())
    {
      coef[node - N] = ans[node];
      return;
    }

    vector<Mint> children;

    for (auto it : g[node])
    {
      children.push_back(prod[it]);
    }

    vector<Mint> prefprod(children.size()), sufprod(children.size());

    prefprod[0] = children[0];
    sufprod.back() = children.back();
    for (int i = 1; i < children.size(); ++i)
    {
      prefprod[i] = prefprod[i - 1] * children[i];
    }
    for (int i = children.size() - 2; i >= 0; --i)
    {
      sufprod[i] = sufprod[i + 1] * children[i];
    }

    for (int i = 0; i < children.size(); ++i)
    {
      int it = g[node][i];

      ans[it] = ans[node];
      if (i != 0)
      {
        ans[it] *= prefprod[i - 1];
      }
      if (i != children.size() - 1)
      {
        ans[it] *= sufprod[i + 1];
      }

      dfs(it);
    }
  };
  dfs(0);
  lesgo = aint(m, coef);
  for (int i = 0; i < m; ++i)
  {
    if (A[i])
    {
      lesgo.update(1, 1, m, i + 1, i + 1);
    }
  }
}

int count_ways(int L, int R)
{
  L -= n - m - 1;
  R -= n - m - 1;

  lesgo.update(1, 1, m, L, R);
  return lesgo.a[1].val;
}

/*
I cannot take this anymore
Saying everything I've said before
All these words, they make no sense
I find bliss in ignorance
Less I hear, the less you say
You'll find that out anyway
Just like before

Everything you say to me
(Takes me one step closer to the edge)
(And I'm about to break)
I need a little room to breathe
(Cause I'm one step closer to the edge)
(I'm about to break)
*/

Compilation message

circuit.cpp: In lambda function:
circuit.cpp:191:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Mint>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  191 |     for (int i = 1; i < children.size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~~~
circuit.cpp:200:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Mint>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  200 |     for (int i = 0; i < children.size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~~~
circuit.cpp:209:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Mint>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  209 |       if (i != children.size() - 1)
      |           ~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 592 KB Output is correct
8 Correct 2 ms 592 KB Output is correct
9 Correct 2 ms 592 KB Output is correct
10 Correct 2 ms 848 KB Output is correct
11 Correct 2 ms 848 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 592 KB Output is correct
16 Correct 2 ms 592 KB Output is correct
17 Correct 2 ms 592 KB Output is correct
18 Correct 2 ms 848 KB Output is correct
19 Correct 2 ms 848 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 2 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 592 KB Output is correct
25 Correct 2 ms 592 KB Output is correct
26 Correct 2 ms 592 KB Output is correct
27 Correct 1 ms 592 KB Output is correct
28 Correct 1 ms 592 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 592 KB Output is correct
32 Correct 2 ms 592 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 2 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 848 KB Output is correct
37 Correct 2 ms 848 KB Output is correct
38 Correct 2 ms 848 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 504 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1361 ms 5968 KB Output is correct
2 Correct 2013 ms 11212 KB Output is correct
3 Correct 1990 ms 11352 KB Output is correct
4 Correct 2025 ms 11212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1361 ms 5968 KB Output is correct
2 Correct 2013 ms 11212 KB Output is correct
3 Correct 1990 ms 11352 KB Output is correct
4 Correct 2025 ms 11212 KB Output is correct
5 Correct 1661 ms 5968 KB Output is correct
6 Correct 2116 ms 11212 KB Output is correct
7 Correct 2061 ms 11320 KB Output is correct
8 Correct 2037 ms 11212 KB Output is correct
9 Correct 1024 ms 592 KB Output is correct
10 Correct 1924 ms 1104 KB Output is correct
11 Correct 1997 ms 1104 KB Output is correct
12 Correct 1907 ms 1104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 592 KB Output is correct
8 Correct 2 ms 592 KB Output is correct
9 Correct 2 ms 592 KB Output is correct
10 Correct 2 ms 848 KB Output is correct
11 Correct 2 ms 848 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1361 ms 5968 KB Output is correct
14 Correct 2013 ms 11212 KB Output is correct
15 Correct 1990 ms 11352 KB Output is correct
16 Correct 2025 ms 11212 KB Output is correct
17 Correct 1661 ms 5968 KB Output is correct
18 Correct 2116 ms 11212 KB Output is correct
19 Correct 2061 ms 11320 KB Output is correct
20 Correct 2037 ms 11212 KB Output is correct
21 Correct 1024 ms 592 KB Output is correct
22 Correct 1924 ms 1104 KB Output is correct
23 Correct 1997 ms 1104 KB Output is correct
24 Correct 1907 ms 1104 KB Output is correct
25 Correct 2073 ms 16324 KB Output is correct
26 Correct 2111 ms 16540 KB Output is correct
27 Correct 2140 ms 16584 KB Output is correct
28 Correct 1574 ms 16584 KB Output is correct
29 Correct 2079 ms 42076 KB Output is correct
30 Correct 1977 ms 42204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 592 KB Output is correct
16 Correct 2 ms 592 KB Output is correct
17 Correct 2 ms 592 KB Output is correct
18 Correct 2 ms 848 KB Output is correct
19 Correct 2 ms 848 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 2 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 592 KB Output is correct
25 Correct 2 ms 592 KB Output is correct
26 Correct 2 ms 592 KB Output is correct
27 Correct 1 ms 592 KB Output is correct
28 Correct 1 ms 592 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 592 KB Output is correct
32 Correct 2 ms 592 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 2 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 848 KB Output is correct
37 Correct 2 ms 848 KB Output is correct
38 Correct 2 ms 848 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 504 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 1415 ms 848 KB Output is correct
44 Correct 1962 ms 848 KB Output is correct
45 Correct 1901 ms 848 KB Output is correct
46 Correct 1932 ms 1104 KB Output is correct
47 Correct 1942 ms 1104 KB Output is correct
48 Correct 1996 ms 1104 KB Output is correct
49 Correct 1971 ms 1104 KB Output is correct
50 Correct 1929 ms 1104 KB Output is correct
51 Correct 2017 ms 940 KB Output is correct
52 Correct 1942 ms 952 KB Output is correct
53 Correct 1832 ms 2128 KB Output is correct
54 Correct 2001 ms 1104 KB Output is correct
55 Correct 2029 ms 848 KB Output is correct
56 Correct 1995 ms 888 KB Output is correct
57 Correct 2057 ms 944 KB Output is correct
58 Correct 2045 ms 2392 KB Output is correct
59 Correct 2084 ms 2384 KB Output is correct
60 Correct 1986 ms 2384 KB Output is correct
61 Correct 1961 ms 1104 KB Output is correct
62 Correct 1978 ms 848 KB Output is correct
63 Correct 1924 ms 848 KB Output is correct
64 Correct 2015 ms 964 KB Output is correct
65 Correct 951 ms 592 KB Output is correct
66 Correct 2016 ms 1104 KB Output is correct
67 Correct 1982 ms 1104 KB Output is correct
68 Correct 2019 ms 1104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 592 KB Output is correct
16 Correct 2 ms 592 KB Output is correct
17 Correct 2 ms 592 KB Output is correct
18 Correct 2 ms 848 KB Output is correct
19 Correct 2 ms 848 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 2 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 592 KB Output is correct
25 Correct 2 ms 592 KB Output is correct
26 Correct 2 ms 592 KB Output is correct
27 Correct 1 ms 592 KB Output is correct
28 Correct 1 ms 592 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 592 KB Output is correct
32 Correct 2 ms 592 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 2 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 848 KB Output is correct
37 Correct 2 ms 848 KB Output is correct
38 Correct 2 ms 848 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 504 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 1361 ms 5968 KB Output is correct
44 Correct 2013 ms 11212 KB Output is correct
45 Correct 1990 ms 11352 KB Output is correct
46 Correct 2025 ms 11212 KB Output is correct
47 Correct 1661 ms 5968 KB Output is correct
48 Correct 2116 ms 11212 KB Output is correct
49 Correct 2061 ms 11320 KB Output is correct
50 Correct 2037 ms 11212 KB Output is correct
51 Correct 1024 ms 592 KB Output is correct
52 Correct 1924 ms 1104 KB Output is correct
53 Correct 1997 ms 1104 KB Output is correct
54 Correct 1907 ms 1104 KB Output is correct
55 Correct 2073 ms 16324 KB Output is correct
56 Correct 2111 ms 16540 KB Output is correct
57 Correct 2140 ms 16584 KB Output is correct
58 Correct 1574 ms 16584 KB Output is correct
59 Correct 2079 ms 42076 KB Output is correct
60 Correct 1977 ms 42204 KB Output is correct
61 Correct 1415 ms 848 KB Output is correct
62 Correct 1962 ms 848 KB Output is correct
63 Correct 1901 ms 848 KB Output is correct
64 Correct 1932 ms 1104 KB Output is correct
65 Correct 1942 ms 1104 KB Output is correct
66 Correct 1996 ms 1104 KB Output is correct
67 Correct 1971 ms 1104 KB Output is correct
68 Correct 1929 ms 1104 KB Output is correct
69 Correct 2017 ms 940 KB Output is correct
70 Correct 1942 ms 952 KB Output is correct
71 Correct 1832 ms 2128 KB Output is correct
72 Correct 2001 ms 1104 KB Output is correct
73 Correct 2029 ms 848 KB Output is correct
74 Correct 1995 ms 888 KB Output is correct
75 Correct 2057 ms 944 KB Output is correct
76 Correct 2045 ms 2392 KB Output is correct
77 Correct 2084 ms 2384 KB Output is correct
78 Correct 1986 ms 2384 KB Output is correct
79 Correct 1961 ms 1104 KB Output is correct
80 Correct 1978 ms 848 KB Output is correct
81 Correct 1924 ms 848 KB Output is correct
82 Correct 2015 ms 964 KB Output is correct
83 Correct 951 ms 592 KB Output is correct
84 Correct 2016 ms 1104 KB Output is correct
85 Correct 1982 ms 1104 KB Output is correct
86 Correct 2019 ms 1104 KB Output is correct
87 Correct 1 ms 336 KB Output is correct
88 Correct 1214 ms 14796 KB Output is correct
89 Correct 2102 ms 10568 KB Output is correct
90 Correct 2041 ms 10452 KB Output is correct
91 Correct 2095 ms 16840 KB Output is correct
92 Correct 2110 ms 16844 KB Output is correct
93 Correct 2058 ms 16716 KB Output is correct
94 Correct 2175 ms 16836 KB Output is correct
95 Correct 2155 ms 16840 KB Output is correct
96 Correct 2008 ms 10164 KB Output is correct
97 Correct 2120 ms 10164 KB Output is correct
98 Correct 1921 ms 37192 KB Output is correct
99 Correct 2128 ms 16584 KB Output is correct
100 Correct 2149 ms 13004 KB Output is correct
101 Correct 2118 ms 11716 KB Output is correct
102 Correct 2110 ms 10184 KB Output is correct
103 Correct 2233 ms 42056 KB Output is correct
104 Correct 2204 ms 43844 KB Output is correct
105 Correct 2125 ms 43852 KB Output is correct
106 Correct 2175 ms 14624 KB Output is correct
107 Correct 2175 ms 9676 KB Output is correct
108 Correct 2134 ms 9940 KB Output is correct
109 Correct 2182 ms 10188 KB Output is correct