Submission #773942

# Submission time Handle Problem Language Result Execution time Memory
773942 2023-07-05T09:47:44 Z ParsaS Sumtree (INOI20_sumtree) C++17
50 / 100
3000 ms 62668 KB
// 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 ans, bad;

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 a >= 0 && a <= b ? 1LL * fact[b] * inv[a] % MOD * inv[b - a] % MOD : 0;
}
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 add(int v) {
    if (F(v) == 0)
        bad++;
    else
        ans = 1LL * ans * F(v) % MOD;
}
void rem(int v) {
    if (F(v) == 0)
        bad--;
    else
        ans = 1LL * ans * binpow(F(v), MOD - 2) % MOD;
}

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);
    }
    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];
            }
            rem(u);
            c[u] -= c[v];
            f[u] += x - f[v];

            c[v]--;
            val[v] = x;

            add(u);
            add(v);
        }
        else {
            int u = par[v]; 
            while (val[u] == -1) {
                c[u] += c[v] + 1;
                f[u] += f[v] - val[v];
                u = par[u];
            }
            rem(v);
            rem(u);
            //cout << " ! " << F(v) << ' ' << F(u) << endl;
            f[u] += f[v] - val[v];
            c[u] += c[v] + 1;
            c[v]++;
            val[v] = -1;

            add(u);
        }
        if (bad == 0)
            cout << ans << '\n';
        else
            cout << 0 << '\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 time Memory Grader output
1 Correct 96 ms 51172 KB Output is correct
2 Correct 99 ms 51244 KB Output is correct
3 Correct 130 ms 51220 KB Output is correct
4 Correct 95 ms 51184 KB Output is correct
5 Correct 97 ms 47220 KB Output is correct
6 Correct 29 ms 35808 KB Output is correct
7 Correct 29 ms 35600 KB Output is correct
8 Correct 30 ms 35668 KB Output is correct
9 Correct 90 ms 43596 KB Output is correct
10 Correct 88 ms 43492 KB Output is correct
11 Correct 90 ms 43508 KB Output is correct
12 Correct 83 ms 43104 KB Output is correct
13 Correct 83 ms 50452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 35480 KB Output is correct
2 Correct 29 ms 35540 KB Output is correct
3 Correct 28 ms 35508 KB Output is correct
4 Correct 28 ms 35524 KB Output is correct
5 Correct 28 ms 35576 KB Output is correct
6 Correct 29 ms 35564 KB Output is correct
7 Correct 30 ms 35656 KB Output is correct
8 Correct 29 ms 35576 KB Output is correct
9 Correct 31 ms 35724 KB Output is correct
10 Correct 31 ms 35788 KB Output is correct
11 Correct 34 ms 35784 KB Output is correct
12 Correct 29 ms 35748 KB Output is correct
13 Correct 30 ms 35724 KB Output is correct
14 Correct 30 ms 35660 KB Output is correct
15 Correct 31 ms 35940 KB Output is correct
16 Correct 30 ms 35700 KB Output is correct
17 Correct 30 ms 35732 KB Output is correct
18 Correct 30 ms 35660 KB Output is correct
19 Correct 30 ms 35644 KB Output is correct
20 Correct 30 ms 35612 KB Output is correct
21 Correct 29 ms 35600 KB Output is correct
22 Correct 29 ms 35540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 114 ms 51196 KB Output is correct
2 Correct 108 ms 51628 KB Output is correct
3 Correct 122 ms 52068 KB Output is correct
4 Correct 121 ms 52372 KB Output is correct
5 Correct 143 ms 49168 KB Output is correct
6 Correct 31 ms 35924 KB Output is correct
7 Correct 29 ms 35684 KB Output is correct
8 Correct 30 ms 35736 KB Output is correct
9 Correct 148 ms 45348 KB Output is correct
10 Correct 136 ms 45116 KB Output is correct
11 Correct 135 ms 45024 KB Output is correct
12 Correct 132 ms 45496 KB Output is correct
13 Execution timed out 3068 ms 62668 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 298 ms 46352 KB Output is correct
2 Correct 279 ms 46268 KB Output is correct
3 Correct 255 ms 46328 KB Output is correct
4 Correct 286 ms 46276 KB Output is correct
5 Correct 274 ms 45896 KB Output is correct
6 Correct 243 ms 46340 KB Output is correct
7 Correct 206 ms 41884 KB Output is correct
8 Correct 228 ms 41960 KB Output is correct
9 Correct 284 ms 46372 KB Output is correct
10 Correct 288 ms 46408 KB Output is correct
11 Correct 270 ms 46312 KB Output is correct
12 Correct 226 ms 42016 KB Output is correct
13 Correct 185 ms 40076 KB Output is correct
14 Correct 212 ms 43488 KB Output is correct
15 Correct 184 ms 43624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 96 ms 51172 KB Output is correct
2 Correct 99 ms 51244 KB Output is correct
3 Correct 130 ms 51220 KB Output is correct
4 Correct 95 ms 51184 KB Output is correct
5 Correct 97 ms 47220 KB Output is correct
6 Correct 29 ms 35808 KB Output is correct
7 Correct 29 ms 35600 KB Output is correct
8 Correct 30 ms 35668 KB Output is correct
9 Correct 90 ms 43596 KB Output is correct
10 Correct 88 ms 43492 KB Output is correct
11 Correct 90 ms 43508 KB Output is correct
12 Correct 83 ms 43104 KB Output is correct
13 Correct 83 ms 50452 KB Output is correct
14 Correct 30 ms 35480 KB Output is correct
15 Correct 29 ms 35540 KB Output is correct
16 Correct 28 ms 35508 KB Output is correct
17 Correct 28 ms 35524 KB Output is correct
18 Correct 28 ms 35576 KB Output is correct
19 Correct 29 ms 35564 KB Output is correct
20 Correct 30 ms 35656 KB Output is correct
21 Correct 29 ms 35576 KB Output is correct
22 Correct 31 ms 35724 KB Output is correct
23 Correct 31 ms 35788 KB Output is correct
24 Correct 34 ms 35784 KB Output is correct
25 Correct 29 ms 35748 KB Output is correct
26 Correct 30 ms 35724 KB Output is correct
27 Correct 30 ms 35660 KB Output is correct
28 Correct 31 ms 35940 KB Output is correct
29 Correct 30 ms 35700 KB Output is correct
30 Correct 30 ms 35732 KB Output is correct
31 Correct 30 ms 35660 KB Output is correct
32 Correct 30 ms 35644 KB Output is correct
33 Correct 30 ms 35612 KB Output is correct
34 Correct 29 ms 35600 KB Output is correct
35 Correct 29 ms 35540 KB Output is correct
36 Correct 114 ms 51196 KB Output is correct
37 Correct 108 ms 51628 KB Output is correct
38 Correct 122 ms 52068 KB Output is correct
39 Correct 121 ms 52372 KB Output is correct
40 Correct 143 ms 49168 KB Output is correct
41 Correct 31 ms 35924 KB Output is correct
42 Correct 29 ms 35684 KB Output is correct
43 Correct 30 ms 35736 KB Output is correct
44 Correct 148 ms 45348 KB Output is correct
45 Correct 136 ms 45116 KB Output is correct
46 Correct 135 ms 45024 KB Output is correct
47 Correct 132 ms 45496 KB Output is correct
48 Execution timed out 3068 ms 62668 KB Time limit exceeded
49 Halted 0 ms 0 KB -