Submission #820244

#TimeUsernameProblemLanguageResultExecution timeMemory
820244maeolaDigital Circuit (IOI22_circuit)C++17
100 / 100
961 ms36204 KiB
#include "circuit.h" #include <bits/stdc++.h> using ll = long long; using namespace std; constexpr int mod = 1e9 + 2022; vector<int> adj[200005]; int choices[200005], coef[200005], *C; int N, M; struct ST { int pool[800005], total[800005]; int lazy[800005]; void build(int i, int l, int r) { if (l == r) { total[i] = C[l]; return; } int m = (l + r) / 2; build(i << 1, l, m); build(i << 1 | 1, m + 1, r); total[i] = (total[i << 1] + total[i << 1 | 1]) % mod; } void clean(int i) { if (lazy[i]) { pool[i] = (total[i] - pool[i] + mod) % mod; lazy[i << 1] ^= 1; lazy[i << 1 | 1] ^= 1; lazy[i] = 0; } } void flip(int i, int l, int r, int ql, int qr) { clean(i); if (qr < l or ql > r) return; if (ql <= l and qr >= r) { lazy[i] = 1; clean(i); return; } int m = (l + r) / 2; flip(i << 1, l, m, ql, qr); flip(i << 1 | 1, m + 1, r, ql, qr); pool[i] = (pool[i << 1] + pool[i << 1 | 1]) % mod; } } st; void dfs1(int i) { if (i >= N) return choices[i] = 1, void(); choices[i] = adj[i].size(); for (int j : adj[i]) { dfs1(j); choices[i] = 1ll * choices[i] * choices[j] % mod; } } void dfs2(int i, int x) { if (i >= N) { coef[i] = x; return; } int n = adj[i].size(); vector<int> l(n), r(n); for (int j = 0; j < n; j++) { int w = adj[i][j]; l[j] = r[j] = choices[w]; if (j) l[j] = 1ll * l[j] * l[j - 1] % mod; } for (int j = n - 2; j >= 0; j--) r[j] = 1ll * r[j] * r[j + 1] % mod; for (int j = 0; j < n; j++) { ll q = 1ll * x * (j ? l[j - 1] : 1) % mod; dfs2(adj[i][j], q * (j != n - 1 ? r[j + 1] : 1) % mod); } } void init(int N_, int M_, vector<int> P, vector<int> A) { N = N_, M = M_; for (int i = 1; i < N + M; i++) adj[P[i]].push_back(i); dfs1(0); dfs2(0, 1); C = coef + N; st.build(1, 0, M - 1); for (int i = 0; i < M; i++) if (A[i]) st.flip(1, 0, M - 1, i, i); } int count_ways(int L, int R) { L -= N, R -= N; st.flip(1, 0, M - 1, L, R); return st.pool[1]; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...