Submission #1031841

# Submission time Handle Problem Language Result Execution time Memory
1031841 2024-07-23T07:58:33 Z NeroZein Digital Circuit (IOI22_circuit) C++17
46 / 100
690 ms 13788 KB
#include "circuit.h"
#include "bits/stdc++.h"
 
using namespace std;
 
const int MAX_N = 1e5 + 5;
const int md = 1000002022;

struct node {
  int tot_sum = 0, sum = 0;
  bool lazy = 0;
};
 
int add(int x, int y) {
  x += y;
  if (x >= md) x -= md;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0) x += md;
  return x; 
}
 
int mul(int x, int y) {
  return (int) ((long long) x * y % md); 
}
 
int n, m;
vector<int> a;
int dp[MAX_N];
int contrib[MAX_N];
node seg[MAX_N * 2];
vector<int> g[MAX_N];

void dfs(int v) {
  if (v >= n) {
    dp[v] = 1; 
    return; 
  }
  dp[v] = g[v].size();
  for (int u : g[v]) {
    dfs(u); 
    dp[v] = mul(dp[v], dp[u]); 
  }
  //cerr << "V, dp[V]: " << v << ' ' << dp[v] << '\n';
}

void dfs2(int v, int val = 1) {
  //cerr << "V, Val: " << v << ' ' << val << '\n';
  if (v >= n) {
    contrib[v - n] = val;
    return;
  }
  int sz = (int) g[v].size();
  vector<int> pref(sz), suf(sz);
  pref[0] = suf[sz - 1] = 1; 
  for (int i = 1; i < sz; ++i) {
    int u = g[v][i - 1];
    pref[i] = mul(pref[i - 1], dp[u]);
  }
  for (int i = sz - 2; i >= 0; --i) {
    int u = g[v][i + 1];
    suf[i] = mul(suf[i + 1], dp[u]); 
  }
  for (int i = 0; i < sz; ++i) {
    int u = g[v][i];
    int nval = mul(val, pref[i]);
    nval = mul(nval, suf[i]); 
    dfs2(u, nval); 
  }
}

node combine(node lx, node rx) {
  node ret;
  ret.sum = add(lx.sum, rx.sum);
  ret.tot_sum = add(lx.tot_sum, rx.tot_sum);
  return ret;
}

void build(int nd, int l, int r) {
  if (l == r) {
    seg[nd].tot_sum = contrib[l];
    if (a[l]) {
      seg[nd].sum = contrib[l];
    }
    return;
  }
  int mid = (l + r) >> 1;
  int rs = nd + ((mid - l + 1) << 1);
  build(nd + 1, l, mid);
  build(rs, mid + 1, r);
  seg[nd] = combine(seg[nd + 1], seg[rs]);
}

void push(int nd, int l, int r) {
  if (!seg[nd].lazy) {
    return; 
  }
  seg[nd].sum = sub(seg[nd].tot_sum, seg[nd].sum);
  if (l != r) {
    int mid = (l + r) >> 1;
    int rs = nd + ((mid - l + 1) << 1);
    seg[nd + 1].lazy ^= 1;
    seg[rs].lazy ^= 1; 
  }
  seg[nd].lazy = false; 
}

void upd(int nd, int l, int r, int s, int e) {
  //cerr << "l, r, s, e: " << l << ' ' << r << ' ' << s << ' ' << e << '\n'; 
  push(nd, l, r); 
  if (l >= s && r <= e) {
    seg[nd].lazy = 1;
    push(nd, l, r);
    return;
  }
  int mid = (l + r) >> 1;
  int rs = nd + ((mid - l + 1) << 1);
  if (mid >= e) {
    push(rs, mid + 1, r); 
    upd(nd + 1, l, mid, s, e);
  } else {
    if (mid < s) {
      push(nd + 1, l, mid);
      upd(rs, mid + 1, r, s, e); 
    } else {
      upd(nd + 1, l, mid, s, e);
      upd(rs, mid + 1, r, s, e); 
    }
  }
  seg[nd] = combine(seg[nd + 1], seg[rs]);
  return;
}

void init(int N, int M, std::vector<int> P, std::vector<int> A) {
  n = N; 
  m = M;
  a = A;
  for (int i = 1; i < n + m; ++i) {
    g[P[i]].push_back(i); 
  }
  dfs(0);
  dfs2(0);
  build(0, 0, m - 1);
}
 
int count_ways(int l, int r) {
  upd(0, 0, m - 1, l - n, r - n);
  return seg[0].sum;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 2 ms 2648 KB Output is correct
8 Correct 1 ms 2648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2648 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 2 ms 2648 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 2 ms 2648 KB Output is correct
8 Correct 2 ms 2648 KB Output is correct
9 Correct 2 ms 2648 KB Output is correct
10 Correct 2 ms 2844 KB Output is correct
11 Correct 2 ms 2904 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 2 ms 2648 KB Output is correct
8 Correct 1 ms 2648 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2648 KB Output is correct
11 Correct 1 ms 2648 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 2 ms 2648 KB Output is correct
14 Correct 1 ms 2648 KB Output is correct
15 Correct 2 ms 2648 KB Output is correct
16 Correct 2 ms 2648 KB Output is correct
17 Correct 2 ms 2648 KB Output is correct
18 Correct 2 ms 2844 KB Output is correct
19 Correct 2 ms 2904 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 1 ms 2648 KB Output is correct
23 Correct 1 ms 2648 KB Output is correct
24 Correct 1 ms 2644 KB Output is correct
25 Correct 2 ms 2648 KB Output is correct
26 Correct 1 ms 2648 KB Output is correct
27 Correct 1 ms 2648 KB Output is correct
28 Correct 2 ms 2648 KB Output is correct
29 Correct 1 ms 2648 KB Output is correct
30 Correct 1 ms 2648 KB Output is correct
31 Correct 1 ms 2904 KB Output is correct
32 Correct 1 ms 2648 KB Output is correct
33 Correct 1 ms 2648 KB Output is correct
34 Correct 1 ms 2648 KB Output is correct
35 Correct 1 ms 2648 KB Output is correct
36 Correct 2 ms 2904 KB Output is correct
37 Correct 1 ms 2904 KB Output is correct
38 Correct 2 ms 2904 KB Output is correct
39 Correct 1 ms 2648 KB Output is correct
40 Correct 1 ms 2648 KB Output is correct
41 Correct 1 ms 2648 KB Output is correct
42 Correct 1 ms 2648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 398 ms 5888 KB Output is correct
2 Runtime error 18 ms 13788 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 398 ms 5888 KB Output is correct
2 Runtime error 18 ms 13788 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2648 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 2 ms 2648 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 2 ms 2648 KB Output is correct
8 Correct 2 ms 2648 KB Output is correct
9 Correct 2 ms 2648 KB Output is correct
10 Correct 2 ms 2844 KB Output is correct
11 Correct 2 ms 2904 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 398 ms 5888 KB Output is correct
14 Runtime error 18 ms 13788 KB Execution killed with signal 11
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 2 ms 2648 KB Output is correct
8 Correct 1 ms 2648 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2648 KB Output is correct
11 Correct 1 ms 2648 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 2 ms 2648 KB Output is correct
14 Correct 1 ms 2648 KB Output is correct
15 Correct 2 ms 2648 KB Output is correct
16 Correct 2 ms 2648 KB Output is correct
17 Correct 2 ms 2648 KB Output is correct
18 Correct 2 ms 2844 KB Output is correct
19 Correct 2 ms 2904 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 1 ms 2648 KB Output is correct
23 Correct 1 ms 2648 KB Output is correct
24 Correct 1 ms 2644 KB Output is correct
25 Correct 2 ms 2648 KB Output is correct
26 Correct 1 ms 2648 KB Output is correct
27 Correct 1 ms 2648 KB Output is correct
28 Correct 2 ms 2648 KB Output is correct
29 Correct 1 ms 2648 KB Output is correct
30 Correct 1 ms 2648 KB Output is correct
31 Correct 1 ms 2904 KB Output is correct
32 Correct 1 ms 2648 KB Output is correct
33 Correct 1 ms 2648 KB Output is correct
34 Correct 1 ms 2648 KB Output is correct
35 Correct 1 ms 2648 KB Output is correct
36 Correct 2 ms 2904 KB Output is correct
37 Correct 1 ms 2904 KB Output is correct
38 Correct 2 ms 2904 KB Output is correct
39 Correct 1 ms 2648 KB Output is correct
40 Correct 1 ms 2648 KB Output is correct
41 Correct 1 ms 2648 KB Output is correct
42 Correct 1 ms 2648 KB Output is correct
43 Correct 447 ms 2904 KB Output is correct
44 Correct 627 ms 2904 KB Output is correct
45 Correct 677 ms 2904 KB Output is correct
46 Correct 662 ms 3160 KB Output is correct
47 Correct 656 ms 3160 KB Output is correct
48 Correct 584 ms 3160 KB Output is correct
49 Correct 659 ms 3160 KB Output is correct
50 Correct 635 ms 3160 KB Output is correct
51 Correct 645 ms 2904 KB Output is correct
52 Correct 644 ms 2904 KB Output is correct
53 Correct 578 ms 3928 KB Output is correct
54 Correct 654 ms 3160 KB Output is correct
55 Correct 679 ms 2904 KB Output is correct
56 Correct 690 ms 2904 KB Output is correct
57 Correct 681 ms 2904 KB Output is correct
58 Correct 643 ms 4184 KB Output is correct
59 Correct 567 ms 4184 KB Output is correct
60 Correct 648 ms 4184 KB Output is correct
61 Correct 479 ms 3048 KB Output is correct
62 Correct 490 ms 2904 KB Output is correct
63 Correct 618 ms 2820 KB Output is correct
64 Correct 614 ms 2904 KB Output is correct
65 Correct 322 ms 2904 KB Output is correct
66 Correct 679 ms 3160 KB Output is correct
67 Correct 522 ms 3160 KB Output is correct
68 Correct 560 ms 3160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 2 ms 2648 KB Output is correct
8 Correct 1 ms 2648 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2648 KB Output is correct
11 Correct 1 ms 2648 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 2 ms 2648 KB Output is correct
14 Correct 1 ms 2648 KB Output is correct
15 Correct 2 ms 2648 KB Output is correct
16 Correct 2 ms 2648 KB Output is correct
17 Correct 2 ms 2648 KB Output is correct
18 Correct 2 ms 2844 KB Output is correct
19 Correct 2 ms 2904 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 1 ms 2648 KB Output is correct
23 Correct 1 ms 2648 KB Output is correct
24 Correct 1 ms 2644 KB Output is correct
25 Correct 2 ms 2648 KB Output is correct
26 Correct 1 ms 2648 KB Output is correct
27 Correct 1 ms 2648 KB Output is correct
28 Correct 2 ms 2648 KB Output is correct
29 Correct 1 ms 2648 KB Output is correct
30 Correct 1 ms 2648 KB Output is correct
31 Correct 1 ms 2904 KB Output is correct
32 Correct 1 ms 2648 KB Output is correct
33 Correct 1 ms 2648 KB Output is correct
34 Correct 1 ms 2648 KB Output is correct
35 Correct 1 ms 2648 KB Output is correct
36 Correct 2 ms 2904 KB Output is correct
37 Correct 1 ms 2904 KB Output is correct
38 Correct 2 ms 2904 KB Output is correct
39 Correct 1 ms 2648 KB Output is correct
40 Correct 1 ms 2648 KB Output is correct
41 Correct 1 ms 2648 KB Output is correct
42 Correct 1 ms 2648 KB Output is correct
43 Correct 398 ms 5888 KB Output is correct
44 Runtime error 18 ms 13788 KB Execution killed with signal 11
45 Halted 0 ms 0 KB -