Submission #784099

# Submission time Handle Problem Language Result Execution time Memory
784099 2023-07-15T16:39:44 Z amirhoseinfar1385 Sumtree (INOI20_sumtree) C++17
100 / 100
1415 ms 278676 KB
#include<bits/stdc++.h>
using namespace std;
int n,k;
const int maxn=200000+10,maxm=500000+10;
vector<int>adj[maxn],child[maxn];
int par[maxn],val[maxn],link[maxn],fres[maxn],wa[maxn],cnt[maxn],asd,timea=0,high[maxn],fact[maxm],revfact[maxm],mod=1e9+7;
pair<int,int>stf[maxn];
int kaf=(1<<19);
 
struct segment{
	set<pair<int,int>>seg[(1<<20)];
	pair<int,int> get(int i){
		if(i==0){
			return make_pair(0,0);
		}
		if((int)seg[i].size()==0){
			return get((i>>1));
		}
		auto x=*seg[i].rbegin();
		return max(get((i>>1)),x);
	}
	void del(int i,int l,int r,int tl,int tr,int w){
		if(l>r||l>tr||r<tl||tl>tr){
			return ;
		}
		if(l>=tl&&r<=tr){
			seg[i].erase(make_pair(high[w],w));
			return ;
		}
		int m=(l+r)>>1;
		del((i<<1),l,m,tl,tr,w);
		del((i<<1)^1,m+1,r,tl,tr,w);
	}
 
	void upd(int i,int l,int r,int tl,int tr,int w){
		if(l>r||l>tr||r<tl||tl>tr){
			return ;
		}
		if(l>=tl&&r<=tr){
			seg[i].insert(make_pair(high[w],w));
			return ;
		}
		int m=(l+r)>>1;
		upd((i<<1),l,m,tl,tr,w);
		upd((i<<1)^1,m+1,r,tl,tr,w);
	}
}segbal;
 
struct segjam{
	int seg[(1<<20)];
	int pors(int i,int l,int r,int tl,int tr){
		if(l>r||l>tr||r<tl||tl>tr){
			return 0;
		}
		if(l>=tl&&r<=tr){
			return seg[i];
		}
		int m=(l+r)>>1;
		int ret=pors((i<<1),l,m,tl,tr)+pors((i<<1)^1,m+1,r,tl,tr);
		return ret;
	}
	void upd(int i,int w){
		if(i==0){
			return ;
		}
		seg[i]+=w;
		upd((i>>1),w);
	}
}segjam,segted;
 
struct segres{
	int seg[(1<<20)];
	segres(){
		for(int i=kaf;i<(1<<20);i++){
			seg[i]=-1;
		}
		for(int i=1;i<kaf;i++){
			seg[i]=-1;
		}
	}
	void calc(int i){
		if(seg[(i<<1)]==-1&&seg[(i<<1)^1]==-1){
			seg[i]=-1;
			return ;
		}
		if(seg[(i<<1)]==-1){
			seg[i]=seg[(i<<1)^1];
			return ;
		}
		if(seg[(i<<1)^1]==-1){
			seg[i]=seg[(i<<1)];
			return ;
		}
		seg[i]=1ll*seg[(i<<1)^1]*seg[(i<<1)]%mod;
		return ;
	}
 
	void upd(int i,int l,int r,int tl,int tr,int w){
		if(l>r||l>tr||r<tl||tl>tr){
			return ;
		}
		if(l>=tl&&r<=tr){
			seg[i]=w;
			return ;
		}
		int m=(l+r)>>1;
		upd((i<<1),l,m,tl,tr,w);
		upd((i<<1)^1,m+1,r,tl,tr,w);
		calc(i);
	}
 
}res;
 
long long mypow(long long m,long long y){
	if(y==0){
		return 1;
	}
	long long p=mypow(m,(y>>1));
	p*=p;
	p%=mod;
	if(y&1){
		p*=m;
		p%=mod;
	}
	return p;
}
 
void aval(){	
	for(int i=0;i<maxn;i++){
		fres[i]=-1;
		val[i]=-1;
	}
	fact[0]=1;
	for(int i=1;i<maxm;i++){
		fact[i]=1ll*fact[i-1]*i%mod;
	}
	revfact[maxm-1]=mypow(fact[maxm-1],mod-2);
	for(int i=maxm-2;i>=0;i--){
		revfact[i]=1ll*revfact[i+1]*(i+1)%mod;
	}
}
 
int c(int i,int j){
	if(i<0||j<0||i<j){
		return 0;
	}
	int ret=1ll*fact[i]*revfact[j]%mod*revfact[i-j]%mod;
	return ret;
}
 
void dfs(int u,int para=0){
	cnt[u]=1;
	high[u]=high[para]+1;
	timea++;
	stf[u].first=timea;
	par[u]=para;
	for(auto x:adj[u]){
		if(x!=para){
			child[u].push_back(x);
			dfs(x,u);
			cnt[u]+=cnt[x];
		}
	}
	stf[u].second=timea;
}
 
int hes(int a,int b){
	a++;
	int ret=c(b+a-1,a-1);	
	return ret;
}
 
int findp(int u,int av){
	if(u==av||val[u]==-1){
		return findp(par[u],av);
	}
	return u;
}
 
int porsjam(int u,int av){
	int ret=0;
	if(u!=av&&val[u]!=-1){
		ret+=val[u];
		return ret;
	}
	for(auto x:child[u]){
		ret+=porsjam(x,av);
	}
	return ret;
}
 
int porsted(int u,int av){
	int ret=0;
	if(u!=av&&val[u]!=-1){
		return ret;
	}
	for(auto x:child[u]){
		ret+=porsted(x,av);
	}
	ret++;
	return ret;
}
 
void upd(int u,int w){
	wa[u]=w;
	val[u]=w;
	int pp=segbal.get(stf[u].first+kaf).second;
	int jam=segjam.pors(1,0,kaf-1,stf[u].first+1,stf[u].second);
	int ted=cnt[u]-segted.pors(1,0,kaf-1,stf[u].first+1,stf[u].second);
	segted.upd(kaf+stf[u].first,ted);
	segted.upd(kaf+stf[pp].first,-ted);
	segbal.upd(1,0,kaf-1,stf[u].first+1,stf[u].second,u);
	segjam.upd(kaf+stf[u].first,-jam+w);
	segjam.upd(kaf+stf[pp].first,+jam-w);
	int jamp=segjam.pors(1,0,kaf-1,stf[pp].first+1,stf[pp].second);
	int tedp=cnt[pp]-segted.pors(1,0,kaf-1,stf[pp].first+1,stf[pp].second);
//	int tedp=porsted(pp,pp);
	int fake=hes(tedp-1,val[pp]-jamp);
	res.upd(1,0,kaf-1,stf[pp].first,stf[pp].first,fake);
	fake=hes(ted-1,val[u]-jam);
	res.upd(1,0,kaf-1,stf[u].first,stf[u].first,fake);
}
 
void del(int u){
	int pp=segbal.get(stf[u].first+kaf).second;
	int jam=segjam.pors(1,0,kaf-1,stf[u].first+1,stf[u].second);
	int ted=cnt[u]-segted.pors(1,0,kaf-1,stf[u].first+1,stf[u].second);
	segted.upd(kaf+stf[u].first,-ted);
	segted.upd(kaf+stf[pp].first,+ted);
	segbal.del(1,0,kaf-1,stf[u].first+1,stf[u].second,u);
	segjam.upd(kaf+stf[u].first,+jam-val[u]);
	segjam.upd(kaf+stf[pp].first,-jam+val[u]);
	val[u]=-1;
	int jamp=segjam.pors(1,0,kaf-1,stf[pp].first+1,stf[pp].second);
	int tedp=cnt[pp]-segted.pors(1,0,kaf-1,stf[pp].first+1,stf[pp].second);
	int fake=hes(tedp-1,val[pp]-jamp);
	fres[stf[pp].first]=fake;
	fres[stf[u].first]=-1;
	res.upd(1,0,kaf-1,stf[pp].first,stf[pp].first,fake);
	res.upd(1,0,kaf-1,stf[u].first,stf[u].first,-1);
}
 
void calres(){
	int ret=1;
	for(int i=stf[1].first;i<=stf[1].second;i++){
		if(fres[i]==-1){
			continue;
		}
		ret*=fres[i];
		ret%=mod;
	}
	cout<<ret<<"\n";
}
 
void tof(){
	fres[stf[1].first]=hes(porsted(1,1)-1,val[1]-porsjam(1,1));
	res.upd(1,0,kaf-1,stf[1].first,stf[1].first,hes(n-1,val[1]));
	segbal.upd(1,0,kaf-1,stf[1].first+1,stf[1].second,1);
	link[1]=0;
}
 
 
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	aval();
	cin>>n>>k;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs(1);
	val[1]=k;
	tof();
	int q;
	cin>>q;
	asd=1;
	cout<<res.seg[1]<<"\n";
	for(;asd<=q;asd++){
		int qq;
		cin>>qq;
		if(qq==1){
			int u,w;
			cin>>u>>w;
			upd(u,w);
		}
		else{
			int u;
			cin>>u;
			del(u);
		}
		cout<<res.seg[1]<<"\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 157 ms 93784 KB Output is correct
2 Correct 135 ms 93840 KB Output is correct
3 Correct 136 ms 93864 KB Output is correct
4 Correct 143 ms 93840 KB Output is correct
5 Correct 132 ms 91624 KB Output is correct
6 Correct 40 ms 69028 KB Output is correct
7 Correct 40 ms 68700 KB Output is correct
8 Correct 40 ms 68844 KB Output is correct
9 Correct 157 ms 83932 KB Output is correct
10 Correct 140 ms 83948 KB Output is correct
11 Correct 171 ms 83924 KB Output is correct
12 Correct 131 ms 83236 KB Output is correct
13 Correct 121 ms 92576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 68644 KB Output is correct
2 Correct 39 ms 68568 KB Output is correct
3 Correct 38 ms 68572 KB Output is correct
4 Correct 39 ms 68564 KB Output is correct
5 Correct 40 ms 68652 KB Output is correct
6 Correct 41 ms 68860 KB Output is correct
7 Correct 41 ms 68840 KB Output is correct
8 Correct 41 ms 68936 KB Output is correct
9 Correct 42 ms 69064 KB Output is correct
10 Correct 45 ms 69080 KB Output is correct
11 Correct 48 ms 69228 KB Output is correct
12 Correct 40 ms 69092 KB Output is correct
13 Correct 45 ms 69176 KB Output is correct
14 Correct 43 ms 69096 KB Output is correct
15 Correct 46 ms 69596 KB Output is correct
16 Correct 42 ms 69044 KB Output is correct
17 Correct 44 ms 69172 KB Output is correct
18 Correct 44 ms 69096 KB Output is correct
19 Correct 41 ms 68940 KB Output is correct
20 Correct 41 ms 68804 KB Output is correct
21 Correct 40 ms 68888 KB Output is correct
22 Correct 39 ms 68688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 183 ms 99172 KB Output is correct
2 Correct 216 ms 104076 KB Output is correct
3 Correct 161 ms 99448 KB Output is correct
4 Correct 335 ms 112796 KB Output is correct
5 Correct 900 ms 158420 KB Output is correct
6 Correct 50 ms 69784 KB Output is correct
7 Correct 43 ms 69000 KB Output is correct
8 Correct 43 ms 69332 KB Output is correct
9 Correct 365 ms 93244 KB Output is correct
10 Correct 408 ms 91776 KB Output is correct
11 Correct 297 ms 91168 KB Output is correct
12 Correct 366 ms 91708 KB Output is correct
13 Correct 1107 ms 241036 KB Output is correct
14 Correct 951 ms 231528 KB Output is correct
15 Correct 1415 ms 278676 KB Output is correct
16 Correct 1265 ms 269264 KB Output is correct
17 Correct 963 ms 231716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 589 ms 90508 KB Output is correct
2 Correct 536 ms 88796 KB Output is correct
3 Correct 541 ms 88876 KB Output is correct
4 Correct 544 ms 88948 KB Output is correct
5 Correct 506 ms 88012 KB Output is correct
6 Correct 511 ms 88860 KB Output is correct
7 Correct 421 ms 80204 KB Output is correct
8 Correct 388 ms 80192 KB Output is correct
9 Correct 659 ms 88992 KB Output is correct
10 Correct 594 ms 88928 KB Output is correct
11 Correct 520 ms 88896 KB Output is correct
12 Correct 405 ms 80156 KB Output is correct
13 Correct 317 ms 77332 KB Output is correct
14 Correct 344 ms 78596 KB Output is correct
15 Correct 364 ms 78656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 157 ms 93784 KB Output is correct
2 Correct 135 ms 93840 KB Output is correct
3 Correct 136 ms 93864 KB Output is correct
4 Correct 143 ms 93840 KB Output is correct
5 Correct 132 ms 91624 KB Output is correct
6 Correct 40 ms 69028 KB Output is correct
7 Correct 40 ms 68700 KB Output is correct
8 Correct 40 ms 68844 KB Output is correct
9 Correct 157 ms 83932 KB Output is correct
10 Correct 140 ms 83948 KB Output is correct
11 Correct 171 ms 83924 KB Output is correct
12 Correct 131 ms 83236 KB Output is correct
13 Correct 121 ms 92576 KB Output is correct
14 Correct 39 ms 68644 KB Output is correct
15 Correct 39 ms 68568 KB Output is correct
16 Correct 38 ms 68572 KB Output is correct
17 Correct 39 ms 68564 KB Output is correct
18 Correct 40 ms 68652 KB Output is correct
19 Correct 41 ms 68860 KB Output is correct
20 Correct 41 ms 68840 KB Output is correct
21 Correct 41 ms 68936 KB Output is correct
22 Correct 42 ms 69064 KB Output is correct
23 Correct 45 ms 69080 KB Output is correct
24 Correct 48 ms 69228 KB Output is correct
25 Correct 40 ms 69092 KB Output is correct
26 Correct 45 ms 69176 KB Output is correct
27 Correct 43 ms 69096 KB Output is correct
28 Correct 46 ms 69596 KB Output is correct
29 Correct 42 ms 69044 KB Output is correct
30 Correct 44 ms 69172 KB Output is correct
31 Correct 44 ms 69096 KB Output is correct
32 Correct 41 ms 68940 KB Output is correct
33 Correct 41 ms 68804 KB Output is correct
34 Correct 40 ms 68888 KB Output is correct
35 Correct 39 ms 68688 KB Output is correct
36 Correct 183 ms 99172 KB Output is correct
37 Correct 216 ms 104076 KB Output is correct
38 Correct 161 ms 99448 KB Output is correct
39 Correct 335 ms 112796 KB Output is correct
40 Correct 900 ms 158420 KB Output is correct
41 Correct 50 ms 69784 KB Output is correct
42 Correct 43 ms 69000 KB Output is correct
43 Correct 43 ms 69332 KB Output is correct
44 Correct 365 ms 93244 KB Output is correct
45 Correct 408 ms 91776 KB Output is correct
46 Correct 297 ms 91168 KB Output is correct
47 Correct 366 ms 91708 KB Output is correct
48 Correct 1107 ms 241036 KB Output is correct
49 Correct 951 ms 231528 KB Output is correct
50 Correct 1415 ms 278676 KB Output is correct
51 Correct 1265 ms 269264 KB Output is correct
52 Correct 963 ms 231716 KB Output is correct
53 Correct 589 ms 90508 KB Output is correct
54 Correct 536 ms 88796 KB Output is correct
55 Correct 541 ms 88876 KB Output is correct
56 Correct 544 ms 88948 KB Output is correct
57 Correct 506 ms 88012 KB Output is correct
58 Correct 511 ms 88860 KB Output is correct
59 Correct 421 ms 80204 KB Output is correct
60 Correct 388 ms 80192 KB Output is correct
61 Correct 659 ms 88992 KB Output is correct
62 Correct 594 ms 88928 KB Output is correct
63 Correct 520 ms 88896 KB Output is correct
64 Correct 405 ms 80156 KB Output is correct
65 Correct 317 ms 77332 KB Output is correct
66 Correct 344 ms 78596 KB Output is correct
67 Correct 364 ms 78656 KB Output is correct
68 Correct 43 ms 68564 KB Output is correct
69 Correct 43 ms 68548 KB Output is correct
70 Correct 605 ms 98756 KB Output is correct
71 Correct 656 ms 98748 KB Output is correct
72 Correct 558 ms 98716 KB Output is correct
73 Correct 610 ms 98780 KB Output is correct
74 Correct 787 ms 99192 KB Output is correct
75 Correct 699 ms 95080 KB Output is correct
76 Correct 521 ms 88820 KB Output is correct
77 Correct 565 ms 89420 KB Output is correct
78 Correct 606 ms 90524 KB Output is correct
79 Correct 590 ms 93812 KB Output is correct
80 Correct 727 ms 93404 KB Output is correct
81 Correct 711 ms 94092 KB Output is correct
82 Correct 420 ms 85520 KB Output is correct
83 Correct 421 ms 94588 KB Output is correct
84 Correct 460 ms 93904 KB Output is correct
85 Correct 429 ms 94036 KB Output is correct