답안 #1036897

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1036897 2024-07-27T19:18:00 Z PM1 Sumtree (INOI20_sumtree) C++17
30 / 100
863 ms 273748 KB
#include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
#define ll long long
const int mxn=5e5+5,M=1e9+7,sz=(1<<21);
int n,r,q;
int st[mxn],fn[mxn],cnt,par[mxn],zr[mxn],bad=0,pos[mxn];
ll f[mxn],rf[mxn],ans=1;
vector<int>v[mxn];
ll ferma(ll x,ll num=M-2){
	ll res=1;
	while(num){
		if(num&1)res=(res*x)%M;
		x=(x*x)%M;
		num>>=1;
	}
	return res;
}
void dfs(int z){
	st[z]=++cnt;
	pos[cnt]=z;
	zr[z]=1;
	for(auto i:v[z]){
		if(par[z]!=i){
			par[i]=z;
			dfs(i);
			zr[z]+=zr[i];
		}
	}
	fn[z]=cnt;
}
ll comb(ll x,ll y){
	//cout<<x<<" "<<y<<'\n';
	ll res=f[y]*rf[x];
	res%=M;
	res*=rf[y-x];
	return res%M;
}
struct segment{
	int val[sz];
	int get(int id,int L,int R,int l,int r){
		if(L>=R || L>=r || l>=r || l>=R)
			return 0;
		if(L>=l && R<=r)
			return val[id];
		int mid=(L+R)>>1,res=0;
		res+=get(id<<1,L,mid,l,r);
		res+=get((id<<1)+1,mid,R,l,r);
		return res;
	}
	void add(int id,int L,int R,int l,int x){
		if(L+1==R){
			val[id]+=x;
			return ;
		}
		int mid=(L+R)>>1;
		if(l<mid)
			add(id<<1,L,mid,l,x);
		else
			add((id<<1)+1,mid,R,l,x);
		val[id]=val[id*2]+val[id*2+1];
		return ;
	}
}seg[2];
struct kirkhar{
	set<int>s[sz];
	int get(int id,int L,int R,int l){
		if(L+1==R)
			return (s[id].size())?*s[id].rbegin():0;
		int mid=(L+R)>>1,res;
		if(l<mid)
			res=get(id<<1,L,mid,l);
		else
			res=get((id<<1)+1,mid,R,l);
		if(res==0)
			return (s[id].size())?*s[id].rbegin():0;
		return res ;
	}
	void add(int id,int L,int R,int l,int r,int x,bool kos){
		if(L>=R || L>=r || l>=r || l>=R)
			return ;
		if(L>=l && R<=r){
			if(kos)
				s[id].insert(x);
			else
				s[id].erase(x);
			return ;
		}
		int mid=(L+R)>>1,res=0;
		add(id<<1,L,mid,l,r,x,kos);
		add((id<<1)+1,mid,R,l,r,x,kos);
		return ;
	}
}gg;
int main(){
	//ios::sync_with_stdio(false);
	//cin.tie(0);
	//cout.tie(0);
	cin>>n>>r;
	for(int i=1;i<n;i++){
		int x,y;
		cin>>x>>y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfs(1);
	f[0]=1;
	rf[0]=1;
	for(int i=1;i<=n+r;i++){
		f[i]=(f[i-1]*i)%M;
		rf[i]=ferma(f[i]);
		//cout<<f[i]<<" "<<rf[i]<<'\n';
	}
	seg[0].add(1,1,n+1,1,r);
	seg[1].add(1,1,n+1,1,n);
	gg.add(1,1,n+1,1,n+1,1,1);
	ans*=comb(n-1,n-1+r);
	ans%=M;
	cout<<ans<<'\n';
	cin>>q;
	while(q--){
		ll ty,x,y;
		cin>>ty>>x;
		if(ty==1){
			int ycnt=zr[x];
			cin>>y;
			ll zir=seg[0].get(1,1,n+1,st[x]+1,fn[x]+1);
			ll zircnt=seg[1].get(1,1,n+1,st[x]+1,fn[x]+1);
			y-=zir;
			ycnt-=zircnt;
			int p=gg.get(1,1,n+1,st[x]);
			p=pos[p];
			ll pre=seg[0].get(1,1,n+1,st[p],fn[p]+1)-seg[0].get(1,1,n+1,st[p]+1,fn[p]+1);
			ll precnt=seg[1].get(1,1,n+1,st[p],fn[p]+1)-seg[1].get(1,1,n+1,st[p]+1,fn[p]+1);
			if(pre<0){
				bad--;
			}
			else{
				ans*=ferma(comb(precnt-1,precnt-1+pre));
				ans%=M;
			}
			pre-=y;
			precnt-=ycnt;
			//cout<<y-zir<<" "<<ycnt-zircnt<<'\n';
			seg[0].add(1,1,n+1,st[x],y);
			seg[1].add(1,1,n+1,st[x],ycnt);
			seg[0].add(1,1,n+1,st[p],-y);
			seg[1].add(1,1,n+1,st[p],-ycnt);

			if(pre<0){
				bad++;
			}
			else{
				ans*=comb(precnt-1,precnt+pre-1);
				ans%=M;
			}
			if(zir<0){
				bad++;
			}
			else{
				ans*=comb(ycnt-1,ycnt+y-1);
				ans%=M;
			}
			gg.add(1,1,n+1,st[x],fn[x]+1,st[x],1);
		}
		else{
			gg.add(1,1,n+1,st[x],fn[x]+1,st[x],0);
			ll zir=seg[0].get(1,1,n+1,st[x],fn[x]+1)-seg[0].get(1,1,n+1,st[x]+1,fn[x]+1);
			ll zircnt=seg[1].get(1,1,n+1,st[x],fn[x]+1)-seg[1].get(1,1,n+1,st[x]+1,fn[x]+1);
			int p=gg.get(1,1,n+1,st[x]);
			p=pos[p];
			ll pre=seg[0].get(1,1,n+1,st[p],fn[p]+1)-seg[0].get(1,1,n+1,st[p]+1,fn[p]+1);
			ll precnt=seg[1].get(1,1,n+1,st[p],fn[p]+1)-seg[1].get(1,1,n+1,st[p]+1,fn[p]+1);
			if(pre<0){
				bad--;
			}
			else{
				ans*=ferma(comb(precnt-1,precnt-1+pre));
				ans%=M;
			}
			if(zir<0){
				bad--;
			}
			else{
				ans*=ferma(comb(zircnt-1,zircnt+zir-1));
				ans%=M;
			}
			pre+=zir;
			precnt+=zircnt;
			seg[0].add(1,1,n+1,st[x],-zir);
			seg[1].add(1,1,n+1,st[x],-zircnt);
			seg[0].add(1,1,n+1,st[p],+zir);
			seg[1].add(1,1,n+1,st[p],+zircnt);			
			if(pre<0){
				bad++;
			}
			else{
				ans*=comb(precnt-1,precnt+pre-1);
				ans%=M;
			}
			
		}
		if(bad)
			cout<<0<<'\n';
		else
			cout<<ans<<'\n';
	}
	return 0;
 
}

Compilation message

Main.cpp: In member function 'void kirkhar::add(int, int, int, int, int, int, bool)':
Main.cpp:90:20: warning: unused variable 'res' [-Wunused-variable]
   90 |   int mid=(L+R)>>1,res=0;
      |                    ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 196 ms 140056 KB Output is correct
2 Correct 203 ms 138068 KB Output is correct
3 Correct 186 ms 137044 KB Output is correct
4 Correct 200 ms 139600 KB Output is correct
5 Correct 210 ms 135764 KB Output is correct
6 Correct 80 ms 117588 KB Output is correct
7 Correct 85 ms 117448 KB Output is correct
8 Correct 67 ms 115288 KB Output is correct
9 Correct 208 ms 132696 KB Output is correct
10 Correct 202 ms 132948 KB Output is correct
11 Correct 199 ms 133144 KB Output is correct
12 Correct 170 ms 128760 KB Output is correct
13 Correct 192 ms 138576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 112464 KB Output is correct
2 Correct 48 ms 112472 KB Output is correct
3 Correct 45 ms 112596 KB Output is correct
4 Correct 45 ms 112468 KB Output is correct
5 Correct 50 ms 112724 KB Output is correct
6 Correct 56 ms 113024 KB Output is correct
7 Correct 54 ms 113000 KB Output is correct
8 Correct 54 ms 113244 KB Output is correct
9 Correct 60 ms 113496 KB Output is correct
10 Correct 66 ms 114476 KB Output is correct
11 Correct 79 ms 116340 KB Output is correct
12 Correct 84 ms 117588 KB Output is correct
13 Correct 84 ms 117332 KB Output is correct
14 Correct 88 ms 117444 KB Output is correct
15 Correct 89 ms 117844 KB Output is correct
16 Correct 84 ms 117548 KB Output is correct
17 Correct 76 ms 115540 KB Output is correct
18 Correct 87 ms 117256 KB Output is correct
19 Correct 82 ms 116660 KB Output is correct
20 Correct 59 ms 113064 KB Output is correct
21 Correct 52 ms 112800 KB Output is correct
22 Correct 51 ms 112828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 238 ms 146100 KB Output is correct
2 Correct 287 ms 150200 KB Output is correct
3 Correct 223 ms 144212 KB Output is correct
4 Correct 415 ms 161620 KB Output is correct
5 Correct 847 ms 194464 KB Output is correct
6 Correct 87 ms 117844 KB Output is correct
7 Correct 86 ms 117448 KB Output is correct
8 Correct 69 ms 115524 KB Output is correct
9 Correct 561 ms 147944 KB Output is correct
10 Correct 469 ms 145748 KB Output is correct
11 Correct 427 ms 144724 KB Output is correct
12 Runtime error 352 ms 273748 KB Execution killed with signal 11
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 808 ms 140628 KB Output is correct
2 Correct 792 ms 141140 KB Output is correct
3 Correct 763 ms 141396 KB Output is correct
4 Correct 863 ms 141372 KB Output is correct
5 Correct 808 ms 140372 KB Output is correct
6 Correct 739 ms 136980 KB Output is correct
7 Correct 612 ms 126256 KB Output is correct
8 Correct 689 ms 130644 KB Output is correct
9 Correct 753 ms 141112 KB Output is correct
10 Correct 689 ms 140940 KB Output is correct
11 Correct 681 ms 140896 KB Output is correct
12 Correct 617 ms 130900 KB Output is correct
13 Incorrect 508 ms 127572 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 196 ms 140056 KB Output is correct
2 Correct 203 ms 138068 KB Output is correct
3 Correct 186 ms 137044 KB Output is correct
4 Correct 200 ms 139600 KB Output is correct
5 Correct 210 ms 135764 KB Output is correct
6 Correct 80 ms 117588 KB Output is correct
7 Correct 85 ms 117448 KB Output is correct
8 Correct 67 ms 115288 KB Output is correct
9 Correct 208 ms 132696 KB Output is correct
10 Correct 202 ms 132948 KB Output is correct
11 Correct 199 ms 133144 KB Output is correct
12 Correct 170 ms 128760 KB Output is correct
13 Correct 192 ms 138576 KB Output is correct
14 Correct 46 ms 112464 KB Output is correct
15 Correct 48 ms 112472 KB Output is correct
16 Correct 45 ms 112596 KB Output is correct
17 Correct 45 ms 112468 KB Output is correct
18 Correct 50 ms 112724 KB Output is correct
19 Correct 56 ms 113024 KB Output is correct
20 Correct 54 ms 113000 KB Output is correct
21 Correct 54 ms 113244 KB Output is correct
22 Correct 60 ms 113496 KB Output is correct
23 Correct 66 ms 114476 KB Output is correct
24 Correct 79 ms 116340 KB Output is correct
25 Correct 84 ms 117588 KB Output is correct
26 Correct 84 ms 117332 KB Output is correct
27 Correct 88 ms 117444 KB Output is correct
28 Correct 89 ms 117844 KB Output is correct
29 Correct 84 ms 117548 KB Output is correct
30 Correct 76 ms 115540 KB Output is correct
31 Correct 87 ms 117256 KB Output is correct
32 Correct 82 ms 116660 KB Output is correct
33 Correct 59 ms 113064 KB Output is correct
34 Correct 52 ms 112800 KB Output is correct
35 Correct 51 ms 112828 KB Output is correct
36 Correct 238 ms 146100 KB Output is correct
37 Correct 287 ms 150200 KB Output is correct
38 Correct 223 ms 144212 KB Output is correct
39 Correct 415 ms 161620 KB Output is correct
40 Correct 847 ms 194464 KB Output is correct
41 Correct 87 ms 117844 KB Output is correct
42 Correct 86 ms 117448 KB Output is correct
43 Correct 69 ms 115524 KB Output is correct
44 Correct 561 ms 147944 KB Output is correct
45 Correct 469 ms 145748 KB Output is correct
46 Correct 427 ms 144724 KB Output is correct
47 Runtime error 352 ms 273748 KB Execution killed with signal 11
48 Halted 0 ms 0 KB -