답안 #632571

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
632571 2022-08-20T10:49:44 Z codr0 Sumtree (INOI20_sumtree) C++14
100 / 100
717 ms 109640 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
 
#define F first
#define S second
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define err(x) cerr<<#x<<"="<<x<<'\n'
#define lc 2*id
#define rc 2*id+1
#define dmid int mid=(r+l)/2
#define all(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define pb push_back
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define bit(i,j) ((j>>i)&1)

using namespace std;
const int md=1e9+7;
const int N=5e5+4;
const int NN=2e5+4;
int n;
vector<int> adj[NN];
int fct[N],inv[N];
int ans,t;
int sz[NN],h[NN],st[NN],en[NN],tme;
int A[NN],B[NN];
struct fenwick{
	int fen[NN];
	void add(int k,int x){
		while(k<NN){
			fen[k]+=x;
			k+=(k&(-k));
		}
	}
	int Get(int k){
		int rt=0;
		while(k>0){
			rt+=fen[k];
			k-=(k&(-k));
		}
		return rt;
	}
	int get(int l,int r){
		return (Get(r)-Get(l-1));
	}
}; fenwick f1,f2;
struct PQ{
	priority_queue<pii> pq1;
	priority_queue<pii> pq2;
	void add(pii x){
		pq1.push(x);
	}
	void erase(pii x){
		pq2.push(x);
	}
	int get(){
		while(!pq2.empty()&&pq1.top()==pq2.top()) pq1.pop(),pq2.pop();
		if(pq1.empty()) return 0;
		return (pq1.top().S);
	}
};
PQ seg[4*NN];
 
	void upd(int id,int l,int r,int v,int x){
		if(st[v]>=r||(en[v]+1)<=l) return;
		if(st[v]<=l&&(en[v]+1)>=r){
			if(x==+1) seg[id].add({h[v],v});
			else seg[id].erase({h[v],v});
			return;
		}
		dmid;
		upd(lc,l,mid,v,x);
		upd(rc,mid,r,v,x);
	}
 
	int get(int id,int l,int r,int v){
		if(st[v]<l||st[v]>=r) return 0;
		int rt=seg[id].get();
		if(l+1==r) return rt;
		dmid;
		int x1=get(lc,l,mid,v);
		int x2=get(rc,mid,r,v);
		if(h[x1]>h[rt]) rt=x1;
		if(h[x2]>h[rt]) rt=x2;
		return rt;
	}
 
	int pw(int a,int b){
		int rt=1;
		while(b){
			if(b&1) rt=1LL*rt*a%md;
			a=1LL*a*a%md;
			b/=2;
		}
		return rt;
	}
 
	int C(int r,int _n){
		if(r<0||r>_n) return 1;
		return (1LL*(1LL*fct[_n]*inv[r]%md)*inv[_n-r]%md);
	}
 
	void dfs(int v,int p){
		h[v]=h[p]+1;
		st[v]=++tme;
		sz[v]=1;
		for(int u:adj[v]) if(u!=p){ dfs(u,v); sz[v]+=sz[u];}
		en[v]=tme;
	}
 
	void add(int v,int w){
		int par=get(1,1,n+1,v);
		upd(1,1,n+1,v,+1);
		f1.add(st[par],-A[par]);
		f2.add(st[par],-B[par]);
		ans=1LL*ans*pw(C(B[par],A[par]+B[par]-1),md-2)%md;
		if(B[par]<0) t--;
		A[v]=sz[v]-f1.get(st[v],en[v]);
		B[v]=w-f2.get(st[v],en[v]);
		A[par]-=A[v];
		B[par]-=B[v];
		f1.add(st[par],A[par]);
		f1.add(st[v],A[v]);
		f2.add(st[par],B[par]);
		f2.add(st[v],B[v]);
		if(B[v]<0) t++;
		if(B[par]<0) t++;
		ans=1LL*ans*C(B[par],A[par]+B[par]-1)%md;
		ans=1LL*ans*C(B[v],A[v]+B[v]-1)%md;
	}

	void rm(int v){
		upd(1,1,n+1,v,-1);
		int par=get(1,1,n+1,v);
		ans=1LL*ans*pw(C(B[par],A[par]+B[par]-1),md-2)%md;
		ans=1LL*ans*pw(C(B[v],A[v]+B[v]-1),md-2)%md;
		f1.add(st[par],-A[par]);
		f2.add(st[par],-B[par]);
		f1.add(st[v],-A[v]);
		f2.add(st[v],-B[v]);
		if(B[v]<0) t--;
		if(B[par]<0) t--;
		A[par]+=A[v];
		B[par]+=B[v];
		f1.add(st[par],A[par]);
		f2.add(st[par],B[par]);
		if(B[par]<0) t++;
		ans=1LL*ans*C(B[par],A[par]+B[par]-1)%md;
	}
 
	void OUT(){
		if(t>0){ cout<<"0\n"; return; }
		cout<<ans<<'\n';
	}
 
	int32_t main(){
	iostream::sync_with_stdio(0); cin.tie(0);
	
	fct[0]=1; FOR(i,1,N-1) fct[i]=1LL*fct[i-1]*i%md;
	inv[N-1]=pw(fct[N-1],md-2); FORR(i,N-2,0) inv[i]=1LL*inv[i+1]*(i+1)%md;
	int k; cin>>n>>k;
	FOR(i,1,n-1){
		int u,v; cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs(1,1);
	ans=C(k,k+n-1);
	t=0;
	A[1]=n;
	B[1]=k;
	f1.add(st[1],A[1]);
	f2.add(st[1],B[1]);
	upd(1,1,n+1,1,+1);
	OUT();
	int q; cin>>q;
	while(q--){
		int id,v,w;
		cin>>id>>v;
		if(id==1){
			cin>>w;
			add(v,w);
		}else rm(v);
		OUT();
	}
 
	return 0;
	}
# 결과 실행 시간 메모리 Grader output
1 Correct 134 ms 76364 KB Output is correct
2 Correct 149 ms 76252 KB Output is correct
3 Correct 140 ms 76304 KB Output is correct
4 Correct 134 ms 76284 KB Output is correct
5 Correct 116 ms 72408 KB Output is correct
6 Correct 33 ms 59468 KB Output is correct
7 Correct 38 ms 59212 KB Output is correct
8 Correct 39 ms 59228 KB Output is correct
9 Correct 154 ms 68684 KB Output is correct
10 Correct 137 ms 68680 KB Output is correct
11 Correct 133 ms 68668 KB Output is correct
12 Correct 124 ms 68164 KB Output is correct
13 Correct 161 ms 75320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 59088 KB Output is correct
2 Correct 33 ms 59084 KB Output is correct
3 Correct 32 ms 59092 KB Output is correct
4 Correct 33 ms 59060 KB Output is correct
5 Correct 36 ms 59100 KB Output is correct
6 Correct 37 ms 59348 KB Output is correct
7 Correct 36 ms 59300 KB Output is correct
8 Correct 35 ms 59308 KB Output is correct
9 Correct 37 ms 59424 KB Output is correct
10 Correct 38 ms 59596 KB Output is correct
11 Correct 38 ms 59660 KB Output is correct
12 Correct 34 ms 59396 KB Output is correct
13 Correct 45 ms 59504 KB Output is correct
14 Correct 40 ms 59700 KB Output is correct
15 Correct 38 ms 59768 KB Output is correct
16 Correct 37 ms 59520 KB Output is correct
17 Correct 38 ms 59588 KB Output is correct
18 Correct 36 ms 59348 KB Output is correct
19 Correct 38 ms 59340 KB Output is correct
20 Correct 38 ms 59308 KB Output is correct
21 Correct 40 ms 59288 KB Output is correct
22 Correct 33 ms 59124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 158 ms 79744 KB Output is correct
2 Correct 191 ms 81696 KB Output is correct
3 Correct 168 ms 80512 KB Output is correct
4 Correct 255 ms 85068 KB Output is correct
5 Correct 376 ms 89388 KB Output is correct
6 Correct 36 ms 59596 KB Output is correct
7 Correct 40 ms 59240 KB Output is correct
8 Correct 38 ms 59608 KB Output is correct
9 Correct 336 ms 77472 KB Output is correct
10 Correct 281 ms 76360 KB Output is correct
11 Correct 268 ms 75820 KB Output is correct
12 Correct 290 ms 76164 KB Output is correct
13 Correct 344 ms 109640 KB Output is correct
14 Correct 336 ms 109612 KB Output is correct
15 Correct 344 ms 109628 KB Output is correct
16 Correct 333 ms 109612 KB Output is correct
17 Correct 346 ms 109520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 447 ms 81228 KB Output is correct
2 Correct 469 ms 81140 KB Output is correct
3 Correct 475 ms 81356 KB Output is correct
4 Correct 461 ms 81364 KB Output is correct
5 Correct 485 ms 80576 KB Output is correct
6 Correct 467 ms 81192 KB Output is correct
7 Correct 394 ms 73100 KB Output is correct
8 Correct 363 ms 73300 KB Output is correct
9 Correct 515 ms 81408 KB Output is correct
10 Correct 463 ms 81304 KB Output is correct
11 Correct 451 ms 81236 KB Output is correct
12 Correct 376 ms 73044 KB Output is correct
13 Correct 262 ms 67892 KB Output is correct
14 Correct 350 ms 71508 KB Output is correct
15 Correct 344 ms 71312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 134 ms 76364 KB Output is correct
2 Correct 149 ms 76252 KB Output is correct
3 Correct 140 ms 76304 KB Output is correct
4 Correct 134 ms 76284 KB Output is correct
5 Correct 116 ms 72408 KB Output is correct
6 Correct 33 ms 59468 KB Output is correct
7 Correct 38 ms 59212 KB Output is correct
8 Correct 39 ms 59228 KB Output is correct
9 Correct 154 ms 68684 KB Output is correct
10 Correct 137 ms 68680 KB Output is correct
11 Correct 133 ms 68668 KB Output is correct
12 Correct 124 ms 68164 KB Output is correct
13 Correct 161 ms 75320 KB Output is correct
14 Correct 32 ms 59088 KB Output is correct
15 Correct 33 ms 59084 KB Output is correct
16 Correct 32 ms 59092 KB Output is correct
17 Correct 33 ms 59060 KB Output is correct
18 Correct 36 ms 59100 KB Output is correct
19 Correct 37 ms 59348 KB Output is correct
20 Correct 36 ms 59300 KB Output is correct
21 Correct 35 ms 59308 KB Output is correct
22 Correct 37 ms 59424 KB Output is correct
23 Correct 38 ms 59596 KB Output is correct
24 Correct 38 ms 59660 KB Output is correct
25 Correct 34 ms 59396 KB Output is correct
26 Correct 45 ms 59504 KB Output is correct
27 Correct 40 ms 59700 KB Output is correct
28 Correct 38 ms 59768 KB Output is correct
29 Correct 37 ms 59520 KB Output is correct
30 Correct 38 ms 59588 KB Output is correct
31 Correct 36 ms 59348 KB Output is correct
32 Correct 38 ms 59340 KB Output is correct
33 Correct 38 ms 59308 KB Output is correct
34 Correct 40 ms 59288 KB Output is correct
35 Correct 33 ms 59124 KB Output is correct
36 Correct 158 ms 79744 KB Output is correct
37 Correct 191 ms 81696 KB Output is correct
38 Correct 168 ms 80512 KB Output is correct
39 Correct 255 ms 85068 KB Output is correct
40 Correct 376 ms 89388 KB Output is correct
41 Correct 36 ms 59596 KB Output is correct
42 Correct 40 ms 59240 KB Output is correct
43 Correct 38 ms 59608 KB Output is correct
44 Correct 336 ms 77472 KB Output is correct
45 Correct 281 ms 76360 KB Output is correct
46 Correct 268 ms 75820 KB Output is correct
47 Correct 290 ms 76164 KB Output is correct
48 Correct 344 ms 109640 KB Output is correct
49 Correct 336 ms 109612 KB Output is correct
50 Correct 344 ms 109628 KB Output is correct
51 Correct 333 ms 109612 KB Output is correct
52 Correct 346 ms 109520 KB Output is correct
53 Correct 447 ms 81228 KB Output is correct
54 Correct 469 ms 81140 KB Output is correct
55 Correct 475 ms 81356 KB Output is correct
56 Correct 461 ms 81364 KB Output is correct
57 Correct 485 ms 80576 KB Output is correct
58 Correct 467 ms 81192 KB Output is correct
59 Correct 394 ms 73100 KB Output is correct
60 Correct 363 ms 73300 KB Output is correct
61 Correct 515 ms 81408 KB Output is correct
62 Correct 463 ms 81304 KB Output is correct
63 Correct 451 ms 81236 KB Output is correct
64 Correct 376 ms 73044 KB Output is correct
65 Correct 262 ms 67892 KB Output is correct
66 Correct 350 ms 71508 KB Output is correct
67 Correct 344 ms 71312 KB Output is correct
68 Correct 36 ms 59068 KB Output is correct
69 Correct 35 ms 59116 KB Output is correct
70 Correct 619 ms 96044 KB Output is correct
71 Correct 588 ms 96064 KB Output is correct
72 Correct 622 ms 95996 KB Output is correct
73 Correct 643 ms 96024 KB Output is correct
74 Correct 710 ms 100768 KB Output is correct
75 Correct 717 ms 94216 KB Output is correct
76 Correct 491 ms 85784 KB Output is correct
77 Correct 499 ms 85924 KB Output is correct
78 Correct 567 ms 85316 KB Output is correct
79 Correct 617 ms 91492 KB Output is correct
80 Correct 661 ms 93532 KB Output is correct
81 Correct 674 ms 94004 KB Output is correct
82 Correct 358 ms 76892 KB Output is correct
83 Correct 377 ms 94880 KB Output is correct
84 Correct 440 ms 104928 KB Output is correct
85 Correct 408 ms 95400 KB Output is correct