답안 #635511

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
635511 2022-08-26T10:56:02 Z K4YAN Sumtree (INOI20_sumtree) C++17
100 / 100
1577 ms 280716 KB
//Be Name Khoda

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define all(x) x.begin(), x.end()
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

const ll mxn = 2e5 + 16, MX = 3e5 + 16, md = 1e9 + 7;
ll n, r, t, q, w, cnt, pw;
ll st[mxn], ft[mxn], dp[mxn], h[mxn], subt[mxn], R[mxn], fact[MX + mxn], taq[MX + mxn], par[mxn][19];
vector<ll> adj[mxn];
bitset<mxn> mark;
set<int> s[2 * MX];

inline ll tav(ll a, ll b) {
    ll res = 1;
    while(b > 0) {
        if(b & 1) res = res * a % md;
        a = a * a % md, b >>= 1;
    } return res;
}

inline ll chs(ll a, ll b) {
    if(a < 0 || b > a) return 0;
    return fact[a] * taq[b] % md * taq[a - b] % md;
}

inline void fun() {
    fact[0] = 1;
    for(int i = 1; i < MX + mxn; i++) {
        fact[i] = fact[i - 1] * i % md;
    } taq[MX + mxn - 1] = tav(fact[MX + mxn - 1], md - 2);
    for(int i = MX + mxn - 2; i > -1; i--) {
        taq[i] = taq[i + 1] * (i + 1) % md;
    } return;
}

void DFS(int v) {
    mark.set(v);
    st[v] = q++, subt[v] = 1;
    for(int i = 1; i < 19; i++) {
        par[v][i] = par[par[v][i - 1]][i - 1];
    } for(auto u : adj[v]) {
        if(!mark[u]) {
            par[u][0] = v;
            h[u] = h[v] + 1;
            DFS(u);
            subt[v] += subt[u];
        }
    } ft[v] = q;
    return;
}

inline int find_par(int v, int x) {
    for(int i = 18; i > -1; i--) {
        if(x & (1 << i)) {
            v = par[v][i];
            x -= (1 << i);
            if(x == 0) return v;
        }
    } return v;
}

struct segtree {

    int sz = 1;
    vector<pll> v;

    void init(ll n) {
        while(sz < n) sz <<= 1;
        v.assign(2 * sz, {0, 0});
        return;
    }
    void set(int i, pll p, int x, int lx, int rx) {
        if(rx - lx == 1) {
            v[x] = p;
            return;
        } int m = (lx + rx) >> 1;
        if(i < m) {
            set(i, p, 2 * x + 1, lx, m);
        } else {
            set(i, p, 2 * x + 2, m, rx);
        } v[x].first = v[2 * x + 1].first + v[2 * x + 2].first;
        v[x].second = v[2 * x + 1].second + v[2 * x + 2].second;
        return;
    }
    void set(int i, pll p) {
        set(i, p, 0, 0, sz);
        return;
    }
    pll ask(int l, int r, int x, int lx, int rx) {
        if(rx <= l || r <= lx) return {0, 0};
        if(l <= lx && rx <= r) {
            return v[x];
        } int m = (lx + rx) >> 1;
        pll a = ask(l, r, 2 * x + 1, lx, m);
        pll b = ask(l, r, 2 * x + 2, m, rx);
        return {a.first + b.first, a.second + b.second};
    }
    pll ask(int l, int r) {
        return ask(l, r, 0, 0, sz);
    }

}; segtree stt;

struct fp {

    int sz = 1;
    
    void init(ll n) {
        while(sz < n) sz <<= 1;
        return;
    }
    void add(int l, int r, int q, int x, int lx, int rx) {
        if(rx <= l || r <= lx) return;
        if(l <= lx && rx <= r) {
            s[x].insert(-q);
            return;
        } int m = (lx + rx) >> 1;
        add(l, r, q, 2 * x + 1, lx, m);
        add(l, r, q, 2 * x + 2, m, rx);
        return;
    }
    void add(int l, int r, int q) {
        add(l, r, q, 0, 0, sz);
        return;
    }
    int ask(int i, int x, int lx, int rx) {
        int a = 0;
        if(int(s[x].size()) > 0) {
            a = *(s[x].begin()), a = -a;
        }
        if(rx - lx == 1) {
            if(int(s[x].size()) == 0) return 0;
            return -1 * (*(s[x].begin()));
        } int m = (lx + rx) >> 1;
        if(i < m) {
            return max(a, ask(i, 2 * x + 1, lx, m));
        } else {
            return max(a, ask(i, 2 * x + 2, m, rx));
        }
    }
    int ask(int i) {
        return ask(i, 0, 0, sz);
    }
    void rem(int l, int r, int q, int x, int lx, int rx) {
        if(rx <= l || r <= lx) return;
        if(l <= lx && rx <= r) {
            s[x].erase(-q);
            return;
        } int m = (lx + rx) >> 1;
        rem(l, r, q, 2 * x + 1, lx, m);
        rem(l, r, q, 2 * x + 2, m, rx);
        return;
    }
    void rem(int l, int r, int q) {
        rem(l, r, q, 0, 0, sz);
        return;
    }

}; fp fpar;

inline void input() {

    cin >> n >> r;
    for(int i = 1; i < n; i++) {
        cin >> q >> w;
        adj[q].push_back(w);
        adj[w].push_back(q);
    } cin >> t;

}

inline void solve() {

    fun(); q = 0;
    DFS(1);
    fill(dp, dp + mxn, 1);
    dp[1] = pw = chs(r + n - 1, n - 1);
    cout << pw << "\n";
    stt.init(n), fpar.init(n);
    fpar.add(1, n, 0); R[1] = r;
    for(int tt = 0; tt < t; tt++) {
        cin >> q;
        if(q == 1) {
            cin >> q >> w;
            R[q] = w;
            ll v = fpar.ask(st[q]), x, rp, sp;
            pll p;
            v = find_par(q, h[q] - v);
            if(dp[v] == 0) cnt--;
            else pw = pw * tav(dp[v], md - 2) % md;
            p = stt.ask(st[q] + 1, ft[q]);
            rp = R[q] - p.first, sp = subt[q] - p.second;
            x = chs(rp + sp - 1, sp - 1);
            if(x == 0) cnt++, dp[q] = 0;
            else {
                dp[q] = x, pw = pw * x % md;
            } stt.set(st[q], {rp, sp});
            fpar.add(st[q] + 1, ft[q], h[q]);
            p = stt.ask(st[v] + 1, ft[v]);
            rp = R[v] - p.first, sp = subt[v] - p.second;
            x = chs(rp + sp - 1, sp - 1);
            if(x == 0) cnt++, dp[v] = 0;
            else {
                dp[v] = x, pw = pw * x % md;
            } stt.set(st[v], {rp, sp});
            if(cnt == 0) {
                cout << pw << "\n";
            } else {
                cout << "0\n";
            }
            continue;
        } cin >> q;
        ll v = fpar.ask(st[q]), x, rp, sp;
        pll p;
        v = find_par(q, h[q] - v);
        if(dp[q] == 0) {
            cnt--, dp[q] = 1;
        } else {
            pw = pw * tav(dp[q], md - 2) % md, dp[q] = 1;
        } R[q] = 0;
        stt.set(st[q], {0, 0});
        fpar.rem(st[q] + 1, ft[q], h[q]);
        if(dp[v] == 0) {
            cnt--;
        } else {
            pw = pw * tav(dp[v], md - 2) % md;
        } 
        p = stt.ask(st[v] + 1, ft[v]);
        rp = R[v] - p.first, sp = subt[v] - p.second;
        x = chs(rp + sp - 1, sp - 1);
        if(x == 0) {
            cnt++, dp[v] = 0;
        } else {
            dp[v] = x, pw = pw * x % md;
        } stt.set(st[v], {rp, sp});
        if(cnt == 0) {
            cout << pw << "\n";
        } else {
            cout << "0\n";
        }
    }
    return;

}

int main() {

    ios::sync_with_stdio(false); cin.tie(NULL);

    input(), solve();

    return 0;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 184 ms 101936 KB Output is correct
2 Correct 185 ms 101912 KB Output is correct
3 Correct 185 ms 101900 KB Output is correct
4 Correct 217 ms 101896 KB Output is correct
5 Correct 157 ms 96900 KB Output is correct
6 Correct 26 ms 43628 KB Output is correct
7 Correct 27 ms 43348 KB Output is correct
8 Correct 26 ms 43428 KB Output is correct
9 Correct 190 ms 94300 KB Output is correct
10 Correct 216 ms 94080 KB Output is correct
11 Correct 241 ms 94212 KB Output is correct
12 Correct 186 ms 92108 KB Output is correct
13 Correct 170 ms 97528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 42516 KB Output is correct
2 Correct 27 ms 42576 KB Output is correct
3 Correct 26 ms 42536 KB Output is correct
4 Correct 28 ms 42556 KB Output is correct
5 Correct 27 ms 42684 KB Output is correct
6 Correct 29 ms 43300 KB Output is correct
7 Correct 29 ms 43216 KB Output is correct
8 Correct 29 ms 43196 KB Output is correct
9 Correct 32 ms 43444 KB Output is correct
10 Correct 33 ms 43604 KB Output is correct
11 Correct 36 ms 43772 KB Output is correct
12 Correct 30 ms 43564 KB Output is correct
13 Correct 35 ms 43692 KB Output is correct
14 Correct 32 ms 43712 KB Output is correct
15 Correct 34 ms 43980 KB Output is correct
16 Correct 30 ms 43472 KB Output is correct
17 Correct 32 ms 43580 KB Output is correct
18 Correct 35 ms 43212 KB Output is correct
19 Correct 35 ms 43476 KB Output is correct
20 Correct 29 ms 43180 KB Output is correct
21 Correct 30 ms 43136 KB Output is correct
22 Correct 26 ms 42708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 232 ms 105308 KB Output is correct
2 Correct 283 ms 110632 KB Output is correct
3 Correct 251 ms 106328 KB Output is correct
4 Correct 408 ms 119624 KB Output is correct
5 Correct 855 ms 159164 KB Output is correct
6 Correct 32 ms 44116 KB Output is correct
7 Correct 28 ms 43428 KB Output is correct
8 Correct 29 ms 43800 KB Output is correct
9 Correct 436 ms 102440 KB Output is correct
10 Correct 390 ms 100764 KB Output is correct
11 Correct 384 ms 100196 KB Output is correct
12 Correct 401 ms 100756 KB Output is correct
13 Correct 1087 ms 233644 KB Output is correct
14 Correct 1577 ms 280716 KB Output is correct
15 Correct 1461 ms 271084 KB Output is correct
16 Correct 1451 ms 271028 KB Output is correct
17 Correct 1442 ms 271108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 621 ms 97912 KB Output is correct
2 Correct 603 ms 101632 KB Output is correct
3 Correct 588 ms 101648 KB Output is correct
4 Correct 659 ms 101772 KB Output is correct
5 Correct 573 ms 99284 KB Output is correct
6 Correct 581 ms 101636 KB Output is correct
7 Correct 476 ms 74152 KB Output is correct
8 Correct 464 ms 74276 KB Output is correct
9 Correct 578 ms 101772 KB Output is correct
10 Correct 571 ms 101864 KB Output is correct
11 Correct 573 ms 101708 KB Output is correct
12 Correct 428 ms 74320 KB Output is correct
13 Correct 273 ms 72148 KB Output is correct
14 Correct 327 ms 72660 KB Output is correct
15 Correct 340 ms 72920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 184 ms 101936 KB Output is correct
2 Correct 185 ms 101912 KB Output is correct
3 Correct 185 ms 101900 KB Output is correct
4 Correct 217 ms 101896 KB Output is correct
5 Correct 157 ms 96900 KB Output is correct
6 Correct 26 ms 43628 KB Output is correct
7 Correct 27 ms 43348 KB Output is correct
8 Correct 26 ms 43428 KB Output is correct
9 Correct 190 ms 94300 KB Output is correct
10 Correct 216 ms 94080 KB Output is correct
11 Correct 241 ms 94212 KB Output is correct
12 Correct 186 ms 92108 KB Output is correct
13 Correct 170 ms 97528 KB Output is correct
14 Correct 26 ms 42516 KB Output is correct
15 Correct 27 ms 42576 KB Output is correct
16 Correct 26 ms 42536 KB Output is correct
17 Correct 28 ms 42556 KB Output is correct
18 Correct 27 ms 42684 KB Output is correct
19 Correct 29 ms 43300 KB Output is correct
20 Correct 29 ms 43216 KB Output is correct
21 Correct 29 ms 43196 KB Output is correct
22 Correct 32 ms 43444 KB Output is correct
23 Correct 33 ms 43604 KB Output is correct
24 Correct 36 ms 43772 KB Output is correct
25 Correct 30 ms 43564 KB Output is correct
26 Correct 35 ms 43692 KB Output is correct
27 Correct 32 ms 43712 KB Output is correct
28 Correct 34 ms 43980 KB Output is correct
29 Correct 30 ms 43472 KB Output is correct
30 Correct 32 ms 43580 KB Output is correct
31 Correct 35 ms 43212 KB Output is correct
32 Correct 35 ms 43476 KB Output is correct
33 Correct 29 ms 43180 KB Output is correct
34 Correct 30 ms 43136 KB Output is correct
35 Correct 26 ms 42708 KB Output is correct
36 Correct 232 ms 105308 KB Output is correct
37 Correct 283 ms 110632 KB Output is correct
38 Correct 251 ms 106328 KB Output is correct
39 Correct 408 ms 119624 KB Output is correct
40 Correct 855 ms 159164 KB Output is correct
41 Correct 32 ms 44116 KB Output is correct
42 Correct 28 ms 43428 KB Output is correct
43 Correct 29 ms 43800 KB Output is correct
44 Correct 436 ms 102440 KB Output is correct
45 Correct 390 ms 100764 KB Output is correct
46 Correct 384 ms 100196 KB Output is correct
47 Correct 401 ms 100756 KB Output is correct
48 Correct 1087 ms 233644 KB Output is correct
49 Correct 1577 ms 280716 KB Output is correct
50 Correct 1461 ms 271084 KB Output is correct
51 Correct 1451 ms 271028 KB Output is correct
52 Correct 1442 ms 271108 KB Output is correct
53 Correct 621 ms 97912 KB Output is correct
54 Correct 603 ms 101632 KB Output is correct
55 Correct 588 ms 101648 KB Output is correct
56 Correct 659 ms 101772 KB Output is correct
57 Correct 573 ms 99284 KB Output is correct
58 Correct 581 ms 101636 KB Output is correct
59 Correct 476 ms 74152 KB Output is correct
60 Correct 464 ms 74276 KB Output is correct
61 Correct 578 ms 101772 KB Output is correct
62 Correct 571 ms 101864 KB Output is correct
63 Correct 573 ms 101708 KB Output is correct
64 Correct 428 ms 74320 KB Output is correct
65 Correct 273 ms 72148 KB Output is correct
66 Correct 327 ms 72660 KB Output is correct
67 Correct 340 ms 72920 KB Output is correct
68 Correct 26 ms 42648 KB Output is correct
69 Correct 25 ms 42552 KB Output is correct
70 Correct 732 ms 109380 KB Output is correct
71 Correct 730 ms 109292 KB Output is correct
72 Correct 764 ms 109376 KB Output is correct
73 Correct 785 ms 109384 KB Output is correct
74 Correct 860 ms 109704 KB Output is correct
75 Correct 799 ms 105192 KB Output is correct
76 Correct 587 ms 101680 KB Output is correct
77 Correct 648 ms 102160 KB Output is correct
78 Correct 691 ms 103244 KB Output is correct
79 Correct 752 ms 105584 KB Output is correct
80 Correct 794 ms 103768 KB Output is correct
81 Correct 833 ms 105468 KB Output is correct
82 Correct 396 ms 99076 KB Output is correct
83 Correct 469 ms 107364 KB Output is correct
84 Correct 499 ms 106416 KB Output is correct
85 Correct 489 ms 106256 KB Output is correct