Submission #468566

# Submission time Handle Problem Language Result Execution time Memory
468566 2021-08-28T18:56:37 Z alirezasamimi100 Sumtree (INOI20_sumtree) C++17
50 / 100
1635 ms 80476 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
/*#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")*/
/*#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")*/
using namespace std;
using ll=long long int;
using ld=long double;
using pll=pair<ll,ll>;
using pii=pair<int,int>;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define lc v<<1
#define rc v<<1|1
#define fast_io ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int N=2e5+10,LN=17,M=5e5+10,SQ=350,B=737,inf=1e9+10;
const ll INF=1e18;
const ld ep=1e-7;
const int MH=1000696969,MD=998244353,MOD=1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
ll pow(ll x, ll y, ll mod){
    ll ans=1;
    while (y != 0) {
        if (y & 1) ans = ans * x % mod;
        y >>= 1;
        x = x * x % mod;
    }
    return ans;
}
ll n,g[N*4],st[N],fn[N],t,p[N][LN],lz[N*4],F[M],I[M],R,q,ans,z;
pll f[N*4];
vector<ll> adj[N];
pll operator+(pll a, pll b){
    if(a.F<b.F) return a;
    if(b.F<a.F) return b;
    return mp(a.F,a.S+b.S);
}
void dfs(ll v, ll P){
    p[v][0]=P;
    for(ll i=1; i<LN; i++) p[v][i]=p[p[v][i-1]][i-1];
    st[v]=++t;
    for(ll u : adj[v]) if(u!=P) dfs(u,v);
    fn[v]=t;
}
void update(ll v, ll l, ll r, ll p, ll x){
    if(r-l==1){
        g[v]+=x;
        return;
    }
    ll m=(l+r)>>1;
    if(p<m) update(lc,l,m,p,x);
    else update(rc,m,r,p,x);
    g[v]=g[lc]+g[rc];
}
ll get(ll v, ll l, ll r, ll tl, ll tr){
    if(l>=tr || tl>=r) return 0;
    if(l>=tl && r<=tr) return g[v];
    ll m=(l+r)>>1;
    return get(lc,l,m,tl,tr)+get(rc,m,r,tl,tr);
}
void build(ll v, ll l, ll r){
    f[v].S=r-l;
    if(r-l>1){
        ll m=(l+r)>>1;
        build(lc,l,m);
        build(rc,m,r);
    }
}
void shift(ll v, ll l, ll r){
    f[v].F+=lz[v];
    if(r-l>1){
        lz[lc]+=lz[v];
        lz[rc]+=lz[v];
    }
    lz[v]=0;
}
void upd(ll v, ll l, ll r, ll tl, ll tr, ll x){
    shift(v,l,r);
    if(l>=tr || tl>=r) return;
    if(l>=tl && r<=tr){
        lz[v]+=x;
        return shift(v,l,r);
    }
    ll m=(l+r)>>1;
    upd(lc,l,m,tl,tr,x);
    upd(rc,m,r,tl,tr,x);
    f[v]=f[lc]+f[rc];
}
pll gt(ll v, ll l, ll r, ll tl, ll tr){
    shift(v,l,r);
    if(l>=tr || tl>=r) return mp(INF,0ll);
    if(l>=tl && r<=tr) return f[v];
    ll m=(l+r)>>1;
    return gt(lc,l,m,tl,tr)+gt(rc,m,r,tl,tr);
}
ll C(ll x, ll y){
    if(y>x) return 0;
    return F[x]*I[y]%MOD*I[x-y]%MOD;
}
ll fp(ll v){
    ll x=gt(1,1,n+1,st[v],st[v]+1).F;
    for(ll i=LN; i--;){
        ll k=p[v][i];
        if(k && gt(1,1,n+1,st[k],st[k]+1).F==x) v=k;
    }
    return v;
}
ll calc(ll v){
    ll x=get(1,1,n+1,st[v],st[v]+1),y=gt(1,1,n+1,st[v],fn[v]+1).S;
    return C(x+y-1,y-1);
}
int main(){
    fast_io;
    cin >> n >> R;
    F[0]=I[0]=1;
    for(ll i=1; i<M; i++){
        F[i]=F[i-1]*i%MOD;
        I[i]=pow(F[i],MOD-2,MOD);
    }
    for(ll i=1; i<n; i++){
        ll v,u;
        cin >> v >> u;
        adj[v].pb(u);
        adj[u].pb(v);
    }
    dfs(1,0);
    build(1,1,n+1);
    upd(1,1,n+1,1,n+1,1);
    update(1,1,n+1,1,R);
    ans=calc(1);
    cout << ans << '\n';
    cin >> q;
    while(q--){
        ll k,v,x,u,y;
        cin >> k >> v;
        if(k==1){
            cin >> x;
            u=fp(v);
            y=calc(u);
            if(y) ans=ans*pow(y,MOD-2,MOD)%MOD;
            else z--;
            x-=get(1,1,n+1,st[v],fn[v]+1);
            update(1,1,n+1,st[v],x);
            update(1,1,n+1,st[u],-x);
            upd(1,1,n+1,st[v],fn[v]+1,1);
            y=calc(u);
            if(y) ans=ans*y%MOD;
            else z++;
            y=calc(v);
            if(y) ans=ans*y%MOD;
            else z++;
        }else{
            u=fp(p[v][0]);
            y=calc(u);
            if(y) ans=ans*pow(y,MOD-2,MOD)%MOD;
            else z--;
            y=calc(v);
            if(y) ans=ans*pow(y,MOD-2,MOD)%MOD;
            else z--;
            upd(1,1,n+1,st[v],fn[v]+1,-1);
            x=get(1,1,n+1,st[v],st[v]+1);
            update(1,1,n+1,st[v],-x);
            update(1,1,n+1,st[u],x);
            y=calc(u);
            if(y) ans=ans*y%MOD;
            else z++;
        }
        if(z) cout << 0 << '\n';
        else cout << ans << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 274 ms 62880 KB Output is correct
2 Correct 266 ms 62768 KB Output is correct
3 Correct 256 ms 62784 KB Output is correct
4 Correct 281 ms 62788 KB Output is correct
5 Correct 242 ms 59348 KB Output is correct
6 Correct 89 ms 13560 KB Output is correct
7 Correct 86 ms 13460 KB Output is correct
8 Correct 85 ms 13512 KB Output is correct
9 Correct 261 ms 58228 KB Output is correct
10 Correct 274 ms 58096 KB Output is correct
11 Correct 273 ms 58272 KB Output is correct
12 Correct 256 ms 56388 KB Output is correct
13 Correct 243 ms 59036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 12868 KB Output is correct
2 Correct 83 ms 12840 KB Output is correct
3 Correct 84 ms 12848 KB Output is correct
4 Correct 83 ms 12868 KB Output is correct
5 Correct 88 ms 12924 KB Output is correct
6 Correct 90 ms 13380 KB Output is correct
7 Correct 91 ms 13308 KB Output is correct
8 Correct 93 ms 13332 KB Output is correct
9 Correct 95 ms 13728 KB Output is correct
10 Correct 98 ms 13764 KB Output is correct
11 Correct 99 ms 13812 KB Output is correct
12 Correct 85 ms 13732 KB Output is correct
13 Correct 98 ms 13688 KB Output is correct
14 Correct 102 ms 13676 KB Output is correct
15 Correct 98 ms 13808 KB Output is correct
16 Correct 95 ms 13804 KB Output is correct
17 Correct 100 ms 13788 KB Output is correct
18 Correct 101 ms 13140 KB Output is correct
19 Correct 95 ms 13684 KB Output is correct
20 Correct 91 ms 13380 KB Output is correct
21 Correct 91 ms 13552 KB Output is correct
22 Correct 88 ms 12920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 370 ms 70672 KB Output is correct
2 Correct 576 ms 73540 KB Output is correct
3 Correct 379 ms 73540 KB Output is correct
4 Correct 862 ms 74360 KB Output is correct
5 Correct 1607 ms 72652 KB Output is correct
6 Correct 90 ms 13768 KB Output is correct
7 Correct 87 ms 13748 KB Output is correct
8 Correct 90 ms 13724 KB Output is correct
9 Correct 1119 ms 70916 KB Output is correct
10 Correct 927 ms 70460 KB Output is correct
11 Correct 832 ms 70308 KB Output is correct
12 Correct 832 ms 70540 KB Output is correct
13 Incorrect 1571 ms 80476 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1555 ms 68932 KB Output is correct
2 Correct 1407 ms 72820 KB Output is correct
3 Correct 1315 ms 72860 KB Output is correct
4 Correct 1493 ms 72840 KB Output is correct
5 Correct 1396 ms 70764 KB Output is correct
6 Correct 1297 ms 72896 KB Output is correct
7 Correct 1228 ms 44516 KB Output is correct
8 Correct 1489 ms 44692 KB Output is correct
9 Correct 1635 ms 72892 KB Output is correct
10 Correct 1267 ms 72960 KB Output is correct
11 Correct 1215 ms 73152 KB Output is correct
12 Correct 1071 ms 44708 KB Output is correct
13 Correct 828 ms 40280 KB Output is correct
14 Correct 1103 ms 42812 KB Output is correct
15 Correct 1269 ms 43156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 274 ms 62880 KB Output is correct
2 Correct 266 ms 62768 KB Output is correct
3 Correct 256 ms 62784 KB Output is correct
4 Correct 281 ms 62788 KB Output is correct
5 Correct 242 ms 59348 KB Output is correct
6 Correct 89 ms 13560 KB Output is correct
7 Correct 86 ms 13460 KB Output is correct
8 Correct 85 ms 13512 KB Output is correct
9 Correct 261 ms 58228 KB Output is correct
10 Correct 274 ms 58096 KB Output is correct
11 Correct 273 ms 58272 KB Output is correct
12 Correct 256 ms 56388 KB Output is correct
13 Correct 243 ms 59036 KB Output is correct
14 Correct 83 ms 12868 KB Output is correct
15 Correct 83 ms 12840 KB Output is correct
16 Correct 84 ms 12848 KB Output is correct
17 Correct 83 ms 12868 KB Output is correct
18 Correct 88 ms 12924 KB Output is correct
19 Correct 90 ms 13380 KB Output is correct
20 Correct 91 ms 13308 KB Output is correct
21 Correct 93 ms 13332 KB Output is correct
22 Correct 95 ms 13728 KB Output is correct
23 Correct 98 ms 13764 KB Output is correct
24 Correct 99 ms 13812 KB Output is correct
25 Correct 85 ms 13732 KB Output is correct
26 Correct 98 ms 13688 KB Output is correct
27 Correct 102 ms 13676 KB Output is correct
28 Correct 98 ms 13808 KB Output is correct
29 Correct 95 ms 13804 KB Output is correct
30 Correct 100 ms 13788 KB Output is correct
31 Correct 101 ms 13140 KB Output is correct
32 Correct 95 ms 13684 KB Output is correct
33 Correct 91 ms 13380 KB Output is correct
34 Correct 91 ms 13552 KB Output is correct
35 Correct 88 ms 12920 KB Output is correct
36 Correct 370 ms 70672 KB Output is correct
37 Correct 576 ms 73540 KB Output is correct
38 Correct 379 ms 73540 KB Output is correct
39 Correct 862 ms 74360 KB Output is correct
40 Correct 1607 ms 72652 KB Output is correct
41 Correct 90 ms 13768 KB Output is correct
42 Correct 87 ms 13748 KB Output is correct
43 Correct 90 ms 13724 KB Output is correct
44 Correct 1119 ms 70916 KB Output is correct
45 Correct 927 ms 70460 KB Output is correct
46 Correct 832 ms 70308 KB Output is correct
47 Correct 832 ms 70540 KB Output is correct
48 Incorrect 1571 ms 80476 KB Output isn't correct
49 Halted 0 ms 0 KB -