Submission #739098

# Submission time Handle Problem Language Result Execution time Memory
739098 2023-05-09T23:39:10 Z math_rabbit_1028 Digital Circuit (IOI22_circuit) C++17
100 / 100
1148 ms 39224 KB
#include "circuit.h"

#include <vector>
#include <bits/stdc++.h>
using namespace std;

int n, m, onoff[101010], par[202020];
vector<int> adj[202020];

const long long MOD = 1000002022;
long long cnt[202020], wgt[202020], sum[101010];

void get_cnt(int v) {
  if (v < n) cnt[v] = adj[v].size();
  else cnt[v] = 1;
  for (int i = 0; i < adj[v].size(); i++) {
    int next = adj[v][i];
    get_cnt(next);
    cnt[v] = (cnt[v] * cnt[next]) % MOD;
  }
}

void get_wgt(int v, long long w) {
  if (v >= n) {
    wgt[v] = w;
    return;
  }
  vector<long long> prefix, suffix;
  for (int i = 0; i < adj[v].size(); i++) {
    prefix.push_back(cnt[adj[v][i]]);
    suffix.push_back(cnt[adj[v][i]]);
  }
  reverse(suffix.begin(), suffix.end());
  for (int i = 1; i < adj[v].size(); i++) {
    prefix[i] = (prefix[i] * prefix[i - 1]) % MOD;
    suffix[i] = (suffix[i] * suffix[i - 1]) % MOD;
  }
  for (int i = 0; i < adj[v].size(); i++) {
    int next = adj[v][i];
    long long t;
    if (adj[v].size() > 1) {
      if (i == 0) t = suffix[adj[v].size() - 2];
      else if (i == adj[v].size() - 1) t = prefix[adj[v].size() - 2];
      else t = (prefix[i - 1] * suffix[adj[v].size() - i - 2]) % MOD;
    }
    else t = 1;
    get_wgt(next, (w * t) % MOD);
  }
}

struct seg {
  long long tree[404040];
  int lazy[404040];
  vector<long long> arr;
  void init(int v, int st, int ed) {
    if (st == ed) {
      tree[v] = arr[st];
      lazy[v] = 0;
      return;
    }
    int mid = (st + ed) / 2;
    init(2 * v, st, mid);
    init(2 * v + 1, mid + 1, ed);
    tree[v] = (tree[2 * v] + tree[2 * v + 1]) % MOD;
  }
  void update(int v, int st, int ed, int gs, int ge) {
    if (lazy[v] % 2 == 1) {
      tree[v] = (sum[ed] - sum[st - 1] - tree[v] + 2 * MOD) % MOD;
      if (st != ed) {
        lazy[2 * v] += lazy[v];
        lazy[2 * v + 1] += lazy[v];
      }
      lazy[v] = 0;
    }
    if (gs > ed || ge < st) return;
    if (gs <= st && ed <= ge) {
      tree[v] = (sum[ed] - sum[st - 1] - tree[v] + 2 * MOD) % MOD;
      if (st != ed) {
        lazy[2 * v] += 1;
        lazy[2 * v + 1] += 1;
      }
      return;
    }
    int mid = (st + ed) / 2;
    update(2 * v, st, mid, gs, ge);
    update(2 * v + 1, mid + 1, ed, gs, ge);
    tree[v] = (tree[2 * v] + tree[2 * v + 1]) % MOD;
  }
  long long get(int v, int st, int ed, int gs, int ge) {
    if (lazy[v] % 2 == 1) {
      tree[v] = (sum[ed] - sum[st - 1] - tree[v] + 2 * MOD) % MOD;
      if (st != ed) {
        lazy[2 * v] += lazy[v];
        lazy[2 * v + 1] += lazy[v];
      }
      lazy[v] = 0;
    }
    if (gs > ed || ge < st) return 0;
    if (gs <= st && ed <= ge) return tree[v];
    int mid = (st + ed) / 2;
    return (get(2 * v, st, mid, gs, ge) + get(2 * v + 1, mid + 1, ed, gs, ge)) % MOD;
  }
} sumtree;

void init(int N, int M, std::vector<int> P, std::vector<int> A) {
  n = N;
  m = M;
  for (int i = 0; i < n + m; i++) par[i] = P[i];
  for (int i = 0; i < m; i++) onoff[i] = A[i];

  for (int i = 1; i < n + m; i++) {
    adj[par[i]].push_back(i);
  }
  get_cnt(0);
  get_wgt(0, 1);
  //for (int i = n; i < n + m; i++) cout << wgt[i] << "\n";
  for (int i = n; i < n + m; i++) sum[i] = (sum[i - 1] + wgt[i]) % MOD;

  for (int i = 0; i < n; i++) sumtree.arr.push_back(0);
  for (int i = n; i < n + m; i++) sumtree.arr.push_back(onoff[i - n] * wgt[i]);
  sumtree.init(1, n, n + m - 1);
}

int count_ways(int L, int R) {
  sumtree.update(1, n, n + m - 1, L, R);
  return (sumtree.get(1, n, n + m - 1, n, n + m - 1)) % MOD;
}

Compilation message

circuit.cpp: In function 'void get_cnt(int)':
circuit.cpp:16:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |   for (int i = 0; i < adj[v].size(); i++) {
      |                   ~~^~~~~~~~~~~~~~~
circuit.cpp: In function 'void get_wgt(int, long long int)':
circuit.cpp:29:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |   for (int i = 0; i < adj[v].size(); i++) {
      |                   ~~^~~~~~~~~~~~~~~
circuit.cpp:34:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |   for (int i = 1; i < adj[v].size(); i++) {
      |                   ~~^~~~~~~~~~~~~~~
circuit.cpp:38:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |   for (int i = 0; i < adj[v].size(); i++) {
      |                   ~~^~~~~~~~~~~~~~~
circuit.cpp:43:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |       else if (i == adj[v].size() - 1) t = prefix[adj[v].size() - 2];
      |                ~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5072 KB Output is correct
2 Correct 2 ms 5072 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 3 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5072 KB Output is correct
2 Correct 3 ms 5072 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 5200 KB Output is correct
7 Correct 3 ms 5200 KB Output is correct
8 Correct 3 ms 5200 KB Output is correct
9 Correct 3 ms 5200 KB Output is correct
10 Correct 3 ms 5328 KB Output is correct
11 Correct 3 ms 5328 KB Output is correct
12 Correct 3 ms 5200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5072 KB Output is correct
2 Correct 2 ms 5072 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 3 ms 5072 KB Output is correct
9 Correct 3 ms 5072 KB Output is correct
10 Correct 3 ms 5072 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 5200 KB Output is correct
15 Correct 3 ms 5200 KB Output is correct
16 Correct 3 ms 5200 KB Output is correct
17 Correct 3 ms 5200 KB Output is correct
18 Correct 3 ms 5328 KB Output is correct
19 Correct 3 ms 5328 KB Output is correct
20 Correct 3 ms 5200 KB Output is correct
21 Correct 4 ms 5200 KB Output is correct
22 Correct 4 ms 5200 KB Output is correct
23 Correct 4 ms 5208 KB Output is correct
24 Correct 3 ms 5200 KB Output is correct
25 Correct 5 ms 5200 KB Output is correct
26 Correct 3 ms 5200 KB Output is correct
27 Correct 3 ms 5200 KB Output is correct
28 Correct 3 ms 5200 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 5328 KB Output is correct
32 Correct 3 ms 5200 KB Output is correct
33 Correct 3 ms 5200 KB Output is correct
34 Correct 3 ms 5200 KB Output is correct
35 Correct 3 ms 5072 KB Output is correct
36 Correct 3 ms 5328 KB Output is correct
37 Correct 3 ms 5328 KB Output is correct
38 Correct 3 ms 5328 KB Output is correct
39 Correct 3 ms 5200 KB Output is correct
40 Correct 3 ms 5200 KB Output is correct
41 Correct 3 ms 5200 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 703 ms 9548 KB Output is correct
2 Correct 865 ms 13892 KB Output is correct
3 Correct 952 ms 13872 KB Output is correct
4 Correct 960 ms 13888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 703 ms 9548 KB Output is correct
2 Correct 865 ms 13892 KB Output is correct
3 Correct 952 ms 13872 KB Output is correct
4 Correct 960 ms 13888 KB Output is correct
5 Correct 601 ms 9532 KB Output is correct
6 Correct 964 ms 13864 KB Output is correct
7 Correct 962 ms 13828 KB Output is correct
8 Correct 873 ms 13996 KB Output is correct
9 Correct 458 ms 5328 KB Output is correct
10 Correct 914 ms 5696 KB Output is correct
11 Correct 786 ms 5712 KB Output is correct
12 Correct 555 ms 5700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5072 KB Output is correct
2 Correct 3 ms 5072 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 5200 KB Output is correct
7 Correct 3 ms 5200 KB Output is correct
8 Correct 3 ms 5200 KB Output is correct
9 Correct 3 ms 5200 KB Output is correct
10 Correct 3 ms 5328 KB Output is correct
11 Correct 3 ms 5328 KB Output is correct
12 Correct 3 ms 5200 KB Output is correct
13 Correct 703 ms 9548 KB Output is correct
14 Correct 865 ms 13892 KB Output is correct
15 Correct 952 ms 13872 KB Output is correct
16 Correct 960 ms 13888 KB Output is correct
17 Correct 601 ms 9532 KB Output is correct
18 Correct 964 ms 13864 KB Output is correct
19 Correct 962 ms 13828 KB Output is correct
20 Correct 873 ms 13996 KB Output is correct
21 Correct 458 ms 5328 KB Output is correct
22 Correct 914 ms 5696 KB Output is correct
23 Correct 786 ms 5712 KB Output is correct
24 Correct 555 ms 5700 KB Output is correct
25 Correct 945 ms 19140 KB Output is correct
26 Correct 860 ms 19292 KB Output is correct
27 Correct 887 ms 19380 KB Output is correct
28 Correct 838 ms 19260 KB Output is correct
29 Correct 975 ms 37960 KB Output is correct
30 Correct 912 ms 37932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5072 KB Output is correct
2 Correct 2 ms 5072 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 3 ms 5072 KB Output is correct
9 Correct 3 ms 5072 KB Output is correct
10 Correct 3 ms 5072 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 5200 KB Output is correct
15 Correct 3 ms 5200 KB Output is correct
16 Correct 3 ms 5200 KB Output is correct
17 Correct 3 ms 5200 KB Output is correct
18 Correct 3 ms 5328 KB Output is correct
19 Correct 3 ms 5328 KB Output is correct
20 Correct 3 ms 5200 KB Output is correct
21 Correct 4 ms 5200 KB Output is correct
22 Correct 4 ms 5200 KB Output is correct
23 Correct 4 ms 5208 KB Output is correct
24 Correct 3 ms 5200 KB Output is correct
25 Correct 5 ms 5200 KB Output is correct
26 Correct 3 ms 5200 KB Output is correct
27 Correct 3 ms 5200 KB Output is correct
28 Correct 3 ms 5200 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 5328 KB Output is correct
32 Correct 3 ms 5200 KB Output is correct
33 Correct 3 ms 5200 KB Output is correct
34 Correct 3 ms 5200 KB Output is correct
35 Correct 3 ms 5072 KB Output is correct
36 Correct 3 ms 5328 KB Output is correct
37 Correct 3 ms 5328 KB Output is correct
38 Correct 3 ms 5328 KB Output is correct
39 Correct 3 ms 5200 KB Output is correct
40 Correct 3 ms 5200 KB Output is correct
41 Correct 3 ms 5200 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
43 Correct 432 ms 5584 KB Output is correct
44 Correct 896 ms 5584 KB Output is correct
45 Correct 760 ms 5596 KB Output is correct
46 Correct 930 ms 5968 KB Output is correct
47 Correct 873 ms 5968 KB Output is correct
48 Correct 775 ms 5968 KB Output is correct
49 Correct 967 ms 5968 KB Output is correct
50 Correct 915 ms 5968 KB Output is correct
51 Correct 762 ms 5788 KB Output is correct
52 Correct 786 ms 5668 KB Output is correct
53 Correct 857 ms 6352 KB Output is correct
54 Correct 893 ms 5956 KB Output is correct
55 Correct 960 ms 5756 KB Output is correct
56 Correct 817 ms 5736 KB Output is correct
57 Correct 523 ms 5704 KB Output is correct
58 Correct 792 ms 6736 KB Output is correct
59 Correct 801 ms 6820 KB Output is correct
60 Correct 671 ms 6820 KB Output is correct
61 Correct 728 ms 5820 KB Output is correct
62 Correct 805 ms 5556 KB Output is correct
63 Correct 878 ms 5540 KB Output is correct
64 Correct 817 ms 5648 KB Output is correct
65 Correct 475 ms 5384 KB Output is correct
66 Correct 648 ms 5584 KB Output is correct
67 Correct 897 ms 5704 KB Output is correct
68 Correct 770 ms 5692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5072 KB Output is correct
2 Correct 2 ms 5072 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 3 ms 5072 KB Output is correct
9 Correct 3 ms 5072 KB Output is correct
10 Correct 3 ms 5072 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 5200 KB Output is correct
15 Correct 3 ms 5200 KB Output is correct
16 Correct 3 ms 5200 KB Output is correct
17 Correct 3 ms 5200 KB Output is correct
18 Correct 3 ms 5328 KB Output is correct
19 Correct 3 ms 5328 KB Output is correct
20 Correct 3 ms 5200 KB Output is correct
21 Correct 4 ms 5200 KB Output is correct
22 Correct 4 ms 5200 KB Output is correct
23 Correct 4 ms 5208 KB Output is correct
24 Correct 3 ms 5200 KB Output is correct
25 Correct 5 ms 5200 KB Output is correct
26 Correct 3 ms 5200 KB Output is correct
27 Correct 3 ms 5200 KB Output is correct
28 Correct 3 ms 5200 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 5328 KB Output is correct
32 Correct 3 ms 5200 KB Output is correct
33 Correct 3 ms 5200 KB Output is correct
34 Correct 3 ms 5200 KB Output is correct
35 Correct 3 ms 5072 KB Output is correct
36 Correct 3 ms 5328 KB Output is correct
37 Correct 3 ms 5328 KB Output is correct
38 Correct 3 ms 5328 KB Output is correct
39 Correct 3 ms 5200 KB Output is correct
40 Correct 3 ms 5200 KB Output is correct
41 Correct 3 ms 5200 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
43 Correct 703 ms 9548 KB Output is correct
44 Correct 865 ms 13892 KB Output is correct
45 Correct 952 ms 13872 KB Output is correct
46 Correct 960 ms 13888 KB Output is correct
47 Correct 601 ms 9532 KB Output is correct
48 Correct 964 ms 13864 KB Output is correct
49 Correct 962 ms 13828 KB Output is correct
50 Correct 873 ms 13996 KB Output is correct
51 Correct 458 ms 5328 KB Output is correct
52 Correct 914 ms 5696 KB Output is correct
53 Correct 786 ms 5712 KB Output is correct
54 Correct 555 ms 5700 KB Output is correct
55 Correct 945 ms 19140 KB Output is correct
56 Correct 860 ms 19292 KB Output is correct
57 Correct 887 ms 19380 KB Output is correct
58 Correct 838 ms 19260 KB Output is correct
59 Correct 975 ms 37960 KB Output is correct
60 Correct 912 ms 37932 KB Output is correct
61 Correct 432 ms 5584 KB Output is correct
62 Correct 896 ms 5584 KB Output is correct
63 Correct 760 ms 5596 KB Output is correct
64 Correct 930 ms 5968 KB Output is correct
65 Correct 873 ms 5968 KB Output is correct
66 Correct 775 ms 5968 KB Output is correct
67 Correct 967 ms 5968 KB Output is correct
68 Correct 915 ms 5968 KB Output is correct
69 Correct 762 ms 5788 KB Output is correct
70 Correct 786 ms 5668 KB Output is correct
71 Correct 857 ms 6352 KB Output is correct
72 Correct 893 ms 5956 KB Output is correct
73 Correct 960 ms 5756 KB Output is correct
74 Correct 817 ms 5736 KB Output is correct
75 Correct 523 ms 5704 KB Output is correct
76 Correct 792 ms 6736 KB Output is correct
77 Correct 801 ms 6820 KB Output is correct
78 Correct 671 ms 6820 KB Output is correct
79 Correct 728 ms 5820 KB Output is correct
80 Correct 805 ms 5556 KB Output is correct
81 Correct 878 ms 5540 KB Output is correct
82 Correct 817 ms 5648 KB Output is correct
83 Correct 475 ms 5384 KB Output is correct
84 Correct 648 ms 5584 KB Output is correct
85 Correct 897 ms 5704 KB Output is correct
86 Correct 770 ms 5692 KB Output is correct
87 Correct 3 ms 5072 KB Output is correct
88 Correct 500 ms 18080 KB Output is correct
89 Correct 946 ms 13932 KB Output is correct
90 Correct 870 ms 13696 KB Output is correct
91 Correct 922 ms 19508 KB Output is correct
92 Correct 1058 ms 19424 KB Output is correct
93 Correct 1101 ms 19388 KB Output is correct
94 Correct 971 ms 19432 KB Output is correct
95 Correct 815 ms 19380 KB Output is correct
96 Correct 859 ms 14768 KB Output is correct
97 Correct 784 ms 14696 KB Output is correct
98 Correct 971 ms 30460 KB Output is correct
99 Correct 1148 ms 19268 KB Output is correct
100 Correct 955 ms 16392 KB Output is correct
101 Correct 963 ms 15424 KB Output is correct
102 Correct 797 ms 13892 KB Output is correct
103 Correct 940 ms 37972 KB Output is correct
104 Correct 895 ms 39212 KB Output is correct
105 Correct 1057 ms 39224 KB Output is correct
106 Correct 841 ms 17848 KB Output is correct
107 Correct 800 ms 14036 KB Output is correct
108 Correct 930 ms 14100 KB Output is correct
109 Correct 986 ms 14012 KB Output is correct