Submission #635630

# Submission time Handle Problem Language Result Execution time Memory
635630 2022-08-26T14:03:52 Z MohammadAghil Sumtree (INOI20_sumtree) C++17
100 / 100
564 ms 92932 KB
#include <bits/stdc++.h>
//#pragma GCC optimize ("Ofast,unroll-loops")
//#pragma GCC target ("avx2")
using namespace std;

typedef long long ll;
typedef pair<int, int> pp;

#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back
#define ff first
#define ss second

void dbg(){
    cerr << endl;
}
template<typename Head, typename... Tail> void dbg(Head h, Tail... t){
    cerr << " " << h << ",";
    dbg(t...);
}
#define er(...) cerr << __LINE__ << " <" << #__VA_ARGS__ << ">:", dbg(__VA_ARGS__)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void IOS(){
    cin.tie(0) -> sync_with_stdio(0);
//    #ifndef ONLINE_JUDGE
//        freopen("inp.txt", "r", stdin);
//        freopen("out.txt", "w", stdout);
//    #endif
}

const ll mod = 1e9 + 7, maxn = 1e6 + 5, lg = 21, inf = ll(1e9) + 5;

struct SegMul{
    vector<ll> seg;
    int n;
    SegMul(int n): n(n){
        seg.assign(n<<1, 1);
    }
    void upd(int i, ll k){
        for(i += n, seg[i] = k; i > 1; i >>= 1) seg[i>>1] = seg[i]*seg[i^1]%mod;
    }
    ll get(){
        return seg[1];
    }
};

struct Fen{
    vector<ll> fen;
    Fen(int n){
        fen.assign(n + 5, 0);
    }
    void upd(int i, ll k){
        for(i++; i < sz(fen); i += i&-i) fen[i] += k;
    }
    ll get(int i){
        ll res = 0;
        for(i++; i; i -= i&-i) res += fen[i];
        return res;
    }
    ll get(int l, int r){
        return get(r - 1) - get(l - 1);
    }
};

ll pw(ll a, ll b){
    if(!b) return 1;
    ll k = pw(a, b>>1ll);
    return  k*k%mod*(b&1ll?a:1ll)%mod;
}

ll fac[maxn], inv[maxn];

ll C(int n, int r){
    if(r < 0 || r > n) return 0;
    return fac[n]*inv[r]%mod*inv[n-r]%mod;
}

vector<int> adj[maxn];
int st[maxn], en[maxn],  par[maxn][lg], t, cnt[maxn];
ll val[maxn];

void dfs(int r, int p){
    cnt[r] = 1;
    st[r] = t++;
    par[r][0] = p;
    rep(i,1,lg) par[r][i] = par[par[r][i-1]][i-1];
    for(int c: adj[r]) if(c - p){
        dfs(c, r);
        cnt[r] += cnt[c];
    }
    en[r] = t;
}

int main(){ IOS();
    fac[0] = inv[0] = 1;

    rep(i,1,maxn){
        fac[i] = fac[i-1]*i%mod;
    }
    inv[maxn-1] = pw(fac[maxn-1], mod-2);
    per(i,maxn-2,0) inv[i] = inv[i + 1]*(i + 1)%mod;

    int n, r; cin >> n >> r;
    val[0] = r;

    rep(i,1,n){
        int u, v; cin >> u >> v; u--, v--;
        adj[u].pb(v), adj[v].pb(u);
    }
    dfs(0, 0);

    SegMul smul(n); smul.upd(0, C(r + n - 1, r));
    Fen fen_par(n); fen_par.upd(st[0], 1), fen_par.upd(en[0], -1);
    Fen cnt_sub(n); cnt_sub.upd(0, n);
    Fen sum_val(n); sum_val.upd(0, r);

    auto get_par = [&](int r){
        int vl = fen_par.get(st[r]);
        per(i,lg-1,0){
            int p = par[r][i];
            if(vl == fen_par.get(st[p])) r = p;
        }
        return r;
    };

    auto add = [&](int r, int x){
        int p = get_par(r);
        fen_par.upd(st[r], 1), fen_par.upd(en[r], -1);
        ll fl = cnt_sub.get(st[r], en[r]);
        ll emp = cnt[r] - fl;
//        er(r, x, p, emp);
        cnt_sub.upd(st[r], emp);
        cnt_sub.upd(st[p], -emp);
        sum_val.upd(st[r], val[r] = x - sum_val.get(st[r], en[r]));
        smul.upd(r, C(val[r] + emp - 1, val[r]));
        sum_val.upd(st[p], -val[r]); val[p] -= val[r];
        emp = cnt_sub.get(st[p], st[p] + 1);
        smul.upd(p, C(val[p] + emp - 1, val[p]));
    };

    auto rem = [&](int r){
        fen_par.upd(st[r], -1), fen_par.upd(en[r], 1);
        int p = get_par(r);
        int tmp;
        cnt_sub.upd(st[r], tmp = -cnt_sub.get(st[r], st[r] + 1));
        cnt_sub.upd(st[p], -tmp);
        sum_val.upd(st[r], -val[r]);
        smul.upd(r, 1);
        sum_val.upd(st[p], val[r]); val[p] += val[r], val[r] = 0;
        int emp = cnt_sub.get(st[p], st[p] + 1);
        smul.upd(p, C(val[p] + emp - 1, val[p]));
    };

    int q; cin >> q;
    cout << smul.get() << '\n';
    while(q--){
        int t; cin >> t;
        if(t == 1){
            int u, x; cin >> u >> x; u--;
            add(u, x);
        }else{
            int u; cin >> u; u--;
            rem(u);
        }
        cout << smul.get() << '\n';
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 187 ms 80668 KB Output is correct
2 Correct 183 ms 80764 KB Output is correct
3 Correct 176 ms 80748 KB Output is correct
4 Correct 184 ms 80856 KB Output is correct
5 Correct 155 ms 76760 KB Output is correct
6 Correct 32 ms 40228 KB Output is correct
7 Correct 31 ms 40012 KB Output is correct
8 Correct 31 ms 40048 KB Output is correct
9 Correct 186 ms 73136 KB Output is correct
10 Correct 189 ms 72908 KB Output is correct
11 Correct 187 ms 73224 KB Output is correct
12 Correct 175 ms 71500 KB Output is correct
13 Correct 164 ms 77656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 39500 KB Output is correct
2 Correct 30 ms 39484 KB Output is correct
3 Correct 30 ms 39456 KB Output is correct
4 Correct 30 ms 39508 KB Output is correct
5 Correct 33 ms 39572 KB Output is correct
6 Correct 34 ms 39860 KB Output is correct
7 Correct 33 ms 39900 KB Output is correct
8 Correct 33 ms 39892 KB Output is correct
9 Correct 34 ms 40012 KB Output is correct
10 Correct 33 ms 40076 KB Output is correct
11 Correct 32 ms 40136 KB Output is correct
12 Correct 32 ms 40016 KB Output is correct
13 Correct 34 ms 40124 KB Output is correct
14 Correct 34 ms 40012 KB Output is correct
15 Correct 34 ms 40396 KB Output is correct
16 Correct 35 ms 40012 KB Output is correct
17 Correct 33 ms 40012 KB Output is correct
18 Correct 35 ms 39764 KB Output is correct
19 Correct 35 ms 40012 KB Output is correct
20 Correct 31 ms 39840 KB Output is correct
21 Correct 31 ms 39776 KB Output is correct
22 Correct 32 ms 39568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 217 ms 81660 KB Output is correct
2 Correct 229 ms 82060 KB Output is correct
3 Correct 210 ms 82440 KB Output is correct
4 Correct 276 ms 82744 KB Output is correct
5 Correct 333 ms 79552 KB Output is correct
6 Correct 35 ms 40268 KB Output is correct
7 Correct 32 ms 40016 KB Output is correct
8 Correct 31 ms 40048 KB Output is correct
9 Correct 332 ms 75788 KB Output is correct
10 Correct 306 ms 75408 KB Output is correct
11 Correct 278 ms 75468 KB Output is correct
12 Correct 314 ms 75032 KB Output is correct
13 Correct 276 ms 92932 KB Output is correct
14 Correct 276 ms 92864 KB Output is correct
15 Correct 256 ms 92824 KB Output is correct
16 Correct 267 ms 92872 KB Output is correct
17 Correct 256 ms 92788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 468 ms 76944 KB Output is correct
2 Correct 406 ms 77236 KB Output is correct
3 Correct 403 ms 77344 KB Output is correct
4 Correct 418 ms 77516 KB Output is correct
5 Correct 400 ms 75336 KB Output is correct
6 Correct 435 ms 77216 KB Output is correct
7 Correct 278 ms 59608 KB Output is correct
8 Correct 315 ms 59472 KB Output is correct
9 Correct 422 ms 77396 KB Output is correct
10 Correct 386 ms 77488 KB Output is correct
11 Correct 403 ms 77416 KB Output is correct
12 Correct 273 ms 59576 KB Output is correct
13 Correct 223 ms 57136 KB Output is correct
14 Correct 236 ms 57640 KB Output is correct
15 Correct 274 ms 57804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 187 ms 80668 KB Output is correct
2 Correct 183 ms 80764 KB Output is correct
3 Correct 176 ms 80748 KB Output is correct
4 Correct 184 ms 80856 KB Output is correct
5 Correct 155 ms 76760 KB Output is correct
6 Correct 32 ms 40228 KB Output is correct
7 Correct 31 ms 40012 KB Output is correct
8 Correct 31 ms 40048 KB Output is correct
9 Correct 186 ms 73136 KB Output is correct
10 Correct 189 ms 72908 KB Output is correct
11 Correct 187 ms 73224 KB Output is correct
12 Correct 175 ms 71500 KB Output is correct
13 Correct 164 ms 77656 KB Output is correct
14 Correct 30 ms 39500 KB Output is correct
15 Correct 30 ms 39484 KB Output is correct
16 Correct 30 ms 39456 KB Output is correct
17 Correct 30 ms 39508 KB Output is correct
18 Correct 33 ms 39572 KB Output is correct
19 Correct 34 ms 39860 KB Output is correct
20 Correct 33 ms 39900 KB Output is correct
21 Correct 33 ms 39892 KB Output is correct
22 Correct 34 ms 40012 KB Output is correct
23 Correct 33 ms 40076 KB Output is correct
24 Correct 32 ms 40136 KB Output is correct
25 Correct 32 ms 40016 KB Output is correct
26 Correct 34 ms 40124 KB Output is correct
27 Correct 34 ms 40012 KB Output is correct
28 Correct 34 ms 40396 KB Output is correct
29 Correct 35 ms 40012 KB Output is correct
30 Correct 33 ms 40012 KB Output is correct
31 Correct 35 ms 39764 KB Output is correct
32 Correct 35 ms 40012 KB Output is correct
33 Correct 31 ms 39840 KB Output is correct
34 Correct 31 ms 39776 KB Output is correct
35 Correct 32 ms 39568 KB Output is correct
36 Correct 217 ms 81660 KB Output is correct
37 Correct 229 ms 82060 KB Output is correct
38 Correct 210 ms 82440 KB Output is correct
39 Correct 276 ms 82744 KB Output is correct
40 Correct 333 ms 79552 KB Output is correct
41 Correct 35 ms 40268 KB Output is correct
42 Correct 32 ms 40016 KB Output is correct
43 Correct 31 ms 40048 KB Output is correct
44 Correct 332 ms 75788 KB Output is correct
45 Correct 306 ms 75408 KB Output is correct
46 Correct 278 ms 75468 KB Output is correct
47 Correct 314 ms 75032 KB Output is correct
48 Correct 276 ms 92932 KB Output is correct
49 Correct 276 ms 92864 KB Output is correct
50 Correct 256 ms 92824 KB Output is correct
51 Correct 267 ms 92872 KB Output is correct
52 Correct 256 ms 92788 KB Output is correct
53 Correct 468 ms 76944 KB Output is correct
54 Correct 406 ms 77236 KB Output is correct
55 Correct 403 ms 77344 KB Output is correct
56 Correct 418 ms 77516 KB Output is correct
57 Correct 400 ms 75336 KB Output is correct
58 Correct 435 ms 77216 KB Output is correct
59 Correct 278 ms 59608 KB Output is correct
60 Correct 315 ms 59472 KB Output is correct
61 Correct 422 ms 77396 KB Output is correct
62 Correct 386 ms 77488 KB Output is correct
63 Correct 403 ms 77416 KB Output is correct
64 Correct 273 ms 59576 KB Output is correct
65 Correct 223 ms 57136 KB Output is correct
66 Correct 236 ms 57640 KB Output is correct
67 Correct 274 ms 57804 KB Output is correct
68 Correct 32 ms 39368 KB Output is correct
69 Correct 29 ms 39508 KB Output is correct
70 Correct 497 ms 85072 KB Output is correct
71 Correct 532 ms 84996 KB Output is correct
72 Correct 556 ms 84968 KB Output is correct
73 Correct 564 ms 85080 KB Output is correct
74 Correct 540 ms 85076 KB Output is correct
75 Correct 528 ms 81124 KB Output is correct
76 Correct 391 ms 77388 KB Output is correct
77 Correct 437 ms 77132 KB Output is correct
78 Correct 446 ms 76236 KB Output is correct
79 Correct 518 ms 81228 KB Output is correct
80 Correct 523 ms 79308 KB Output is correct
81 Correct 524 ms 80892 KB Output is correct
82 Correct 328 ms 74636 KB Output is correct
83 Correct 350 ms 83116 KB Output is correct
84 Correct 391 ms 82124 KB Output is correct
85 Correct 394 ms 82032 KB Output is correct