Submission #826747

#TimeUsernameProblemLanguageResultExecution timeMemory
826747LittleCubeDigital Circuit (IOI22_circuit)C++17
100 / 100
870 ms33200 KiB
#include "circuit.h" #include <bits/stdc++.h> #define ll long long using namespace std; const ll mod = 1'000'002'022; namespace { int N, M; ll ways[200005], s[100005]; ll seg[400005], lazy[400005]; vector<int> child[100005]; void init_seg(int i = 1, int L = 0, int R = M - 1) { if (L == R) { seg[i] = s[L]; return; } int mid = (L + R) / 2; init_seg(i << 1, L, mid); init_seg(i << 1 | 1, mid + 1, R); seg[i] = (seg[i << 1] + seg[i << 1 | 1]) % mod; } ll ans = 0; ll modify(int mL, int mR, int i = 1, int L = 0, int R = M - 1) { if (mL <= L && R <= mR) { lazy[i] ^= 1, seg[i] = -seg[i]; return -seg[i]; } else if (R < mL || mR < L) return 0; else { int mid = (L + R) / 2; if (lazy[i]) { lazy[i] = 0; lazy[i << 1] ^= 1; lazy[i << 1 | 1] ^= 1; seg[i << 1] = -seg[i << 1]; seg[i << 1 | 1] = -seg[i << 1 | 1]; } ll res = (modify(mL, mR, i << 1, L, mid) + modify(mL, mR, i << 1 | 1, mid + 1, R)) % mod; seg[i] = (seg[i << 1] + seg[i << 1 | 1]) % mod; return res; } } void dfs(int u, ll up) { if (u >= N) s[u - N] = up; else { int k = child[u].size(); vector<ll> pre(k + 1, 1), suf(k + 1, 1); for (int i = 0; i < k; i++) pre[i + 1] = pre[i] * ways[child[u][i]] % mod; for (int i = k - 1; i >= 0; i--) suf[i] = suf[i + 1] * ways[child[u][i]] % mod; for (int i = 0; i < k; i++) dfs(child[u][i], up * pre[i] % mod * suf[i + 1] % mod); } } } void init(int N, int M, vector<int> P, vector<int> A) { ::M = M; ::N = N; for (int i = N + M - 1; i >= N; i--) { child[P[i]].emplace_back(i); ways[i] = 1; } for (int i = N - 1; i >= 0; i--) { if (i) child[P[i]].emplace_back(i); ways[i] = child[i].size(); for (int j : child[i]) ways[i] = ways[i] * ways[j] % mod; } dfs(0, 1); init_seg(); for (int i = 0; i < M; i++) if (A[i]) ans = (ans + modify(i, i)) % mod; } int count_ways(int L, int R) { L -= N, R -= N; ans = (ans + modify(L, R)) % mod; return (ans + mod) % mod; }
#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...