답안 #634711

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
634711 2022-08-24T17:40:25 Z hltk 디지털 회로 (IOI22_circuit) C++17
100 / 100
1224 ms 33684 KB
#include "circuit.h"

#include <iostream>
#include <numeric>
#include <algorithm>
#include <vector>

using namespace std;

const int M = 1'000'002'022;
using i64 = long long;

int add(int a, int b) {
  a += b;
  if (a >= M) a -= M;
  return a;
}

int mul(int a, int b) {
  return i64(a) * b % M;
}

struct Tree {
  vector<int> sum, flip_sum, tag;
  void init(int n) {
    sum.resize(n * 4);
    flip_sum.resize(n * 4);
    tag.resize(n * 4);
  }
  void change(int s, int l, int r, int k, int x) {
    if (r - l == 1) {
      sum[s] = x;
      return;
    }
    push(s);
    int m = (l + r) / 2;
    if (k < m) change(s * 2 + 0, l, m, k, x);
    else change(s * 2 + 1, m, r, k, x);
    sum[s] = add(sum[s * 2], sum[s * 2 + 1]);
    flip_sum[s] = add(flip_sum[s * 2], flip_sum[s * 2 + 1]);
  }
  void flip(int s, int l, int r, int ql, int qr) {
    if (r <= ql || qr <= l) return;
    if (ql <= l && r <= qr) return apply(s);
    push(s);
    int m = (l + r) / 2;
    flip(s * 2 + 0, l, m, ql, qr);
    flip(s * 2 + 1, m, r, ql, qr);
    sum[s] = add(sum[s * 2], sum[s * 2 + 1]);
    flip_sum[s] = add(flip_sum[s * 2], flip_sum[s * 2 + 1]);
  }
  void push(int s) {
    if (tag[s]) {
      apply(s * 2 + 0);
      apply(s * 2 + 1);
      tag[s] = 0;
    }
  }
  void apply(int s) {
    swap(flip_sum[s], sum[s]);
    tag[s] ^= 1;
  }
};

vector<vector<int>> children;
vector<int> a;
vector<int> dp;

int prod_dfs(int u) {
  int prod = children[u].empty() ? 1 : children[u].size();
  for (int v : children[u]) {
    prod = mul(prod, prod_dfs(v));
  }
  dp[u] = prod;
  return prod;
}

vector<int> oth_value;

void dfs(int u, int oth) {
  oth_value[u] = oth;
  vector<int> values{1};
  for (int v : children[u]) {
    values.push_back(dp[v]);
  }
  values.push_back(1);
  reverse(values.begin(), values.end());
  vector<int> rvalues = values;
  partial_sum(values.begin(), values.end(), values.begin(), mul);
  partial_sum(rvalues.rbegin(), rvalues.rend(), rvalues.rbegin(), mul);
  values.pop_back();
  for (int v : children[u]) {
    values.pop_back();
    int value = mul(oth, mul(values.back(), rvalues.back()));
    dfs(v, value);
    rvalues.pop_back();
  }
}

int n, m;

Tree seg;

void init(int n, int m, vector<int> p, vector<int> a) {
  children.resize(n + m);
  for (int i = 1; i < n + m; ++i) {
    children[p[i]].push_back(i);
  }
  dp.assign(n + m, 0);
  oth_value.assign(n + m, 0);
  prod_dfs(0);
  dfs(0, 1);
  ::n = n;
  ::m = m;
  ::a = a;
  seg.init(m);
  for (int i = 0; i < m; ++i) {
    seg.change(1, 0, m, i, oth_value[i + n]);
    if (a[i] == 0) {
      seg.flip(1, 0, m, i, i + 1);
    }
  }
}

int count_ways(int l, int r) {
  l -= n;
  r -= n;
  seg.flip(1, 0, m, l, r + 1);
  return seg.sum[1];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 ms 208 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 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 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 336 KB Output is correct
8 Correct 2 ms 336 KB Output is correct
9 Correct 2 ms 336 KB Output is correct
10 Correct 1 ms 592 KB Output is correct
11 Correct 1 ms 592 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 ms 208 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 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 208 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 336 KB Output is correct
16 Correct 2 ms 336 KB Output is correct
17 Correct 2 ms 336 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 2 ms 464 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 592 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 592 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 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 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 755 ms 5816 KB Output is correct
2 Correct 1000 ms 11504 KB Output is correct
3 Correct 981 ms 11304 KB Output is correct
4 Correct 936 ms 11352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 755 ms 5816 KB Output is correct
2 Correct 1000 ms 11504 KB Output is correct
3 Correct 981 ms 11304 KB Output is correct
4 Correct 936 ms 11352 KB Output is correct
5 Correct 887 ms 5788 KB Output is correct
6 Correct 1136 ms 11324 KB Output is correct
7 Correct 908 ms 11432 KB Output is correct
8 Correct 938 ms 11316 KB Output is correct
9 Correct 490 ms 624 KB Output is correct
10 Correct 629 ms 980 KB Output is correct
11 Correct 741 ms 980 KB Output is correct
12 Correct 1018 ms 976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 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 336 KB Output is correct
8 Correct 2 ms 336 KB Output is correct
9 Correct 2 ms 336 KB Output is correct
10 Correct 1 ms 592 KB Output is correct
11 Correct 1 ms 592 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 755 ms 5816 KB Output is correct
14 Correct 1000 ms 11504 KB Output is correct
15 Correct 981 ms 11304 KB Output is correct
16 Correct 936 ms 11352 KB Output is correct
17 Correct 887 ms 5788 KB Output is correct
18 Correct 1136 ms 11324 KB Output is correct
19 Correct 908 ms 11432 KB Output is correct
20 Correct 938 ms 11316 KB Output is correct
21 Correct 490 ms 624 KB Output is correct
22 Correct 629 ms 980 KB Output is correct
23 Correct 741 ms 980 KB Output is correct
24 Correct 1018 ms 976 KB Output is correct
25 Correct 1076 ms 16832 KB Output is correct
26 Correct 1004 ms 17072 KB Output is correct
27 Correct 1132 ms 17136 KB Output is correct
28 Correct 799 ms 17140 KB Output is correct
29 Correct 1058 ms 32412 KB Output is correct
30 Correct 985 ms 32328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 ms 208 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 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 208 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 336 KB Output is correct
16 Correct 2 ms 336 KB Output is correct
17 Correct 2 ms 336 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 2 ms 464 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 592 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 592 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 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 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 607 ms 772 KB Output is correct
44 Correct 934 ms 824 KB Output is correct
45 Correct 900 ms 812 KB Output is correct
46 Correct 775 ms 1136 KB Output is correct
47 Correct 842 ms 1140 KB Output is correct
48 Correct 948 ms 1136 KB Output is correct
49 Correct 953 ms 1132 KB Output is correct
50 Correct 885 ms 1144 KB Output is correct
51 Correct 686 ms 804 KB Output is correct
52 Correct 786 ms 800 KB Output is correct
53 Correct 850 ms 1616 KB Output is correct
54 Correct 892 ms 1132 KB Output is correct
55 Correct 950 ms 968 KB Output is correct
56 Correct 1020 ms 908 KB Output is correct
57 Correct 888 ms 796 KB Output is correct
58 Correct 959 ms 1840 KB Output is correct
59 Correct 786 ms 1988 KB Output is correct
60 Correct 772 ms 1880 KB Output is correct
61 Correct 952 ms 992 KB Output is correct
62 Correct 937 ms 780 KB Output is correct
63 Correct 973 ms 752 KB Output is correct
64 Correct 1008 ms 824 KB Output is correct
65 Correct 447 ms 628 KB Output is correct
66 Correct 854 ms 980 KB Output is correct
67 Correct 959 ms 980 KB Output is correct
68 Correct 860 ms 976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 ms 208 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 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 208 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 336 KB Output is correct
16 Correct 2 ms 336 KB Output is correct
17 Correct 2 ms 336 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 2 ms 464 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 592 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 592 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 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 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 755 ms 5816 KB Output is correct
44 Correct 1000 ms 11504 KB Output is correct
45 Correct 981 ms 11304 KB Output is correct
46 Correct 936 ms 11352 KB Output is correct
47 Correct 887 ms 5788 KB Output is correct
48 Correct 1136 ms 11324 KB Output is correct
49 Correct 908 ms 11432 KB Output is correct
50 Correct 938 ms 11316 KB Output is correct
51 Correct 490 ms 624 KB Output is correct
52 Correct 629 ms 980 KB Output is correct
53 Correct 741 ms 980 KB Output is correct
54 Correct 1018 ms 976 KB Output is correct
55 Correct 1076 ms 16832 KB Output is correct
56 Correct 1004 ms 17072 KB Output is correct
57 Correct 1132 ms 17136 KB Output is correct
58 Correct 799 ms 17140 KB Output is correct
59 Correct 1058 ms 32412 KB Output is correct
60 Correct 985 ms 32328 KB Output is correct
61 Correct 607 ms 772 KB Output is correct
62 Correct 934 ms 824 KB Output is correct
63 Correct 900 ms 812 KB Output is correct
64 Correct 775 ms 1136 KB Output is correct
65 Correct 842 ms 1140 KB Output is correct
66 Correct 948 ms 1136 KB Output is correct
67 Correct 953 ms 1132 KB Output is correct
68 Correct 885 ms 1144 KB Output is correct
69 Correct 686 ms 804 KB Output is correct
70 Correct 786 ms 800 KB Output is correct
71 Correct 850 ms 1616 KB Output is correct
72 Correct 892 ms 1132 KB Output is correct
73 Correct 950 ms 968 KB Output is correct
74 Correct 1020 ms 908 KB Output is correct
75 Correct 888 ms 796 KB Output is correct
76 Correct 959 ms 1840 KB Output is correct
77 Correct 786 ms 1988 KB Output is correct
78 Correct 772 ms 1880 KB Output is correct
79 Correct 952 ms 992 KB Output is correct
80 Correct 937 ms 780 KB Output is correct
81 Correct 973 ms 752 KB Output is correct
82 Correct 1008 ms 824 KB Output is correct
83 Correct 447 ms 628 KB Output is correct
84 Correct 854 ms 980 KB Output is correct
85 Correct 959 ms 980 KB Output is correct
86 Correct 860 ms 976 KB Output is correct
87 Correct 0 ms 208 KB Output is correct
88 Correct 609 ms 14916 KB Output is correct
89 Correct 1020 ms 10580 KB Output is correct
90 Correct 1030 ms 10660 KB Output is correct
91 Correct 1059 ms 17224 KB Output is correct
92 Correct 1224 ms 17224 KB Output is correct
93 Correct 1081 ms 17224 KB Output is correct
94 Correct 1111 ms 17248 KB Output is correct
95 Correct 922 ms 17144 KB Output is correct
96 Correct 836 ms 10420 KB Output is correct
97 Correct 894 ms 10372 KB Output is correct
98 Correct 877 ms 27676 KB Output is correct
99 Correct 802 ms 17136 KB Output is correct
100 Correct 988 ms 13608 KB Output is correct
101 Correct 1083 ms 12360 KB Output is correct
102 Correct 909 ms 10608 KB Output is correct
103 Correct 1039 ms 32284 KB Output is correct
104 Correct 1109 ms 33584 KB Output is correct
105 Correct 862 ms 33684 KB Output is correct
106 Correct 872 ms 13384 KB Output is correct
107 Correct 965 ms 9668 KB Output is correct
108 Correct 909 ms 10044 KB Output is correct
109 Correct 1004 ms 10716 KB Output is correct