답안 #1057362

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1057362 2024-08-13T18:13:56 Z spacewalker 디지털 회로 (IOI22_circuit) C++17
100 / 100
713 ms 26528 KB
#include "circuit.h"
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
constexpr ll MOD = 1'000'002'022;

template<class T>
ostream& operator<< (ostream &os, const vector<T> &arr) {
  os << "[";
  for (const T &v : arr) os << v << ", ";
  return os << "]";
}

vector<ll> product_except(vector<ll> nums) {
  int n = nums.size();
  vector<ll> pref = nums, suff = nums;
  for (int i = 1; i < n; ++i) pref[i] = (pref[i-1] * nums[i]) % MOD;
  for (int i = n - 2; i >= 0; --i) suff[i] = suff[i+1] * nums[i] % MOD;
  vector<ll> ans(n);
  for (int i = 0; i < n; ++i) ans[i] = (i > 0 ? pref[i-1] : 1) * (i + 1 < n ? suff[i+1] : 1) % MOD;
  return ans;
}

vector<ll> get_weights(vector<int> P, int M) {
  int N = (int)P.size() - M;
  vector<vector<int>> children(N + M);
  for (int i = 1; i < N + M; ++i) children[P[i]].push_back(i);
  vector<ll> assign_ways(N + M, 1);
  for (int i = N - 1; i >= 0; --i) {
    assign_ways[i] = children[i].size();
    for (int ch : children[i]) assign_ways[i] = (assign_ways[i] * assign_ways[ch]) % MOD;
  }
  vector<ll> ans(N + M, 1);
  for (int i = 0; i < N; ++i) {
    vector<ll> ways_children(children[i].size());
    for (int j = 0; j < children[i].size(); ++j) ways_children[j] = assign_ways[children[i][j]];
    vector<ll> ways_except = product_except(ways_children);
    for (int j = 0; j < children[i].size(); ++j) ans[children[i][j]] = (ans[children[i][j]] * ways_except[j] % MOD * ans[i]) % MOD;
  }
  return vector(begin(ans) + N, end(ans));
}

struct CircuitTree {
  ll total, totalFlip;
  int wt, al, ar;
  bool lazy;
  unique_ptr<CircuitTree> left, right;
  void combine() {
    total = (left->total + right->total) % MOD;
    totalFlip = (left->totalFlip + right->totalFlip) % MOD;
  }
  void propagate() {
    if (lazy) {
      swap(total, totalFlip);
      if (left) {
        left->lazy = !left->lazy;
        right->lazy = !right->lazy;
      }
      lazy = false;
    }
  }
  CircuitTree() {}
  CircuitTree(int i, int j, const vector<ll> &weights, const vector<int> &init) : total(0), totalFlip(0), al(i), ar(j), lazy(false) {
    if (i == j) {
      wt = totalFlip = weights[i];
      if (init[i]) swap(total, totalFlip);
    } else {
      int k = (i + j) / 2;
      left = make_unique<CircuitTree>(i, k, weights, init);
      right = make_unique<CircuitTree>(k + 1, j, weights, init);
      combine();
      // cerr << "[" << i << ", " << j << "] total " << total << " " << totalFlip << endl;
    }
  }
  void flip(int i, int j) {
    propagate();
    if (j < al || ar < i) return;
    if (i <= al && ar <= j) {
      lazy = !lazy; propagate();
    } else {
      left->flip(i, j); right->flip(i, j);
      combine();
    }
  }
  ll sum() {
    propagate();
    return total;
  }
};

struct DumbCircuitTree {
  vector<int> p, a;
  int n, m;
  vector<vector<int>> children;
  DumbCircuitTree() {}
  DumbCircuitTree(vector<int> p, vector<int> a) : p(p), a(a), n((int)p.size() - (int)a.size()), m(a.size()), children(n + m) {
    for (int i = n + m - 1; i > 0; --i) children[p[i]].push_back(i);
  }
  void flip(int L, int R) {
    for (int j = L; j <= R; ++j) a[j] = 1 - a[j];
  }
  bool check_state(vector<int> &thresholds) {
    vector<int> st(n + m);
    for (int i = 0; i < m; ++i) st[i+n] = a[i];
    for (int i = n-1; i >= 0; --i) {
      int active = 0;
      for (int ch : children[i]) if (st[ch] == 1) ++active;
      if (active >= thresholds[i]) st[i] = 1;
    }
    // cerr << thresholds << " " << st << endl;
    return st[0] == 1;
  }
  int _count_ways(vector<int> &thresholds, int i) {
    if (i == n) return check_state(thresholds) ? 1 : 0;
    else {
      int ans = 0;
      for (int ct = 1; ct <= children[i].size(); ++ct) {
        thresholds[i] = ct;
        ans += _count_ways(thresholds, i + 1);
      }
      return ans;
    }
  }
  int count_ways() {
    // cerr << "got ct " << endl;
    vector<int> thresholds(n, 1);
    return _count_ways(thresholds, 0);
  }
};

CircuitTree ct;
DumbCircuitTree dct;
int N;

void init(int _N, int M, std::vector<int> P, std::vector<int> A) {
  N = _N;
  vector<ll> weights = get_weights(P, M); 
  ct = CircuitTree(0, M - 1, weights, A);
  dct = DumbCircuitTree(P, A);
}

int count_ways(int L, int R) {
  ct.flip(L - N, R - N);
  // dct.flip(L - N, R - N);
  return ct.sum();
  // assert(ct.sum() == dct.count_ways());
  // return dct.count_ways();
}

Compilation message

circuit.cpp: In function 'std::vector<long long int> get_weights(std::vector<int>, int)':
circuit.cpp:37:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     for (int j = 0; j < children[i].size(); ++j) ways_children[j] = assign_ways[children[i][j]];
      |                     ~~^~~~~~~~~~~~~~~~~~~~
circuit.cpp:39:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     for (int j = 0; j < children[i].size(); ++j) ans[children[i][j]] = (ans[children[i][j]] * ways_except[j] % MOD * ans[i]) % MOD;
      |                     ~~^~~~~~~~~~~~~~~~~~~~
circuit.cpp: In member function 'int DumbCircuitTree::_count_ways(std::vector<int>&, int)':
circuit.cpp:118:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |       for (int ct = 1; ct <= children[i].size(); ++ct) {
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 0 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 0 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 600 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 0 ms 600 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 0 ms 600 KB Output is correct
30 Correct 1 ms 600 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 600 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 0 ms 600 KB Output is correct
42 Correct 0 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 392 ms 8780 KB Output is correct
2 Correct 558 ms 17228 KB Output is correct
3 Correct 600 ms 17220 KB Output is correct
4 Correct 599 ms 17228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 392 ms 8780 KB Output is correct
2 Correct 558 ms 17228 KB Output is correct
3 Correct 600 ms 17220 KB Output is correct
4 Correct 599 ms 17228 KB Output is correct
5 Correct 476 ms 8780 KB Output is correct
6 Correct 643 ms 17220 KB Output is correct
7 Correct 638 ms 17220 KB Output is correct
8 Correct 612 ms 17228 KB Output is correct
9 Correct 276 ms 856 KB Output is correct
10 Correct 573 ms 1432 KB Output is correct
11 Correct 586 ms 1432 KB Output is correct
12 Correct 552 ms 1432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 392 ms 8780 KB Output is correct
14 Correct 558 ms 17228 KB Output is correct
15 Correct 600 ms 17220 KB Output is correct
16 Correct 599 ms 17228 KB Output is correct
17 Correct 476 ms 8780 KB Output is correct
18 Correct 643 ms 17220 KB Output is correct
19 Correct 638 ms 17220 KB Output is correct
20 Correct 612 ms 17228 KB Output is correct
21 Correct 276 ms 856 KB Output is correct
22 Correct 573 ms 1432 KB Output is correct
23 Correct 586 ms 1432 KB Output is correct
24 Correct 552 ms 1432 KB Output is correct
25 Correct 608 ms 25680 KB Output is correct
26 Correct 713 ms 26040 KB Output is correct
27 Correct 637 ms 26040 KB Output is correct
28 Correct 497 ms 26040 KB Output is correct
29 Correct 662 ms 26040 KB Output is correct
30 Correct 655 ms 26040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 0 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 600 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 0 ms 600 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 0 ms 600 KB Output is correct
30 Correct 1 ms 600 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 600 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 0 ms 600 KB Output is correct
42 Correct 0 ms 600 KB Output is correct
43 Correct 445 ms 976 KB Output is correct
44 Correct 574 ms 956 KB Output is correct
45 Correct 578 ms 1220 KB Output is correct
46 Correct 489 ms 1644 KB Output is correct
47 Correct 548 ms 1644 KB Output is correct
48 Correct 562 ms 1644 KB Output is correct
49 Correct 529 ms 1644 KB Output is correct
50 Correct 537 ms 1644 KB Output is correct
51 Correct 581 ms 1300 KB Output is correct
52 Correct 605 ms 1300 KB Output is correct
53 Correct 515 ms 600 KB Output is correct
54 Correct 582 ms 1644 KB Output is correct
55 Correct 557 ms 1448 KB Output is correct
56 Correct 565 ms 1476 KB Output is correct
57 Correct 605 ms 1368 KB Output is correct
58 Correct 587 ms 1644 KB Output is correct
59 Correct 543 ms 1576 KB Output is correct
60 Correct 531 ms 1576 KB Output is correct
61 Correct 518 ms 960 KB Output is correct
62 Correct 528 ms 1236 KB Output is correct
63 Correct 576 ms 1112 KB Output is correct
64 Correct 534 ms 1368 KB Output is correct
65 Correct 244 ms 856 KB Output is correct
66 Correct 549 ms 1432 KB Output is correct
67 Correct 564 ms 1432 KB Output is correct
68 Correct 498 ms 1432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 0 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 600 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 0 ms 600 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 0 ms 600 KB Output is correct
30 Correct 1 ms 600 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 600 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 0 ms 600 KB Output is correct
42 Correct 0 ms 600 KB Output is correct
43 Correct 392 ms 8780 KB Output is correct
44 Correct 558 ms 17228 KB Output is correct
45 Correct 600 ms 17220 KB Output is correct
46 Correct 599 ms 17228 KB Output is correct
47 Correct 476 ms 8780 KB Output is correct
48 Correct 643 ms 17220 KB Output is correct
49 Correct 638 ms 17220 KB Output is correct
50 Correct 612 ms 17228 KB Output is correct
51 Correct 276 ms 856 KB Output is correct
52 Correct 573 ms 1432 KB Output is correct
53 Correct 586 ms 1432 KB Output is correct
54 Correct 552 ms 1432 KB Output is correct
55 Correct 608 ms 25680 KB Output is correct
56 Correct 713 ms 26040 KB Output is correct
57 Correct 637 ms 26040 KB Output is correct
58 Correct 497 ms 26040 KB Output is correct
59 Correct 662 ms 26040 KB Output is correct
60 Correct 655 ms 26040 KB Output is correct
61 Correct 445 ms 976 KB Output is correct
62 Correct 574 ms 956 KB Output is correct
63 Correct 578 ms 1220 KB Output is correct
64 Correct 489 ms 1644 KB Output is correct
65 Correct 548 ms 1644 KB Output is correct
66 Correct 562 ms 1644 KB Output is correct
67 Correct 529 ms 1644 KB Output is correct
68 Correct 537 ms 1644 KB Output is correct
69 Correct 581 ms 1300 KB Output is correct
70 Correct 605 ms 1300 KB Output is correct
71 Correct 515 ms 600 KB Output is correct
72 Correct 582 ms 1644 KB Output is correct
73 Correct 557 ms 1448 KB Output is correct
74 Correct 565 ms 1476 KB Output is correct
75 Correct 605 ms 1368 KB Output is correct
76 Correct 587 ms 1644 KB Output is correct
77 Correct 543 ms 1576 KB Output is correct
78 Correct 531 ms 1576 KB Output is correct
79 Correct 518 ms 960 KB Output is correct
80 Correct 528 ms 1236 KB Output is correct
81 Correct 576 ms 1112 KB Output is correct
82 Correct 534 ms 1368 KB Output is correct
83 Correct 244 ms 856 KB Output is correct
84 Correct 549 ms 1432 KB Output is correct
85 Correct 564 ms 1432 KB Output is correct
86 Correct 498 ms 1432 KB Output is correct
87 Correct 0 ms 344 KB Output is correct
88 Correct 380 ms 22468 KB Output is correct
89 Correct 616 ms 15064 KB Output is correct
90 Correct 584 ms 15704 KB Output is correct
91 Correct 612 ms 26296 KB Output is correct
92 Correct 687 ms 26296 KB Output is correct
93 Correct 685 ms 26248 KB Output is correct
94 Correct 596 ms 26292 KB Output is correct
95 Correct 578 ms 26228 KB Output is correct
96 Correct 545 ms 20008 KB Output is correct
97 Correct 605 ms 20008 KB Output is correct
98 Correct 480 ms 8536 KB Output is correct
99 Correct 634 ms 26040 KB Output is correct
100 Correct 643 ms 22668 KB Output is correct
101 Correct 644 ms 21320 KB Output is correct
102 Correct 589 ms 19572 KB Output is correct
103 Correct 602 ms 26048 KB Output is correct
104 Correct 685 ms 26464 KB Output is correct
105 Correct 605 ms 26528 KB Output is correct
106 Correct 589 ms 13656 KB Output is correct
107 Correct 612 ms 15600 KB Output is correct
108 Correct 649 ms 17152 KB Output is correct
109 Correct 621 ms 19844 KB Output is correct