Submission #773920

#TimeUsernameProblemLanguageResultExecution timeMemory
773920ParsaSSumtree (INOI20_sumtree)C++17
30 / 100
304 ms92108 KiB
// In the name of God #include<bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define mp make_pair typedef long long ll; const int N = 1e6 + 5, MOD = 1e9 + 7; int n, r; int fact[N], inv[N], f[N], c[N]; int val[N], par[N]; vector<int> adj[N]; int binpow(int a, int b) { return b ? 1LL * binpow(1LL * a * a % MOD, b / 2) * (b % 2 ? a : 1) % MOD : 1; } int C(int a, int b) { return 1LL * fact[b] * inv[a] % MOD * inv[b - a] % MOD; } void dfs(int v, int p) { c[v] = 1; par[v] = p; for (auto u : adj[v]) { if (u != p) { dfs(u, v); c[v] += c[u]; } } } int F(int v) { return C(c[v], val[v] - f[v] + c[v]); } void solve() { cin >> n >> r; for (int i = 0; i < n - 1; i++) { int v, u; cin >> v >> u; adj[v].pb(u), adj[u].pb(v); } int ans = C(n - 1, r + n - 1); val[1] = r; dfs(1, 0); c[1]--; cout << ans << '\n'; int q; cin >> q; memset(val, -1, sizeof val); val[1] = r; while (q--) { int t, v; cin >> t >> v; if (t == 1) { int x; cin >> x; int u = par[v]; while (val[u] == -1) { c[u] -= c[v]; f[u] += x - f[v]; u = par[u]; } ans = 1LL * ans * binpow(F(u), MOD - 2) % MOD; c[u] -= c[v]; f[u] += x - f[v]; c[v]--; val[v] = x; ans = 1LL * ans * F(u) % MOD * F(v) % MOD; } else { int u = par[v]; while (val[u] == -1) { c[u] += c[v] + 1; f[u] += f[v] - val[v]; u = par[u]; } ans = 1LL * ans * binpow(1LL * F(v) * F(u) % MOD, MOD - 2) % MOD; //cout << " ! " << F(v) << ' ' << F(u) << endl; f[u] += f[v] - val[v]; c[u] += c[v] + 1; c[v]++; val[v] = -1; ans = 1LL * ans * F(u) % MOD; //cout << "? " << c[u] << ' '<< f[u] << endl; } cout << ans << '\n'; } } int32_t main() { fact[0] = 1; for (int i = 1; i < N; i++) fact[i] = 1LL * i * fact[i - 1] % MOD; inv[N - 1] = binpow(fact[N - 1], MOD - 2); for (int i = N - 2; i >= 0; i--) inv[i] = 1LL * inv[i + 1] * (i + 1) % MOD; ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); solve(); return 0; }
#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...