Submission #636258

# Submission time Handle Problem Language Result Execution time Memory
636258 2022-08-28T16:21:30 Z Mahdi Sumtree (INOI20_sumtree) C++17
100 / 100
718 ms 68120 KB
#include<bits/stdc++.h>
#pragma GCC optimize ("Ofast")
using namespace std;
#define all(v) v.begin(), v.end()
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> pii;
const int N=2e5+5, M=1e9+7;
int n, r, q, f[5*N/2], rf[5*N/2], ans, cmo, siz[N];
int ss[N], pr[N][18], st[N], fn[N];
vector<int>g[N];
ll tp[N];

inline int tav(int x, int y){
    ll res=1;
    while(y){
        if(y&1)
            res=res*x%M;
        y>>=1;
        x=1LL*x*x%M;
    }
    return res;
}

inline int rev(const int &x){
    return tav(x, M-2); 
}

void pre(){
    f[0]=1;
    int t=5*N/2;
    for(int i=1;i<t;++i)
        f[i]=1LL*f[i-1]*i%M;
    rf[t-1]=rev(f[t-1]);
    for(int i=t-2;i>=0;--i)
        rf[i]=1LL*rf[i+1]*(i+1)%M;
}

int C(int x, int y){
    ll res=1LL*f[x]*rf[y]%M;
    return res*rf[x-y]%M;
}

void dfs(int &tm, const int &v, const int &p=-1){
    st[v]=tm++;
    ss[v]=1;
    for(int u: g[v]){
        if(u!=p){
            pr[u][0]=v;
            for(int i=1;i<18;++i)
                pr[u][i]=pr[pr[u][i-1]][i-1];
            dfs(tm, u, v);
            ss[v]+=ss[u];
        }
    }
    fn[v]=tm;
}

void dfs(){
    int tm=0;
    dfs(tm, 0);
}

struct seg_tree{
    int sz, h[3*N];
    ll a[3*N], b[3*N];
    
    void add(int x, int lx, int rx, int l, int r, int val){
        if(lx>=r || rx<=l)
            return;
        if(lx>=l && rx<=r){
            h[x]+=val;
            return;
        }
        int m=(lx+rx)/2;
        add(2*x+1, lx, m, l, r, val);
        add(2*x+2, m, rx, l, r, val);
    }

    int num(int x){
        x+=sz-1;
        int res=h[x];
        while(x){
            --x;
            x/=2;
            res+=h[x];
        }
        return res;
    }

    void adda(int x, int val){
        x+=sz-1;
        a[x]+=val;
        while(x){
            --x;
            x/=2;
            a[x]+=val;
        }
    }

    ll suma(int x, int lx, int rx, int l, int r){
        if(lx>=r || rx<=l)
            return 0;
        if(lx>=l && rx<=r)
            return a[x];
        int m=(lx+rx)/2;
        return suma(2*x+1, lx, m, l, r)+suma(2*x+2, m, rx, l, r);
    }

    void addb(int x, int val){
        x+=sz-1;
        b[x]+=val;
        while(x){
            --x;
            x/=2;
            b[x]+=val;
        }
    }

    ll sumb(int x, int lx, int rx, int l, int r){
        //cout<<"shit "<<" : "<<x<<", "<<lx<<", "<<rx<<", "<<l<<", "<<r<<" : "<<b[x]<<'\n';
        if(lx>=r || rx<=l)
            return 0;
        if(lx>=l && rx<=r)
            return b[x];
        int m=(lx+rx)/2;
        return sumb(2*x+1, lx, m, l, r)+sumb(2*x+2, m, rx, l, r);
    }
    
    void upd(){
        sz=1;
        while(sz<n)
            sz<<=1;
        add(0, 0, sz, 0, n, 1);
        adda(0, n);
        addb(0, r);
        //cout<<"wtf: ";
        sumb(0, 0, sz, 0, n);
        /*cout<<"come on : ";
        for(int i=0;i<2*sz;++i)
            cout<<b[i]<<' ';
        cout<<'\n';
        cout<<"------------------------------\n";*/
    }
} A;

int par(int v){
    int h=A.num(st[v]);
    for(int i=17;i>=0;--i){
        int u=pr[v][i];
        if(A.num(st[u])==h)
            v=u;
    }
    return v;
}

inline void du(const int &u){
    if(tp[u]<0)
        ++cmo;
    else
        ans=1LL*ans*C(siz[u]+tp[u]-1, siz[u]-1)%M;
}

inline void ud(const int &u){
    if(tp[u]<0)
        --cmo;
    else
        ans=1LL*ans*rev(C(siz[u]+tp[u]-1, siz[u]-1))%M;
}

void add_u(int u, int v){
    int w=par(u);
    //cout<<"fuck : "<<w<<'\n';
    A.add(0, 0, A.sz, st[u], fn[u], 1);
    ud(w);
    siz[u]=ss[u]-A.suma(0, 0, A.sz, st[u], fn[u]);
    tp[u]=v-A.sumb(0, 0, A.sz, st[u], fn[u]);
    du(u);
    A.adda(st[u], siz[u]);
    A.addb(st[u], tp[u]);
    siz[w]-=siz[u];
    tp[w]-=tp[u];
    du(w);
    A.adda(st[w], -siz[u]);
    A.addb(st[w], -tp[u]);
}

void erase_u(int u){
    A.add(0, 0, A.sz, st[u], fn[u], -1);
    int w=par(u);
    //cout<<"fuck :: "<<w<<'\n';
    ud(u);
    ud(w);
    siz[w]+=siz[u];
    tp[w]+=tp[u];
    du(w);
    A.adda(st[u], -siz[u]);
    A.addb(st[u], -tp[u]);
    A.adda(st[w], siz[u]);
    A.addb(st[w], tp[u]);
    siz[u]=tp[u]=0;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    pre();
    cin>>n>>r;
    for(int i=1;i<n;++i){
        int u, v;
        cin>>u>>v;
        g[--u].push_back(--v);
        g[v].push_back(u);
    }
    dfs();
    A.upd();
    siz[0]=n;
    tp[0]=r;
    ans=C(r+n-1, n-1);
    /*for(int i=0;i<n;++i)
        cout<<A.sumb(0, 0, A.sz, st[i], fn[i])<<' ';
    cout<<'\n';*/
    cout<<ans<<'\n';
    cin>>q;
    while(q--){
        int t, u;
        cin>>t>>u;
        --u;
        if(t==1){
            int v;
            cin>>v;
            add_u(u, v);
        }
        else
            erase_u(u);
        //cout<<"ans = ";
        if(cmo==0)
            cout<<ans<<'\n';
        else
            cout<<0<<'\n';
        /*for(int i=0;i<n;++i)
            cout<<siz[i]<<' ';
        cout<<'\n';
        for(int i=0;i<n;++i)
            cout<<tp[i]<<' ';
        cout<<'\n';
        for(int i=0;i<n;++i)
            cout<<A.sumb(0, 0, A.sz, st[i], fn[i])<<' ';
        cout<<'\n';*/
    }
}
/*
9 10
1 2
1 3
3 4
3 9
9 6
9 8
5 6
7 6
5
1 3 11
1 6 7
2 3
1 7 7
2 6
*/
# Verdict Execution time Memory Grader output
1 Correct 143 ms 43592 KB Output is correct
2 Correct 150 ms 43632 KB Output is correct
3 Correct 155 ms 43568 KB Output is correct
4 Correct 146 ms 43508 KB Output is correct
5 Correct 123 ms 38928 KB Output is correct
6 Correct 10 ms 9556 KB Output is correct
7 Correct 11 ms 9340 KB Output is correct
8 Correct 11 ms 9368 KB Output is correct
9 Correct 152 ms 34352 KB Output is correct
10 Correct 144 ms 34300 KB Output is correct
11 Correct 160 ms 34380 KB Output is correct
12 Correct 137 ms 33032 KB Output is correct
13 Correct 151 ms 40996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8916 KB Output is correct
2 Correct 10 ms 8916 KB Output is correct
3 Correct 10 ms 8908 KB Output is correct
4 Correct 10 ms 8916 KB Output is correct
5 Correct 11 ms 9044 KB Output is correct
6 Correct 13 ms 9300 KB Output is correct
7 Correct 13 ms 9340 KB Output is correct
8 Correct 14 ms 9340 KB Output is correct
9 Correct 16 ms 9588 KB Output is correct
10 Correct 15 ms 9596 KB Output is correct
11 Correct 16 ms 9684 KB Output is correct
12 Correct 11 ms 9580 KB Output is correct
13 Correct 15 ms 9556 KB Output is correct
14 Correct 16 ms 9544 KB Output is correct
15 Correct 15 ms 9844 KB Output is correct
16 Correct 16 ms 9556 KB Output is correct
17 Correct 16 ms 9676 KB Output is correct
18 Correct 14 ms 9300 KB Output is correct
19 Correct 15 ms 9556 KB Output is correct
20 Correct 13 ms 9300 KB Output is correct
21 Correct 12 ms 9300 KB Output is correct
22 Correct 10 ms 9076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 186 ms 52988 KB Output is correct
2 Correct 221 ms 53524 KB Output is correct
3 Correct 171 ms 53876 KB Output is correct
4 Correct 291 ms 54716 KB Output is correct
5 Correct 433 ms 51576 KB Output is correct
6 Correct 12 ms 9812 KB Output is correct
7 Correct 11 ms 9528 KB Output is correct
8 Correct 12 ms 9584 KB Output is correct
9 Correct 413 ms 46496 KB Output is correct
10 Correct 380 ms 45960 KB Output is correct
11 Correct 373 ms 45872 KB Output is correct
12 Correct 384 ms 46080 KB Output is correct
13 Correct 440 ms 68056 KB Output is correct
14 Correct 463 ms 68120 KB Output is correct
15 Correct 454 ms 68088 KB Output is correct
16 Correct 458 ms 68024 KB Output is correct
17 Correct 465 ms 68020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 568 ms 48620 KB Output is correct
2 Correct 568 ms 48332 KB Output is correct
3 Correct 558 ms 48456 KB Output is correct
4 Correct 583 ms 48588 KB Output is correct
5 Correct 562 ms 46548 KB Output is correct
6 Correct 565 ms 48376 KB Output is correct
7 Correct 435 ms 30480 KB Output is correct
8 Correct 466 ms 30548 KB Output is correct
9 Correct 593 ms 48552 KB Output is correct
10 Correct 557 ms 48620 KB Output is correct
11 Correct 551 ms 48772 KB Output is correct
12 Correct 448 ms 30836 KB Output is correct
13 Correct 317 ms 25156 KB Output is correct
14 Correct 418 ms 28656 KB Output is correct
15 Correct 393 ms 29256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 143 ms 43592 KB Output is correct
2 Correct 150 ms 43632 KB Output is correct
3 Correct 155 ms 43568 KB Output is correct
4 Correct 146 ms 43508 KB Output is correct
5 Correct 123 ms 38928 KB Output is correct
6 Correct 10 ms 9556 KB Output is correct
7 Correct 11 ms 9340 KB Output is correct
8 Correct 11 ms 9368 KB Output is correct
9 Correct 152 ms 34352 KB Output is correct
10 Correct 144 ms 34300 KB Output is correct
11 Correct 160 ms 34380 KB Output is correct
12 Correct 137 ms 33032 KB Output is correct
13 Correct 151 ms 40996 KB Output is correct
14 Correct 9 ms 8916 KB Output is correct
15 Correct 10 ms 8916 KB Output is correct
16 Correct 10 ms 8908 KB Output is correct
17 Correct 10 ms 8916 KB Output is correct
18 Correct 11 ms 9044 KB Output is correct
19 Correct 13 ms 9300 KB Output is correct
20 Correct 13 ms 9340 KB Output is correct
21 Correct 14 ms 9340 KB Output is correct
22 Correct 16 ms 9588 KB Output is correct
23 Correct 15 ms 9596 KB Output is correct
24 Correct 16 ms 9684 KB Output is correct
25 Correct 11 ms 9580 KB Output is correct
26 Correct 15 ms 9556 KB Output is correct
27 Correct 16 ms 9544 KB Output is correct
28 Correct 15 ms 9844 KB Output is correct
29 Correct 16 ms 9556 KB Output is correct
30 Correct 16 ms 9676 KB Output is correct
31 Correct 14 ms 9300 KB Output is correct
32 Correct 15 ms 9556 KB Output is correct
33 Correct 13 ms 9300 KB Output is correct
34 Correct 12 ms 9300 KB Output is correct
35 Correct 10 ms 9076 KB Output is correct
36 Correct 186 ms 52988 KB Output is correct
37 Correct 221 ms 53524 KB Output is correct
38 Correct 171 ms 53876 KB Output is correct
39 Correct 291 ms 54716 KB Output is correct
40 Correct 433 ms 51576 KB Output is correct
41 Correct 12 ms 9812 KB Output is correct
42 Correct 11 ms 9528 KB Output is correct
43 Correct 12 ms 9584 KB Output is correct
44 Correct 413 ms 46496 KB Output is correct
45 Correct 380 ms 45960 KB Output is correct
46 Correct 373 ms 45872 KB Output is correct
47 Correct 384 ms 46080 KB Output is correct
48 Correct 440 ms 68056 KB Output is correct
49 Correct 463 ms 68120 KB Output is correct
50 Correct 454 ms 68088 KB Output is correct
51 Correct 458 ms 68024 KB Output is correct
52 Correct 465 ms 68020 KB Output is correct
53 Correct 568 ms 48620 KB Output is correct
54 Correct 568 ms 48332 KB Output is correct
55 Correct 558 ms 48456 KB Output is correct
56 Correct 583 ms 48588 KB Output is correct
57 Correct 562 ms 46548 KB Output is correct
58 Correct 565 ms 48376 KB Output is correct
59 Correct 435 ms 30480 KB Output is correct
60 Correct 466 ms 30548 KB Output is correct
61 Correct 593 ms 48552 KB Output is correct
62 Correct 557 ms 48620 KB Output is correct
63 Correct 551 ms 48772 KB Output is correct
64 Correct 448 ms 30836 KB Output is correct
65 Correct 317 ms 25156 KB Output is correct
66 Correct 418 ms 28656 KB Output is correct
67 Correct 393 ms 29256 KB Output is correct
68 Correct 9 ms 8896 KB Output is correct
69 Correct 9 ms 8944 KB Output is correct
70 Correct 678 ms 57928 KB Output is correct
71 Correct 682 ms 57988 KB Output is correct
72 Correct 700 ms 58092 KB Output is correct
73 Correct 688 ms 57912 KB Output is correct
74 Correct 718 ms 57800 KB Output is correct
75 Correct 713 ms 53192 KB Output is correct
76 Correct 546 ms 48632 KB Output is correct
77 Correct 600 ms 48492 KB Output is correct
78 Correct 635 ms 47392 KB Output is correct
79 Correct 660 ms 53244 KB Output is correct
80 Correct 663 ms 51324 KB Output is correct
81 Correct 675 ms 52724 KB Output is correct
82 Correct 428 ms 40908 KB Output is correct
83 Correct 456 ms 47736 KB Output is correct
84 Correct 457 ms 47184 KB Output is correct
85 Correct 455 ms 47464 KB Output is correct