Submission #1051173

# Submission time Handle Problem Language Result Execution time Memory
1051173 2024-08-09T21:55:27 Z Trent Digital Circuit (IOI22_circuit) C++17
100 / 100
689 ms 49196 KB
#include "circuit.h"
#include "bits/stdc++.h"
using namespace std;
#define forR(i, x) for(int i = 0; i < (x); ++i)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<bool> vb;

const ll MOD = 1000002022;
const int MN = 1e5 + 10;
vvi ch;
vll w;
int n, m;
vll totPos;
void sdfs(int c) {
  totPos[c] = c >= n ? 1 : ch[c].size();
  for(int i : ch[c]) {
    sdfs(i);
    totPos[c] = totPos[c] * totPos[i] % MOD;
  }
}
void dfs(int c, ll cw) {
  w[c] = cw;
  vll ppa(ch[c].size()), spa(ch[c].size()), val(ch[c].size());
  for(int j = 0; j < ch[c].size(); j++) {
    val[j] = totPos[ch[c][j]];
    ppa[j] = val[j] * (j == 0 ? 1 : ppa[j-1]) % MOD;
  }
  for(int j = (int) ch[c].size() - 1; j >= 0; --j) {
    spa[j] = val[j] * (j + 1 == ch[c].size() ? 1 : spa[j+1]) % MOD;
  }
  for(int i = 0; i < ch[c].size(); ++i) {
    ll wCh = cw * (i == 0 ? 1 : ppa[i-1]) % MOD * (i + 1 == ch[c].size() ? 1 : spa[i+1]) % MOD;
    dfs(ch[c][i], wCh);
  }
}

vb tgl;
struct node {
  ll tot, iTot;
  bool lz;
};
const int MM = 1e5 + 10, ME = 4 * MM;
node seg[ME];
void push(int c) {
  if(seg[c].lz) {
    if(2 * c + 1 < ME) {
      swap(seg[2*c].tot, seg[2*c].iTot);
      seg[2*c].lz = !seg[2*c].lz;
      swap(seg[2*c+1].tot, seg[2*c+1].iTot);
      seg[2*c+1].lz = !seg[2*c+1].lz;
    }
    seg[c].lz = false;
  }
}
void build(int v, int nl, int nr) {
  if(nl == nr) {
    if(tgl[nl + n]) seg[v].tot = w[nl+n], seg[v].iTot = 0;
    else seg[v].tot = 0, seg[v].iTot = w[nl+n];
  } else {
    int mid = (nl+nr)/2;
    build(2*v, nl, mid);
    build(2*v+1, mid+1, nr);
    seg[v].tot = (seg[2*v].tot+seg[2*v+1].tot) % MOD;
    seg[v].iTot = (seg[2*v].iTot+seg[2*v+1].iTot) % MOD;
  }
}
void upd(int v, int nl, int nr, int l, int r) {
  push(v);
  if(l > r) return;
  if(l == nl && r == nr) {
    swap(seg[v].tot, seg[v].iTot);
    assert(!seg[v].lz);
    seg[v].lz = true;
  } else {
    int mid = (nl+nr)/2;
    upd(2*v, nl, mid, l, min(mid, r));
    upd(2*v+1, mid+1, nr, max(mid+1,l), r);
    seg[v].tot = (seg[2*v].tot + seg[2*v+1].tot) % MOD;
    seg[v].iTot = (seg[2*v].iTot + seg[2*v+1].iTot) % MOD;
  }
}
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
  ::n = N, ::m = M;
  ch.resize(N+M);
  totPos.resize(N+M);
  w.resize(N+M);
  tgl.resize(N+M);

  REP(i, 1, N+M) ch[P[i]].push_back(i);
  sdfs(0);
  dfs(0, 1);
  forR(i, M) tgl[N+i] = A[i] == 1;
  build(1, 0, m-1);
}

int count_ways(int L, int R) {
  upd(1, 0, m-1, L-n, R-n);
  ll tot = seg[1].tot;
  return (int) tot;
}

Compilation message

circuit.cpp: In function 'void dfs(int, ll)':
circuit.cpp:29:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |   for(int j = 0; j < ch[c].size(); j++) {
      |                  ~~^~~~~~~~~~~~~~
circuit.cpp:34:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |     spa[j] = val[j] * (j + 1 == ch[c].size() ? 1 : spa[j+1]) % MOD;
      |                        ~~~~~~^~~~~~~~~~~~~~~
circuit.cpp:36:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |   for(int i = 0; i < ch[c].size(); ++i) {
      |                  ~~^~~~~~~~~~~~~~
circuit.cpp:37:58: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     ll wCh = cw * (i == 0 ? 1 : ppa[i-1]) % MOD * (i + 1 == ch[c].size() ? 1 : spa[i+1]) % MOD;
      |                                                    ~~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory 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 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 0 ms 600 KB Output is correct
10 Correct 0 ms 856 KB Output is correct
11 Correct 1 ms 856 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
# Verdict Execution time Memory 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 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 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 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 0 ms 600 KB Output is correct
17 Correct 0 ms 600 KB Output is correct
18 Correct 0 ms 856 KB Output is correct
19 Correct 1 ms 856 KB Output is correct
20 Correct 0 ms 600 KB Output is correct
21 Correct 0 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 0 ms 600 KB Output is correct
25 Correct 0 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 0 ms 600 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 0 ms 600 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 0 ms 856 KB Output is correct
37 Correct 0 ms 856 KB Output is correct
38 Correct 1 ms 856 KB Output is correct
39 Correct 0 ms 344 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 393 ms 7508 KB Output is correct
2 Correct 537 ms 14972 KB Output is correct
3 Correct 617 ms 14652 KB Output is correct
4 Correct 529 ms 14632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 393 ms 7508 KB Output is correct
2 Correct 537 ms 14972 KB Output is correct
3 Correct 617 ms 14652 KB Output is correct
4 Correct 529 ms 14632 KB Output is correct
5 Correct 445 ms 7508 KB Output is correct
6 Correct 614 ms 14664 KB Output is correct
7 Correct 600 ms 14572 KB Output is correct
8 Correct 558 ms 13400 KB Output is correct
9 Correct 249 ms 856 KB Output is correct
10 Correct 542 ms 3160 KB Output is correct
11 Correct 560 ms 3160 KB Output is correct
12 Correct 516 ms 3160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 0 ms 600 KB Output is correct
10 Correct 0 ms 856 KB Output is correct
11 Correct 1 ms 856 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
13 Correct 393 ms 7508 KB Output is correct
14 Correct 537 ms 14972 KB Output is correct
15 Correct 617 ms 14652 KB Output is correct
16 Correct 529 ms 14632 KB Output is correct
17 Correct 445 ms 7508 KB Output is correct
18 Correct 614 ms 14664 KB Output is correct
19 Correct 600 ms 14572 KB Output is correct
20 Correct 558 ms 13400 KB Output is correct
21 Correct 249 ms 856 KB Output is correct
22 Correct 542 ms 3160 KB Output is correct
23 Correct 560 ms 3160 KB Output is correct
24 Correct 516 ms 3160 KB Output is correct
25 Correct 601 ms 22100 KB Output is correct
26 Correct 677 ms 22256 KB Output is correct
27 Correct 673 ms 22020 KB Output is correct
28 Correct 520 ms 20048 KB Output is correct
29 Correct 653 ms 48616 KB Output is correct
30 Correct 564 ms 46672 KB Output is correct
# Verdict Execution time Memory 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 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 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 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 0 ms 600 KB Output is correct
17 Correct 0 ms 600 KB Output is correct
18 Correct 0 ms 856 KB Output is correct
19 Correct 1 ms 856 KB Output is correct
20 Correct 0 ms 600 KB Output is correct
21 Correct 0 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 0 ms 600 KB Output is correct
25 Correct 0 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 0 ms 600 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 0 ms 600 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 0 ms 856 KB Output is correct
37 Correct 0 ms 856 KB Output is correct
38 Correct 1 ms 856 KB Output is correct
39 Correct 0 ms 344 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 374 ms 2904 KB Output is correct
44 Correct 555 ms 2904 KB Output is correct
45 Correct 592 ms 2904 KB Output is correct
46 Correct 503 ms 3160 KB Output is correct
47 Correct 560 ms 3160 KB Output is correct
48 Correct 568 ms 3160 KB Output is correct
49 Correct 573 ms 3160 KB Output is correct
50 Correct 514 ms 3160 KB Output is correct
51 Correct 549 ms 3084 KB Output is correct
52 Correct 599 ms 3076 KB Output is correct
53 Correct 486 ms 2136 KB Output is correct
54 Correct 547 ms 3160 KB Output is correct
55 Correct 546 ms 2904 KB Output is correct
56 Correct 549 ms 2904 KB Output is correct
57 Correct 571 ms 2904 KB Output is correct
58 Correct 599 ms 4440 KB Output is correct
59 Correct 570 ms 4696 KB Output is correct
60 Correct 520 ms 4696 KB Output is correct
61 Correct 578 ms 3416 KB Output is correct
62 Correct 528 ms 2904 KB Output is correct
63 Correct 582 ms 2904 KB Output is correct
64 Correct 567 ms 2904 KB Output is correct
65 Correct 265 ms 856 KB Output is correct
66 Correct 525 ms 3160 KB Output is correct
67 Correct 581 ms 3160 KB Output is correct
68 Correct 560 ms 3160 KB Output is correct
# Verdict Execution time Memory 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 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 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 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 0 ms 600 KB Output is correct
17 Correct 0 ms 600 KB Output is correct
18 Correct 0 ms 856 KB Output is correct
19 Correct 1 ms 856 KB Output is correct
20 Correct 0 ms 600 KB Output is correct
21 Correct 0 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 0 ms 600 KB Output is correct
25 Correct 0 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 0 ms 600 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 0 ms 600 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 0 ms 856 KB Output is correct
37 Correct 0 ms 856 KB Output is correct
38 Correct 1 ms 856 KB Output is correct
39 Correct 0 ms 344 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 393 ms 7508 KB Output is correct
44 Correct 537 ms 14972 KB Output is correct
45 Correct 617 ms 14652 KB Output is correct
46 Correct 529 ms 14632 KB Output is correct
47 Correct 445 ms 7508 KB Output is correct
48 Correct 614 ms 14664 KB Output is correct
49 Correct 600 ms 14572 KB Output is correct
50 Correct 558 ms 13400 KB Output is correct
51 Correct 249 ms 856 KB Output is correct
52 Correct 542 ms 3160 KB Output is correct
53 Correct 560 ms 3160 KB Output is correct
54 Correct 516 ms 3160 KB Output is correct
55 Correct 601 ms 22100 KB Output is correct
56 Correct 677 ms 22256 KB Output is correct
57 Correct 673 ms 22020 KB Output is correct
58 Correct 520 ms 20048 KB Output is correct
59 Correct 653 ms 48616 KB Output is correct
60 Correct 564 ms 46672 KB Output is correct
61 Correct 374 ms 2904 KB Output is correct
62 Correct 555 ms 2904 KB Output is correct
63 Correct 592 ms 2904 KB Output is correct
64 Correct 503 ms 3160 KB Output is correct
65 Correct 560 ms 3160 KB Output is correct
66 Correct 568 ms 3160 KB Output is correct
67 Correct 573 ms 3160 KB Output is correct
68 Correct 514 ms 3160 KB Output is correct
69 Correct 549 ms 3084 KB Output is correct
70 Correct 599 ms 3076 KB Output is correct
71 Correct 486 ms 2136 KB Output is correct
72 Correct 547 ms 3160 KB Output is correct
73 Correct 546 ms 2904 KB Output is correct
74 Correct 549 ms 2904 KB Output is correct
75 Correct 571 ms 2904 KB Output is correct
76 Correct 599 ms 4440 KB Output is correct
77 Correct 570 ms 4696 KB Output is correct
78 Correct 520 ms 4696 KB Output is correct
79 Correct 578 ms 3416 KB Output is correct
80 Correct 528 ms 2904 KB Output is correct
81 Correct 582 ms 2904 KB Output is correct
82 Correct 567 ms 2904 KB Output is correct
83 Correct 265 ms 856 KB Output is correct
84 Correct 525 ms 3160 KB Output is correct
85 Correct 581 ms 3160 KB Output is correct
86 Correct 560 ms 3160 KB Output is correct
87 Correct 0 ms 344 KB Output is correct
88 Correct 389 ms 20984 KB Output is correct
89 Correct 610 ms 14788 KB Output is correct
90 Correct 564 ms 14336 KB Output is correct
91 Correct 611 ms 22252 KB Output is correct
92 Correct 614 ms 22392 KB Output is correct
93 Correct 626 ms 22112 KB Output is correct
94 Correct 675 ms 22152 KB Output is correct
95 Correct 678 ms 20048 KB Output is correct
96 Correct 535 ms 12704 KB Output is correct
97 Correct 611 ms 15456 KB Output is correct
98 Correct 551 ms 34904 KB Output is correct
99 Correct 677 ms 22200 KB Output is correct
100 Correct 630 ms 18708 KB Output is correct
101 Correct 663 ms 17276 KB Output is correct
102 Correct 645 ms 15424 KB Output is correct
103 Correct 674 ms 49052 KB Output is correct
104 Correct 689 ms 49196 KB Output is correct
105 Correct 600 ms 47012 KB Output is correct
106 Correct 618 ms 20400 KB Output is correct
107 Correct 659 ms 14492 KB Output is correct
108 Correct 610 ms 16420 KB Output is correct
109 Correct 591 ms 15688 KB Output is correct