#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((long long)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{
long long seg[(1<<20)];
long long pors(long long i,long long l,long long r,long long tl,long long tr){
if(l>r||l>tr||r<tl||tl>tr){
return 0;
}
if(l>=tl&&r<=tr){
return seg[i];
}
long long m=(l+r)>>1;
long long ret=pors((i<<1),l,m,tl,tr)+pors((i<<1)^1,m+1,r,tl,tr);
return ret;
}
void upd(long long i,long long w){
if(i==0){
return ;
}
seg[i]+=w;
upd((i>>1),w);
}
}segjam,segted;
struct segres{
long long seg[(1<<20)];
segres(){
for(long long i=kaf;i<(1<<20);i++){
seg[i]=-1;
}
for(long long 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,long long w){
if(l>r||l>tr||r<tl||tl>tr){
return ;
}
if(l>=tl&&r<=tr){
w%=mod;
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(long long i=1;i<maxm;i++){
fact[i]=1ll*fact[i-1]*i%mod;
}
revfact[maxm-1]=mypow(fact[maxm-1],mod-2);
for(long long i=maxm-2;i>=0;i--){
revfact[i]=1ll*revfact[i+1]*(i+1)%mod;
}
}
long long c(long long i,long long j){
if(i<0||j<0||i<j){
return 0;
}
long long ret=1ll*fact[i]*revfact[j]%mod*revfact[i-j]%mod;
return ret;
}
void dfs(long long u,long long 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;
}
long long hes(long long a,long long b){
a++;
long long 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;
}
long long porsjam(int u,int av){
long long ret=0;
if(u!=av&&val[u]!=-1){
ret+=val[u];
return ret;
}
for(auto x:child[u]){
ret+=porsjam(x,av);
}
return ret;
}
long long porsted(int u,int av){
long long ret=0;
if(u!=av&&val[u]!=-1){
return ret;
}
for(auto x:child[u]){
ret+=porsted(x,av);
}
ret++;
return ret;
}
void upd(long long u,long long w){
wa[u]=w;
val[u]=w;
long long pp=segbal.get(stf[u].first+kaf).second;
// long long pp=findp(u,u);
long long jam=segjam.pors(1,0,kaf-1,stf[u].first+1,stf[u].second);
// long long jam=porsjam(u,u);
long long ted=cnt[u]-segted.pors(1,0,kaf-1,stf[u].first+1,stf[u].second);
// long long ted=porsted(u,u);
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);
long long jamp=segjam.pors(1,0,kaf-1,stf[pp].first+1,stf[pp].second);
// long long jamp=porsjam(pp,pp);
long long tedp=cnt[pp]-segted.pors(1,0,kaf-1,stf[pp].first+1,stf[pp].second);
// long long tedp=porsted(pp,pp);
long long fake=hes(tedp-1,val[pp]-jamp);
// fres[stf[pp].first]=fake;
// cout<<u<<" "<<pp<<" "<<jamp<<" "<<val[pp]<<" "<<tedp<<"\n";
res.upd(1,0,kaf-1,stf[pp].first,stf[pp].first,fake);
fake=hes(ted-1,val[u]-jam);
// fres[stf[u].first]=fake;
res.upd(1,0,kaf-1,stf[u].first,stf[u].first,fake);
}
void del(long long u){
long long pp=segbal.get(stf[u].first+kaf).second;
// long long pp=findp(u,u);
long long jam=segjam.pors(1,0,kaf-1,stf[u].first+1,stf[u].second);
// long long jam=porsjam(u,u);
long long ted=cnt[u]-segted.pors(1,0,kaf-1,stf[u].first+1,stf[u].second);
// long long ted=porsted(u,u);
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;
long long jamp=segjam.pors(1,0,kaf-1,stf[pp].first+1,stf[pp].second);
// long long jamp=porsjam(pp,pp);
long long tedp=cnt[pp]-segted.pors(1,0,kaf-1,stf[pp].first+1,stf[pp].second);
// long long tedp=porsted(pp,pp);
long long 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,-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);
// val[u]=-1;
}
void calres(){
long long ret=1;
for(int i=stf[1].first;i<=stf[1].second;i++){
// cout<<i<<" asd "<<fres[i]<<"\n";
if(fres[i]==-1){
continue;
}
ret*=fres[i];
ret%=mod;
}
cout<<ret<<"\n";
}
void tof(){
//cout<<porsted(1,1)<<" "<<porsjam(1,1)<<"\n";
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(long long i=0;i<n-1;i++){
long long u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
// segjam.upd(stf[1].first+kaf,k);
val[1]=k;
tof();
long long q;
cin>>q;
asd=1;
cout<<res.seg[1]<<"\n";
// calres();
for(;asd<=q;asd++){
long long qq;
cin>>qq;
if(qq==1){
long long u,w;
cin>>u>>w;
upd(u,w);
}
else{
long long u;
cin>>u;
del(u);
}
cout<<res.seg[1]<<"\n";
// calres();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
94560 KB |
Output is correct |
2 |
Correct |
132 ms |
94536 KB |
Output is correct |
3 |
Correct |
134 ms |
94620 KB |
Output is correct |
4 |
Correct |
136 ms |
94568 KB |
Output is correct |
5 |
Correct |
134 ms |
92996 KB |
Output is correct |
6 |
Correct |
44 ms |
73048 KB |
Output is correct |
7 |
Correct |
42 ms |
72784 KB |
Output is correct |
8 |
Correct |
43 ms |
72832 KB |
Output is correct |
9 |
Correct |
153 ms |
86392 KB |
Output is correct |
10 |
Correct |
140 ms |
86236 KB |
Output is correct |
11 |
Correct |
148 ms |
86220 KB |
Output is correct |
12 |
Correct |
135 ms |
85620 KB |
Output is correct |
13 |
Correct |
125 ms |
93368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
72704 KB |
Output is correct |
2 |
Correct |
42 ms |
72736 KB |
Output is correct |
3 |
Correct |
40 ms |
72684 KB |
Output is correct |
4 |
Correct |
41 ms |
72788 KB |
Output is correct |
5 |
Correct |
43 ms |
72812 KB |
Output is correct |
6 |
Correct |
45 ms |
73080 KB |
Output is correct |
7 |
Correct |
48 ms |
73108 KB |
Output is correct |
8 |
Correct |
46 ms |
72952 KB |
Output is correct |
9 |
Correct |
46 ms |
73144 KB |
Output is correct |
10 |
Correct |
47 ms |
73232 KB |
Output is correct |
11 |
Correct |
48 ms |
73408 KB |
Output is correct |
12 |
Correct |
43 ms |
73212 KB |
Output is correct |
13 |
Correct |
48 ms |
73316 KB |
Output is correct |
14 |
Correct |
48 ms |
73264 KB |
Output is correct |
15 |
Correct |
50 ms |
73660 KB |
Output is correct |
16 |
Correct |
50 ms |
73056 KB |
Output is correct |
17 |
Correct |
48 ms |
73288 KB |
Output is correct |
18 |
Correct |
47 ms |
73204 KB |
Output is correct |
19 |
Correct |
45 ms |
73052 KB |
Output is correct |
20 |
Correct |
44 ms |
72948 KB |
Output is correct |
21 |
Correct |
44 ms |
72956 KB |
Output is correct |
22 |
Correct |
41 ms |
72860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
175 ms |
103372 KB |
Output is correct |
2 |
Correct |
223 ms |
108668 KB |
Output is correct |
3 |
Correct |
179 ms |
104524 KB |
Output is correct |
4 |
Correct |
322 ms |
117736 KB |
Output is correct |
5 |
Correct |
868 ms |
164164 KB |
Output is correct |
6 |
Correct |
48 ms |
73804 KB |
Output is correct |
7 |
Correct |
44 ms |
73128 KB |
Output is correct |
8 |
Correct |
46 ms |
73440 KB |
Output is correct |
9 |
Correct |
373 ms |
99872 KB |
Output is correct |
10 |
Correct |
321 ms |
98292 KB |
Output is correct |
11 |
Correct |
301 ms |
97740 KB |
Output is correct |
12 |
Correct |
365 ms |
98264 KB |
Output is correct |
13 |
Correct |
999 ms |
244480 KB |
Output is correct |
14 |
Correct |
917 ms |
235092 KB |
Output is correct |
15 |
Correct |
1279 ms |
282064 KB |
Output is correct |
16 |
Correct |
1202 ms |
272652 KB |
Output is correct |
17 |
Correct |
925 ms |
235180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
526 ms |
95416 KB |
Output is correct |
2 |
Correct |
523 ms |
95336 KB |
Output is correct |
3 |
Correct |
529 ms |
95552 KB |
Output is correct |
4 |
Correct |
525 ms |
95512 KB |
Output is correct |
5 |
Correct |
492 ms |
94336 KB |
Output is correct |
6 |
Correct |
519 ms |
95516 KB |
Output is correct |
7 |
Correct |
398 ms |
85108 KB |
Output is correct |
8 |
Correct |
398 ms |
85152 KB |
Output is correct |
9 |
Correct |
519 ms |
95424 KB |
Output is correct |
10 |
Correct |
495 ms |
95380 KB |
Output is correct |
11 |
Correct |
490 ms |
95308 KB |
Output is correct |
12 |
Correct |
408 ms |
85008 KB |
Output is correct |
13 |
Correct |
298 ms |
80804 KB |
Output is correct |
14 |
Correct |
340 ms |
83140 KB |
Output is correct |
15 |
Correct |
352 ms |
83448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
94560 KB |
Output is correct |
2 |
Correct |
132 ms |
94536 KB |
Output is correct |
3 |
Correct |
134 ms |
94620 KB |
Output is correct |
4 |
Correct |
136 ms |
94568 KB |
Output is correct |
5 |
Correct |
134 ms |
92996 KB |
Output is correct |
6 |
Correct |
44 ms |
73048 KB |
Output is correct |
7 |
Correct |
42 ms |
72784 KB |
Output is correct |
8 |
Correct |
43 ms |
72832 KB |
Output is correct |
9 |
Correct |
153 ms |
86392 KB |
Output is correct |
10 |
Correct |
140 ms |
86236 KB |
Output is correct |
11 |
Correct |
148 ms |
86220 KB |
Output is correct |
12 |
Correct |
135 ms |
85620 KB |
Output is correct |
13 |
Correct |
125 ms |
93368 KB |
Output is correct |
14 |
Correct |
42 ms |
72704 KB |
Output is correct |
15 |
Correct |
42 ms |
72736 KB |
Output is correct |
16 |
Correct |
40 ms |
72684 KB |
Output is correct |
17 |
Correct |
41 ms |
72788 KB |
Output is correct |
18 |
Correct |
43 ms |
72812 KB |
Output is correct |
19 |
Correct |
45 ms |
73080 KB |
Output is correct |
20 |
Correct |
48 ms |
73108 KB |
Output is correct |
21 |
Correct |
46 ms |
72952 KB |
Output is correct |
22 |
Correct |
46 ms |
73144 KB |
Output is correct |
23 |
Correct |
47 ms |
73232 KB |
Output is correct |
24 |
Correct |
48 ms |
73408 KB |
Output is correct |
25 |
Correct |
43 ms |
73212 KB |
Output is correct |
26 |
Correct |
48 ms |
73316 KB |
Output is correct |
27 |
Correct |
48 ms |
73264 KB |
Output is correct |
28 |
Correct |
50 ms |
73660 KB |
Output is correct |
29 |
Correct |
50 ms |
73056 KB |
Output is correct |
30 |
Correct |
48 ms |
73288 KB |
Output is correct |
31 |
Correct |
47 ms |
73204 KB |
Output is correct |
32 |
Correct |
45 ms |
73052 KB |
Output is correct |
33 |
Correct |
44 ms |
72948 KB |
Output is correct |
34 |
Correct |
44 ms |
72956 KB |
Output is correct |
35 |
Correct |
41 ms |
72860 KB |
Output is correct |
36 |
Correct |
175 ms |
103372 KB |
Output is correct |
37 |
Correct |
223 ms |
108668 KB |
Output is correct |
38 |
Correct |
179 ms |
104524 KB |
Output is correct |
39 |
Correct |
322 ms |
117736 KB |
Output is correct |
40 |
Correct |
868 ms |
164164 KB |
Output is correct |
41 |
Correct |
48 ms |
73804 KB |
Output is correct |
42 |
Correct |
44 ms |
73128 KB |
Output is correct |
43 |
Correct |
46 ms |
73440 KB |
Output is correct |
44 |
Correct |
373 ms |
99872 KB |
Output is correct |
45 |
Correct |
321 ms |
98292 KB |
Output is correct |
46 |
Correct |
301 ms |
97740 KB |
Output is correct |
47 |
Correct |
365 ms |
98264 KB |
Output is correct |
48 |
Correct |
999 ms |
244480 KB |
Output is correct |
49 |
Correct |
917 ms |
235092 KB |
Output is correct |
50 |
Correct |
1279 ms |
282064 KB |
Output is correct |
51 |
Correct |
1202 ms |
272652 KB |
Output is correct |
52 |
Correct |
925 ms |
235180 KB |
Output is correct |
53 |
Correct |
526 ms |
95416 KB |
Output is correct |
54 |
Correct |
523 ms |
95336 KB |
Output is correct |
55 |
Correct |
529 ms |
95552 KB |
Output is correct |
56 |
Correct |
525 ms |
95512 KB |
Output is correct |
57 |
Correct |
492 ms |
94336 KB |
Output is correct |
58 |
Correct |
519 ms |
95516 KB |
Output is correct |
59 |
Correct |
398 ms |
85108 KB |
Output is correct |
60 |
Correct |
398 ms |
85152 KB |
Output is correct |
61 |
Correct |
519 ms |
95424 KB |
Output is correct |
62 |
Correct |
495 ms |
95380 KB |
Output is correct |
63 |
Correct |
490 ms |
95308 KB |
Output is correct |
64 |
Correct |
408 ms |
85008 KB |
Output is correct |
65 |
Correct |
298 ms |
80804 KB |
Output is correct |
66 |
Correct |
340 ms |
83140 KB |
Output is correct |
67 |
Correct |
352 ms |
83448 KB |
Output is correct |
68 |
Correct |
42 ms |
72700 KB |
Output is correct |
69 |
Correct |
41 ms |
72736 KB |
Output is correct |
70 |
Correct |
579 ms |
103816 KB |
Output is correct |
71 |
Correct |
575 ms |
103832 KB |
Output is correct |
72 |
Correct |
574 ms |
103672 KB |
Output is correct |
73 |
Correct |
580 ms |
103708 KB |
Output is correct |
74 |
Correct |
738 ms |
104104 KB |
Output is correct |
75 |
Correct |
744 ms |
100760 KB |
Output is correct |
76 |
Correct |
507 ms |
95308 KB |
Output is correct |
77 |
Correct |
573 ms |
96044 KB |
Output is correct |
78 |
Correct |
600 ms |
96964 KB |
Output is correct |
79 |
Correct |
584 ms |
99436 KB |
Output is correct |
80 |
Correct |
670 ms |
98924 KB |
Output is correct |
81 |
Correct |
676 ms |
99736 KB |
Output is correct |
82 |
Correct |
381 ms |
89572 KB |
Output is correct |
83 |
Correct |
401 ms |
96560 KB |
Output is correct |
84 |
Correct |
454 ms |
95856 KB |
Output is correct |
85 |
Correct |
410 ms |
95936 KB |
Output is correct |