답안 #468569

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
468569 2021-08-28T19:08:30 Z alirezasamimi100 Sumtree (INOI20_sumtree) C++17
100 / 100
2935 ms 61136 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=18,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;
}
int n,g[N*4],st[N],fn[N],t,p[N][LN],lz[N*4],R,q,z;
ll F[M],I[M],ans;
pii f[N*4];
vector<int> adj[N];
pii operator+(pii a, pii 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(int v, int P){
    p[v][0]=P;
    for(int i=1; i<LN; i++) p[v][i]=p[p[v][i-1]][i-1];
    st[v]=++t;
    for(int u : adj[v]) if(u!=P) dfs(u,v);
    fn[v]=t;
}
void update(int v, int l, int r, int p, int x){
    if(r-l==1){
        g[v]+=x;
        return;
    }
    int 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];
}
int get(int v, int l, int r, int tl, int tr){
    if(l>=tr || tl>=r) return 0;
    if(l>=tl && r<=tr) return g[v];
    int m=(l+r)>>1;
    return get(lc,l,m,tl,tr)+get(rc,m,r,tl,tr);
}
void build(int v, int l, int 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(int v, int l, int r){
    f[v].F+=lz[v];
    if(r-l>1){
        lz[lc]+=lz[v];
        lz[rc]+=lz[v];
    }
    lz[v]=0;
}
void upd(int v, int l, int r, int tl, int tr, int x){
    shift(v,l,r);
    if(l>=tr || tl>=r) return;
    if(l>=tl && r<=tr){
        lz[v]+=x;
        return shift(v,l,r);
    }
    int m=(l+r)>>1;
    upd(lc,l,m,tl,tr,x);
    upd(rc,m,r,tl,tr,x);
    f[v]=f[lc]+f[rc];
}
pii gt(int v, int l, int r, int tl, int tr){
    shift(v,l,r);
    if(l>=tr || tl>=r) return mp(inf,0);
    if(l>=tl && r<=tr) return f[v];
    int m=(l+r)>>1;
    return gt(lc,l,m,tl,tr)+gt(rc,m,r,tl,tr);
}
ll C(int x, int y){
    if(y>x) return 0;
    return F[x]*I[y]%MOD*I[x-y]%MOD;
}
int fp(int v){
    int x=gt(1,1,n+1,st[v],st[v]+1).F;
    for(ll i=LN; i--;){
        int k=p[v][i];
        if(k && gt(1,1,n+1,st[k],st[k]+1).F==x) v=k;
    }
    return v;
}
ll calc(int v){
    int 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(int i=1; i<M; i++){
        F[i]=F[i-1]*i%MOD;
        I[i]=pow(F[i],MOD-2,MOD);
    }
    for(int 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--){
        int 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 253 ms 45108 KB Output is correct
2 Correct 255 ms 45108 KB Output is correct
3 Correct 255 ms 45164 KB Output is correct
4 Correct 242 ms 45144 KB Output is correct
5 Correct 223 ms 41928 KB Output is correct
6 Correct 86 ms 13512 KB Output is correct
7 Correct 84 ms 13232 KB Output is correct
8 Correct 83 ms 13200 KB Output is correct
9 Correct 253 ms 39100 KB Output is correct
10 Correct 250 ms 39200 KB Output is correct
11 Correct 283 ms 39040 KB Output is correct
12 Correct 229 ms 37948 KB Output is correct
13 Correct 224 ms 42972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 12800 KB Output is correct
2 Correct 82 ms 12792 KB Output is correct
3 Correct 82 ms 12816 KB Output is correct
4 Correct 83 ms 12868 KB Output is correct
5 Correct 87 ms 12868 KB Output is correct
6 Correct 90 ms 13076 KB Output is correct
7 Correct 93 ms 13092 KB Output is correct
8 Correct 90 ms 13160 KB Output is correct
9 Correct 95 ms 13276 KB Output is correct
10 Correct 98 ms 13304 KB Output is correct
11 Correct 99 ms 13416 KB Output is correct
12 Correct 88 ms 13308 KB Output is correct
13 Correct 98 ms 13384 KB Output is correct
14 Correct 97 ms 13380 KB Output is correct
15 Correct 101 ms 13448 KB Output is correct
16 Correct 95 ms 13308 KB Output is correct
17 Correct 101 ms 13380 KB Output is correct
18 Correct 94 ms 13064 KB Output is correct
19 Correct 94 ms 13256 KB Output is correct
20 Correct 92 ms 13124 KB Output is correct
21 Correct 91 ms 13132 KB Output is correct
22 Correct 90 ms 12916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 330 ms 48748 KB Output is correct
2 Correct 507 ms 49008 KB Output is correct
3 Correct 336 ms 49220 KB Output is correct
4 Correct 728 ms 49600 KB Output is correct
5 Correct 1374 ms 47344 KB Output is correct
6 Correct 89 ms 13508 KB Output is correct
7 Correct 87 ms 13272 KB Output is correct
8 Correct 89 ms 13336 KB Output is correct
9 Correct 1022 ms 44280 KB Output is correct
10 Correct 836 ms 44012 KB Output is correct
11 Correct 742 ms 43780 KB Output is correct
12 Correct 787 ms 43300 KB Output is correct
13 Correct 1224 ms 57552 KB Output is correct
14 Correct 1205 ms 61020 KB Output is correct
15 Correct 1224 ms 61136 KB Output is correct
16 Correct 1263 ms 61068 KB Output is correct
17 Correct 1200 ms 61116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1461 ms 45280 KB Output is correct
2 Correct 1320 ms 45060 KB Output is correct
3 Correct 1280 ms 45132 KB Output is correct
4 Correct 1474 ms 45160 KB Output is correct
5 Correct 1377 ms 43940 KB Output is correct
6 Correct 1182 ms 45112 KB Output is correct
7 Correct 1114 ms 30036 KB Output is correct
8 Correct 1393 ms 29996 KB Output is correct
9 Correct 1506 ms 45120 KB Output is correct
10 Correct 1229 ms 45136 KB Output is correct
11 Correct 1185 ms 45112 KB Output is correct
12 Correct 1089 ms 30016 KB Output is correct
13 Correct 868 ms 27148 KB Output is correct
14 Correct 1020 ms 28612 KB Output is correct
15 Correct 1226 ms 28484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 253 ms 45108 KB Output is correct
2 Correct 255 ms 45108 KB Output is correct
3 Correct 255 ms 45164 KB Output is correct
4 Correct 242 ms 45144 KB Output is correct
5 Correct 223 ms 41928 KB Output is correct
6 Correct 86 ms 13512 KB Output is correct
7 Correct 84 ms 13232 KB Output is correct
8 Correct 83 ms 13200 KB Output is correct
9 Correct 253 ms 39100 KB Output is correct
10 Correct 250 ms 39200 KB Output is correct
11 Correct 283 ms 39040 KB Output is correct
12 Correct 229 ms 37948 KB Output is correct
13 Correct 224 ms 42972 KB Output is correct
14 Correct 82 ms 12800 KB Output is correct
15 Correct 82 ms 12792 KB Output is correct
16 Correct 82 ms 12816 KB Output is correct
17 Correct 83 ms 12868 KB Output is correct
18 Correct 87 ms 12868 KB Output is correct
19 Correct 90 ms 13076 KB Output is correct
20 Correct 93 ms 13092 KB Output is correct
21 Correct 90 ms 13160 KB Output is correct
22 Correct 95 ms 13276 KB Output is correct
23 Correct 98 ms 13304 KB Output is correct
24 Correct 99 ms 13416 KB Output is correct
25 Correct 88 ms 13308 KB Output is correct
26 Correct 98 ms 13384 KB Output is correct
27 Correct 97 ms 13380 KB Output is correct
28 Correct 101 ms 13448 KB Output is correct
29 Correct 95 ms 13308 KB Output is correct
30 Correct 101 ms 13380 KB Output is correct
31 Correct 94 ms 13064 KB Output is correct
32 Correct 94 ms 13256 KB Output is correct
33 Correct 92 ms 13124 KB Output is correct
34 Correct 91 ms 13132 KB Output is correct
35 Correct 90 ms 12916 KB Output is correct
36 Correct 330 ms 48748 KB Output is correct
37 Correct 507 ms 49008 KB Output is correct
38 Correct 336 ms 49220 KB Output is correct
39 Correct 728 ms 49600 KB Output is correct
40 Correct 1374 ms 47344 KB Output is correct
41 Correct 89 ms 13508 KB Output is correct
42 Correct 87 ms 13272 KB Output is correct
43 Correct 89 ms 13336 KB Output is correct
44 Correct 1022 ms 44280 KB Output is correct
45 Correct 836 ms 44012 KB Output is correct
46 Correct 742 ms 43780 KB Output is correct
47 Correct 787 ms 43300 KB Output is correct
48 Correct 1224 ms 57552 KB Output is correct
49 Correct 1205 ms 61020 KB Output is correct
50 Correct 1224 ms 61136 KB Output is correct
51 Correct 1263 ms 61068 KB Output is correct
52 Correct 1200 ms 61116 KB Output is correct
53 Correct 1461 ms 45280 KB Output is correct
54 Correct 1320 ms 45060 KB Output is correct
55 Correct 1280 ms 45132 KB Output is correct
56 Correct 1474 ms 45160 KB Output is correct
57 Correct 1377 ms 43940 KB Output is correct
58 Correct 1182 ms 45112 KB Output is correct
59 Correct 1114 ms 30036 KB Output is correct
60 Correct 1393 ms 29996 KB Output is correct
61 Correct 1506 ms 45120 KB Output is correct
62 Correct 1229 ms 45136 KB Output is correct
63 Correct 1185 ms 45112 KB Output is correct
64 Correct 1089 ms 30016 KB Output is correct
65 Correct 868 ms 27148 KB Output is correct
66 Correct 1020 ms 28612 KB Output is correct
67 Correct 1226 ms 28484 KB Output is correct
68 Correct 83 ms 12844 KB Output is correct
69 Correct 84 ms 12852 KB Output is correct
70 Correct 2150 ms 54940 KB Output is correct
71 Correct 2454 ms 54848 KB Output is correct
72 Correct 2521 ms 54752 KB Output is correct
73 Correct 2605 ms 54972 KB Output is correct
74 Correct 2935 ms 54852 KB Output is correct
75 Correct 2682 ms 52804 KB Output is correct
76 Correct 1128 ms 49748 KB Output is correct
77 Correct 1257 ms 49552 KB Output is correct
78 Correct 1387 ms 48476 KB Output is correct
79 Correct 2328 ms 52704 KB Output is correct
80 Correct 2416 ms 51684 KB Output is correct
81 Correct 2416 ms 52388 KB Output is correct
82 Correct 882 ms 46420 KB Output is correct
83 Correct 1565 ms 51984 KB Output is correct
84 Correct 1852 ms 51744 KB Output is correct
85 Correct 1935 ms 51948 KB Output is correct