Submission #798024

#TimeUsernameProblemLanguageResultExecution timeMemory
798024gesghaDigital Circuit (IOI22_circuit)C++17
61 / 100
3050 ms28968 KiB
#include "circuit.h" #include <bits/stdc++.h> #define fr(i, a, b) for(int i = a; i <= b; i++) #define rf(i, a, b) for(int i = a; i >= b; i--) #define fe(x, y) for (auto& x : y) #define fi first #define se second #define pb push_back #define all(x) x.begin(), x.end() #define pw(x) (1LL << (x)) #define sz(x) (int)x.size() using namespace std; #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; #define fbo find_by_order #define ook order_of_key template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> using ve = vector <T>; template <typename T> bool umx (T& a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> bool umn (T& a, T b) { return a > b ? a = b, 1 : 0; } using ll = long long; using ld = long double; using pll = pair <ll, ll>; using pii = pair <int, int>; using ull = unsigned long long; const int oo = 1e9; const ll OO = 1e18; const int N = 2e5 + 10; const int M = 5e3 + 100; const int mod = 1e9 + 2022; int a[N]; int dp[N][2]; int dp1[N][2]; ve <int> G[N]; bool ok = 1; int n, m; int add(int a, int b) { return a + b < mod ? a + b : a + b - mod; } int sub(int a, int b) { return a - b >= 0 ? a - b : a - b + mod; } int mul (int a, int b) { return 1LL * a * b % mod; } int bpow(int a, int n) { int res = 1; for (; n; n >>= 1, a = mul(a, a)) if(n&1) res = mul(res, a); return res; } int tin[N]; int tout[N]; int tim; int mn[N]; int mx[N]; bool isupper(int u, int v) { return tin[u] <= tin[v] && tout[v] <= tout[u]; } int dead[N]; int S[4 * N]; int res[4 * N]; void build(int v, int tl, int tr) { if (tl == tr) { S[v] = bpow(2, n - dead[tl + n]); if (a[tl + n]) res[v] = bpow(2, n - dead[tl + n]); // cout << tl << " - " << res[v] << "\n"; return; } int tm = (tl + tr) >> 1; build(v << 1, tl, tm); build(v << 1 | 1, tm + 1, tr); S[v] = add(S[v << 1], S[v << 1 | 1]); res[v] = add(res[v << 1], res[v << 1 | 1]); } int rev[N * 4]; void push(int v) { if (!rev[v]) return; for (auto to : {(v << 1), (v << 1 | 1)}) { rev[to] ^= 1; res[to] = sub(S[to], res[to]); } rev[v] = 0; } void upd(int v, int tl, int tr, int L, int R) { // cout << tl << " " << tr << " " << L << " " << R << "\n"; if (L > R) return; if (tl == L && tr == R) { res[v] = sub(S[v], res[v]); rev[v] ^= 1; return; } int tm = (tl + tr) >> 1; push(v); upd(v << 1, tl, tm, L, min(R, tm)); upd(v << 1 | 1, tm + 1, tr, max(L, tm + 1), R); res[v] = add(res[v << 1], res[v << 1 | 1]); } void dfs(int u) { tin[u] = tim++; if (!sz(G[u])) { mn[u] = u; mx[u] = u; if(a[u]) { dp[u][0] = 0; dp[u][1] = 1; } else { dp[u][0] = 1; dp[u][1] = 0; } return; } mx[u] = -oo; mn[u] = oo; for (auto to : G[u]) { dead[to] = dead[u] + 1; dfs(to); mx[u] = max(mx[u], mx[to]); mn[u] = min(mn[u], mn[to]); } ve <ve <int>> d(sz(G[u]) + 1); fe (x, d) x.resize(sz(G[u]) + 1); d[0][0] = 1; for (int i = 0; i < sz(G[u]); i++) { int to = G[u][i]; for (int cnt_al = 0; cnt_al <= i; cnt_al++) { d[i + 1][cnt_al + 1] = add(d[i + 1][cnt_al + 1], mul(d[i][cnt_al], dp[to][1])); d[i + 1][cnt_al] = add(d[i + 1][cnt_al], mul(d[i][cnt_al], dp[to][0])); } } dp[u][0] = dp[u][1] = 0; for (int cnt_al = 0; cnt_al <= sz(G[u]); cnt_al++) { dp[u][1] = add(dp[u][1], mul(d[sz(G[u])][cnt_al], max(0, cnt_al))); dp[u][0] = add(dp[u][0], mul(d[sz(G[u])][cnt_al], sz(G[u]) - cnt_al)); } tout[u] = tim; } void init(int N, int M, vector<int> P, vector<int> A) { n = N; m = M; for (int i = 0; i < M; i++) a[i + N] = A[i]; for (int i = 1; i < N + M; i++) G[P[i]].pb(i); for (int i = 0; i < N; i++) ok &= bool(sz(G[i]) == 2); dfs(0); if (ok) { build(1, 0, m - 1); } } int count_ways(int L, int R) { if (!ok) { for (int i = L; i <= R; i++) { a[i] = 1 - a[i]; } dfs(0); } else { L -= n; R -= n; upd(1, 0, m - 1, L, R); return res[1]; } return dp[0][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...