Submission #787129

# Submission time Handle Problem Language Result Execution time Memory
787129 2023-07-18T20:56:12 Z Sam_a17 Digital Circuit (IOI22_circuit) C++17
100 / 100
925 ms 46680 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;
 
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
 
#define sz(x) (int)x.size()
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define blt __builtin_popcount
 
#define pb push_back
#define popf pop_front
#define popb pop_back
#define ld long double
#define ll long long
 
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
 
// Indexed Set  
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
// template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(Tree <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
 
 
// for random generations
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
// mt19937 myrand(131);
 
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
 
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
// void setIO(string str = "") {
//   fastIO();
 
//   // if(str == "input") {
//     // freopen("input.txt", "r", stdin);
//     // freopen("output.txt", "w", stdout);
//   // } else if(str != "") {
//     // freopen((str + ".in").c_str(), "r", stdin);
//     // freopen((str + ".out").c_str(), "w", stdout);
//   // }
// }

const long long mod = 1000002022;
const int N = 3e5 + 10;
vector<int> adj[N];
long long dp[N][2];
int n, m, a[N], par[N];
int cnt = 0;

long long add(long long a, long long b) {
  return (a + b) % mod;
}

long long mult(long long a, long long b) {
  return (a * b) % mod;
}

long long sub(long long a, long long b) {
  return (a - b + 2 * mod) % mod;
}

struct segTree {
  struct node {
    long long a, b;
    long long lazy;
  };

  vector<node> tree;
  int size = 1;

  void init(int s) {
    while(size < s) {
      size *= 2;
    }
    tree.assign(2 * size - 1, {0, 0, 0});
  }

  node merge(node a, node b) {
    a.a = add(a.a, b.a);
    a.b = add(a.b, b.b);
    a.lazy = 0;
    return a;
  }

  void propogate(int x, int lx, int rx) {
    if(rx - lx == 1 || !tree[x].lazy) {
      tree[x].lazy = 0;
      return;
    }

    tree[2 * x + 1].lazy += tree[x].lazy;
    tree[2 * x + 1].lazy %= 2;
    swap(tree[2 * x + 1].a, tree[2 * x + 1].b);

    tree[2 * x + 2].lazy += tree[x].lazy;
    tree[2 * x + 2].lazy %= 2;
    swap(tree[2 * x + 2].a, tree[2 * x + 2].b);

    tree[x].lazy = 0;
  }

  void upd(int u, int is_on, long long val, int x, int lx, int rx) {
    if(rx - lx == 1) {
      if(is_on) {
        tree[x].a = val;
        tree[x].b = 0;
      } else {
        tree[x].a = 0;
        tree[x].b = val;
      }
      return;
    }

    int mid = (lx + rx) / 2;
    if(u < mid) {
      upd(u, is_on, val, 2 * x + 1, lx, mid);
    } else {
      upd(u, is_on, val, 2 * x + 2, mid, rx);
    }
    tree[x] = merge(tree[2 * x + 1], tree[2 * x + 2]);
  }

  void upd(int u, int is_on, long long val) {
    upd(u, is_on, val, 0, 0, size);
  }

  void change(int l, int r, int x, int lx, int rx) {
    propogate(x, lx, rx);
    if(lx >= r || rx <= l) {
      return;
    }

    if(lx >= l && rx <= r) {
      assert(tree[x].lazy == 0);
      swap(tree[x].a, tree[x].b);
      tree[x].lazy++;
      propogate(x, lx, rx);
      return;
    }

    int mid = (lx + rx) / 2;
    change(l, r, 2 * x + 1, lx, mid);
    change(l, r, 2 * x + 2, mid, rx);
    tree[x] = merge(tree[2 * x + 1], tree[2 * x + 2]);
  }

  void change(int l, int r) {
    change(l, r, 0, 0, size);
  }
};

segTree seg;

long long s[N];

void dfs1(int node) {
  if(sz(adj[node]) == 0) {
    s[node] = 1;
  } else {
    s[node] = sz(adj[node]);
    for(auto i: adj[node]) {
      dfs1(i);
      s[node] = mult(s[node], s[i]);
    }
  }
}

void dfs2(int node, long long coef) {
  if(sz(adj[node]) == 0) {
    dp[node][a[node]] = coef;
    dp[node][1 - a[node]] = 0;
  } else if(sz(adj[node]) == 1) {
    int cur = adj[node][0];
    par[cur] = node;
    dfs2(cur, coef);
  } else {
    int first = adj[node][0], second = adj[node][1];
    par[first] = par[second] = node;

    vector<long long> pref(sz(adj[node]) + 1, 1), suf(sz(adj[node]) + 1, 1);
    for(int i = 0; i < sz(adj[node]); i++) {
      pref[i + 1] = mult(pref[i], s[adj[node][i]]);
    }

    for(int i = sz(adj[node]); i >= 1; i--) {
      suf[i - 1] = mult(suf[i], s[adj[node][i - 1]]);
    }

    for(int i = 0; i < sz(adj[node]); i++) {
      long long mulfCof = mult(pref[i], suf[i + 1]);
      dfs2(adj[node][i], mult(coef, mulfCof));
    }
  }
}

void init(int maxN, int M, vector<int> P, vector<int> A) {
  n = maxN, m = M;

  for(int i = 1; i < sz(P); i++) {
    adj[P[i]].push_back(i);
  }

  par[0] = -1;

  for(int i = 0; i < m; i++) {
    a[n + i] = A[i];
    cnt += a[n + i];
  }

  dfs1(0);
  dfs2(0, 1);
  seg.init(n + m + 5);
  for(int i = 0; i < m; i++) {
    int cur = n + i;
    seg.upd(cur, a[cur], dp[cur][a[cur]]);
  }
}

int count_ways(int L, int R) {
  seg.change(L, R + 1);
  seg.propogate(0, 0, seg.size);
  return seg.tree[0].a;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7248 KB Output is correct
2 Correct 3 ms 7248 KB Output is correct
3 Correct 4 ms 7376 KB Output is correct
4 Correct 3 ms 7376 KB Output is correct
5 Correct 3 ms 7376 KB Output is correct
6 Correct 3 ms 7376 KB Output is correct
7 Correct 4 ms 7440 KB Output is correct
8 Correct 4 ms 7476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7348 KB Output is correct
2 Correct 3 ms 7376 KB Output is correct
3 Correct 3 ms 7504 KB Output is correct
4 Correct 3 ms 7504 KB Output is correct
5 Correct 4 ms 7504 KB Output is correct
6 Correct 4 ms 7504 KB Output is correct
7 Correct 4 ms 7504 KB Output is correct
8 Correct 4 ms 7552 KB Output is correct
9 Correct 4 ms 7504 KB Output is correct
10 Correct 4 ms 7632 KB Output is correct
11 Correct 4 ms 7632 KB Output is correct
12 Correct 4 ms 7504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7248 KB Output is correct
2 Correct 3 ms 7248 KB Output is correct
3 Correct 4 ms 7376 KB Output is correct
4 Correct 3 ms 7376 KB Output is correct
5 Correct 3 ms 7376 KB Output is correct
6 Correct 3 ms 7376 KB Output is correct
7 Correct 4 ms 7440 KB Output is correct
8 Correct 4 ms 7476 KB Output is correct
9 Correct 3 ms 7348 KB Output is correct
10 Correct 3 ms 7376 KB Output is correct
11 Correct 3 ms 7504 KB Output is correct
12 Correct 3 ms 7504 KB Output is correct
13 Correct 4 ms 7504 KB Output is correct
14 Correct 4 ms 7504 KB Output is correct
15 Correct 4 ms 7504 KB Output is correct
16 Correct 4 ms 7552 KB Output is correct
17 Correct 4 ms 7504 KB Output is correct
18 Correct 4 ms 7632 KB Output is correct
19 Correct 4 ms 7632 KB Output is correct
20 Correct 4 ms 7504 KB Output is correct
21 Correct 4 ms 7504 KB Output is correct
22 Correct 3 ms 7504 KB Output is correct
23 Correct 4 ms 7504 KB Output is correct
24 Correct 4 ms 7504 KB Output is correct
25 Correct 3 ms 7504 KB Output is correct
26 Correct 4 ms 7504 KB Output is correct
27 Correct 4 ms 7504 KB Output is correct
28 Correct 4 ms 7504 KB Output is correct
29 Correct 4 ms 7376 KB Output is correct
30 Correct 4 ms 7376 KB Output is correct
31 Correct 3 ms 7504 KB Output is correct
32 Correct 4 ms 7492 KB Output is correct
33 Correct 3 ms 7532 KB Output is correct
34 Correct 4 ms 7504 KB Output is correct
35 Correct 3 ms 7376 KB Output is correct
36 Correct 6 ms 7632 KB Output is correct
37 Correct 4 ms 7632 KB Output is correct
38 Correct 4 ms 7632 KB Output is correct
39 Correct 3 ms 7504 KB Output is correct
40 Correct 4 ms 7504 KB Output is correct
41 Correct 4 ms 7504 KB Output is correct
42 Correct 4 ms 7376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 509 ms 16716 KB Output is correct
2 Correct 834 ms 26036 KB Output is correct
3 Correct 751 ms 26020 KB Output is correct
4 Correct 747 ms 26036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 509 ms 16716 KB Output is correct
2 Correct 834 ms 26036 KB Output is correct
3 Correct 751 ms 26020 KB Output is correct
4 Correct 747 ms 26036 KB Output is correct
5 Correct 637 ms 16716 KB Output is correct
6 Correct 752 ms 26036 KB Output is correct
7 Correct 863 ms 26056 KB Output is correct
8 Correct 694 ms 26104 KB Output is correct
9 Correct 366 ms 7888 KB Output is correct
10 Correct 715 ms 8528 KB Output is correct
11 Correct 648 ms 8528 KB Output is correct
12 Correct 687 ms 8528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7348 KB Output is correct
2 Correct 3 ms 7376 KB Output is correct
3 Correct 3 ms 7504 KB Output is correct
4 Correct 3 ms 7504 KB Output is correct
5 Correct 4 ms 7504 KB Output is correct
6 Correct 4 ms 7504 KB Output is correct
7 Correct 4 ms 7504 KB Output is correct
8 Correct 4 ms 7552 KB Output is correct
9 Correct 4 ms 7504 KB Output is correct
10 Correct 4 ms 7632 KB Output is correct
11 Correct 4 ms 7632 KB Output is correct
12 Correct 4 ms 7504 KB Output is correct
13 Correct 509 ms 16716 KB Output is correct
14 Correct 834 ms 26036 KB Output is correct
15 Correct 751 ms 26020 KB Output is correct
16 Correct 747 ms 26036 KB Output is correct
17 Correct 637 ms 16716 KB Output is correct
18 Correct 752 ms 26036 KB Output is correct
19 Correct 863 ms 26056 KB Output is correct
20 Correct 694 ms 26104 KB Output is correct
21 Correct 366 ms 7888 KB Output is correct
22 Correct 715 ms 8528 KB Output is correct
23 Correct 648 ms 8528 KB Output is correct
24 Correct 687 ms 8528 KB Output is correct
25 Correct 796 ms 29292 KB Output is correct
26 Correct 925 ms 29356 KB Output is correct
27 Correct 785 ms 29356 KB Output is correct
28 Correct 560 ms 29364 KB Output is correct
29 Correct 821 ms 46668 KB Output is correct
30 Correct 757 ms 46676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7248 KB Output is correct
2 Correct 3 ms 7248 KB Output is correct
3 Correct 4 ms 7376 KB Output is correct
4 Correct 3 ms 7376 KB Output is correct
5 Correct 3 ms 7376 KB Output is correct
6 Correct 3 ms 7376 KB Output is correct
7 Correct 4 ms 7440 KB Output is correct
8 Correct 4 ms 7476 KB Output is correct
9 Correct 3 ms 7348 KB Output is correct
10 Correct 3 ms 7376 KB Output is correct
11 Correct 3 ms 7504 KB Output is correct
12 Correct 3 ms 7504 KB Output is correct
13 Correct 4 ms 7504 KB Output is correct
14 Correct 4 ms 7504 KB Output is correct
15 Correct 4 ms 7504 KB Output is correct
16 Correct 4 ms 7552 KB Output is correct
17 Correct 4 ms 7504 KB Output is correct
18 Correct 4 ms 7632 KB Output is correct
19 Correct 4 ms 7632 KB Output is correct
20 Correct 4 ms 7504 KB Output is correct
21 Correct 4 ms 7504 KB Output is correct
22 Correct 3 ms 7504 KB Output is correct
23 Correct 4 ms 7504 KB Output is correct
24 Correct 4 ms 7504 KB Output is correct
25 Correct 3 ms 7504 KB Output is correct
26 Correct 4 ms 7504 KB Output is correct
27 Correct 4 ms 7504 KB Output is correct
28 Correct 4 ms 7504 KB Output is correct
29 Correct 4 ms 7376 KB Output is correct
30 Correct 4 ms 7376 KB Output is correct
31 Correct 3 ms 7504 KB Output is correct
32 Correct 4 ms 7492 KB Output is correct
33 Correct 3 ms 7532 KB Output is correct
34 Correct 4 ms 7504 KB Output is correct
35 Correct 3 ms 7376 KB Output is correct
36 Correct 6 ms 7632 KB Output is correct
37 Correct 4 ms 7632 KB Output is correct
38 Correct 4 ms 7632 KB Output is correct
39 Correct 3 ms 7504 KB Output is correct
40 Correct 4 ms 7504 KB Output is correct
41 Correct 4 ms 7504 KB Output is correct
42 Correct 4 ms 7376 KB Output is correct
43 Correct 542 ms 8016 KB Output is correct
44 Correct 736 ms 8016 KB Output is correct
45 Correct 645 ms 8016 KB Output is correct
46 Correct 702 ms 8528 KB Output is correct
47 Correct 708 ms 8528 KB Output is correct
48 Correct 659 ms 8628 KB Output is correct
49 Correct 697 ms 8632 KB Output is correct
50 Correct 686 ms 8520 KB Output is correct
51 Correct 795 ms 8016 KB Output is correct
52 Correct 678 ms 7992 KB Output is correct
53 Correct 648 ms 8272 KB Output is correct
54 Correct 696 ms 8496 KB Output is correct
55 Correct 738 ms 8008 KB Output is correct
56 Correct 756 ms 8036 KB Output is correct
57 Correct 715 ms 8008 KB Output is correct
58 Correct 691 ms 9404 KB Output is correct
59 Correct 786 ms 9020 KB Output is correct
60 Correct 689 ms 9016 KB Output is correct
61 Correct 842 ms 8144 KB Output is correct
62 Correct 727 ms 8000 KB Output is correct
63 Correct 718 ms 8016 KB Output is correct
64 Correct 648 ms 7976 KB Output is correct
65 Correct 317 ms 7888 KB Output is correct
66 Correct 696 ms 8652 KB Output is correct
67 Correct 729 ms 8504 KB Output is correct
68 Correct 797 ms 8496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7248 KB Output is correct
2 Correct 3 ms 7248 KB Output is correct
3 Correct 4 ms 7376 KB Output is correct
4 Correct 3 ms 7376 KB Output is correct
5 Correct 3 ms 7376 KB Output is correct
6 Correct 3 ms 7376 KB Output is correct
7 Correct 4 ms 7440 KB Output is correct
8 Correct 4 ms 7476 KB Output is correct
9 Correct 3 ms 7348 KB Output is correct
10 Correct 3 ms 7376 KB Output is correct
11 Correct 3 ms 7504 KB Output is correct
12 Correct 3 ms 7504 KB Output is correct
13 Correct 4 ms 7504 KB Output is correct
14 Correct 4 ms 7504 KB Output is correct
15 Correct 4 ms 7504 KB Output is correct
16 Correct 4 ms 7552 KB Output is correct
17 Correct 4 ms 7504 KB Output is correct
18 Correct 4 ms 7632 KB Output is correct
19 Correct 4 ms 7632 KB Output is correct
20 Correct 4 ms 7504 KB Output is correct
21 Correct 4 ms 7504 KB Output is correct
22 Correct 3 ms 7504 KB Output is correct
23 Correct 4 ms 7504 KB Output is correct
24 Correct 4 ms 7504 KB Output is correct
25 Correct 3 ms 7504 KB Output is correct
26 Correct 4 ms 7504 KB Output is correct
27 Correct 4 ms 7504 KB Output is correct
28 Correct 4 ms 7504 KB Output is correct
29 Correct 4 ms 7376 KB Output is correct
30 Correct 4 ms 7376 KB Output is correct
31 Correct 3 ms 7504 KB Output is correct
32 Correct 4 ms 7492 KB Output is correct
33 Correct 3 ms 7532 KB Output is correct
34 Correct 4 ms 7504 KB Output is correct
35 Correct 3 ms 7376 KB Output is correct
36 Correct 6 ms 7632 KB Output is correct
37 Correct 4 ms 7632 KB Output is correct
38 Correct 4 ms 7632 KB Output is correct
39 Correct 3 ms 7504 KB Output is correct
40 Correct 4 ms 7504 KB Output is correct
41 Correct 4 ms 7504 KB Output is correct
42 Correct 4 ms 7376 KB Output is correct
43 Correct 509 ms 16716 KB Output is correct
44 Correct 834 ms 26036 KB Output is correct
45 Correct 751 ms 26020 KB Output is correct
46 Correct 747 ms 26036 KB Output is correct
47 Correct 637 ms 16716 KB Output is correct
48 Correct 752 ms 26036 KB Output is correct
49 Correct 863 ms 26056 KB Output is correct
50 Correct 694 ms 26104 KB Output is correct
51 Correct 366 ms 7888 KB Output is correct
52 Correct 715 ms 8528 KB Output is correct
53 Correct 648 ms 8528 KB Output is correct
54 Correct 687 ms 8528 KB Output is correct
55 Correct 796 ms 29292 KB Output is correct
56 Correct 925 ms 29356 KB Output is correct
57 Correct 785 ms 29356 KB Output is correct
58 Correct 560 ms 29364 KB Output is correct
59 Correct 821 ms 46668 KB Output is correct
60 Correct 757 ms 46676 KB Output is correct
61 Correct 542 ms 8016 KB Output is correct
62 Correct 736 ms 8016 KB Output is correct
63 Correct 645 ms 8016 KB Output is correct
64 Correct 702 ms 8528 KB Output is correct
65 Correct 708 ms 8528 KB Output is correct
66 Correct 659 ms 8628 KB Output is correct
67 Correct 697 ms 8632 KB Output is correct
68 Correct 686 ms 8520 KB Output is correct
69 Correct 795 ms 8016 KB Output is correct
70 Correct 678 ms 7992 KB Output is correct
71 Correct 648 ms 8272 KB Output is correct
72 Correct 696 ms 8496 KB Output is correct
73 Correct 738 ms 8008 KB Output is correct
74 Correct 756 ms 8036 KB Output is correct
75 Correct 715 ms 8008 KB Output is correct
76 Correct 691 ms 9404 KB Output is correct
77 Correct 786 ms 9020 KB Output is correct
78 Correct 689 ms 9016 KB Output is correct
79 Correct 842 ms 8144 KB Output is correct
80 Correct 727 ms 8000 KB Output is correct
81 Correct 718 ms 8016 KB Output is correct
82 Correct 648 ms 7976 KB Output is correct
83 Correct 317 ms 7888 KB Output is correct
84 Correct 696 ms 8652 KB Output is correct
85 Correct 729 ms 8504 KB Output is correct
86 Correct 797 ms 8496 KB Output is correct
87 Correct 3 ms 7248 KB Output is correct
88 Correct 545 ms 28328 KB Output is correct
89 Correct 630 ms 19840 KB Output is correct
90 Correct 787 ms 19640 KB Output is correct
91 Correct 833 ms 29512 KB Output is correct
92 Correct 856 ms 29536 KB Output is correct
93 Correct 766 ms 29484 KB Output is correct
94 Correct 912 ms 29584 KB Output is correct
95 Correct 718 ms 29492 KB Output is correct
96 Correct 788 ms 18316 KB Output is correct
97 Correct 799 ms 18344 KB Output is correct
98 Correct 615 ms 24760 KB Output is correct
99 Correct 843 ms 29364 KB Output is correct
100 Correct 901 ms 26888 KB Output is correct
101 Correct 846 ms 19912 KB Output is correct
102 Correct 780 ms 18628 KB Output is correct
103 Correct 810 ms 46680 KB Output is correct
104 Correct 842 ms 35864 KB Output is correct
105 Correct 809 ms 35752 KB Output is correct
106 Correct 818 ms 21680 KB Output is correct
107 Correct 774 ms 18776 KB Output is correct
108 Correct 860 ms 18712 KB Output is correct
109 Correct 706 ms 18896 KB Output is correct