# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1036902 |
2024-07-27T19:26:36 Z |
PM1 |
Sumtree (INOI20_sumtree) |
C++17 |
|
1046 ms |
237140 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,int z){
if(x>y){
bad+=z;
return 1;
}
//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,1);
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);
ans*=ferma(comb(precnt-1,precnt-1+pre,-1));
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);
ans*=comb(precnt-1,precnt+pre-1,1);
ans%=M;
ans*=comb(ycnt-1,ycnt+y-1,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);
ans*=ferma(comb(precnt-1,precnt-1+pre,-1));
ans%=M;
ans*=ferma(comb(zircnt-1,zircnt+zir-1,-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);
ans*=comb(precnt-1,precnt+pre-1,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:94:20: warning: unused variable 'res' [-Wunused-variable]
94 | int mid=(L+R)>>1,res=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
138344 KB |
Output is correct |
2 |
Correct |
186 ms |
136532 KB |
Output is correct |
3 |
Correct |
217 ms |
135332 KB |
Output is correct |
4 |
Correct |
193 ms |
137872 KB |
Output is correct |
5 |
Correct |
206 ms |
134100 KB |
Output is correct |
6 |
Correct |
85 ms |
115796 KB |
Output is correct |
7 |
Correct |
83 ms |
115536 KB |
Output is correct |
8 |
Correct |
70 ms |
113232 KB |
Output is correct |
9 |
Correct |
196 ms |
130900 KB |
Output is correct |
10 |
Correct |
226 ms |
131256 KB |
Output is correct |
11 |
Correct |
213 ms |
131408 KB |
Output is correct |
12 |
Correct |
185 ms |
127004 KB |
Output is correct |
13 |
Correct |
185 ms |
136952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
110676 KB |
Output is correct |
2 |
Correct |
44 ms |
110684 KB |
Output is correct |
3 |
Correct |
47 ms |
110676 KB |
Output is correct |
4 |
Correct |
45 ms |
110648 KB |
Output is correct |
5 |
Correct |
49 ms |
110672 KB |
Output is correct |
6 |
Correct |
53 ms |
111188 KB |
Output is correct |
7 |
Correct |
56 ms |
111312 KB |
Output is correct |
8 |
Correct |
57 ms |
111444 KB |
Output is correct |
9 |
Correct |
62 ms |
111584 KB |
Output is correct |
10 |
Correct |
68 ms |
112720 KB |
Output is correct |
11 |
Correct |
91 ms |
114388 KB |
Output is correct |
12 |
Correct |
87 ms |
115708 KB |
Output is correct |
13 |
Correct |
95 ms |
115540 KB |
Output is correct |
14 |
Correct |
91 ms |
115660 KB |
Output is correct |
15 |
Correct |
94 ms |
115792 KB |
Output is correct |
16 |
Correct |
94 ms |
115792 KB |
Output is correct |
17 |
Correct |
75 ms |
113492 KB |
Output is correct |
18 |
Correct |
91 ms |
115372 KB |
Output is correct |
19 |
Correct |
83 ms |
114928 KB |
Output is correct |
20 |
Correct |
57 ms |
111024 KB |
Output is correct |
21 |
Correct |
58 ms |
110932 KB |
Output is correct |
22 |
Correct |
51 ms |
110904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
248 ms |
144464 KB |
Output is correct |
2 |
Correct |
309 ms |
148304 KB |
Output is correct |
3 |
Correct |
227 ms |
142420 KB |
Output is correct |
4 |
Correct |
425 ms |
159568 KB |
Output is correct |
5 |
Correct |
868 ms |
192336 KB |
Output is correct |
6 |
Correct |
90 ms |
116048 KB |
Output is correct |
7 |
Correct |
89 ms |
115540 KB |
Output is correct |
8 |
Correct |
67 ms |
113744 KB |
Output is correct |
9 |
Correct |
553 ms |
145980 KB |
Output is correct |
10 |
Correct |
488 ms |
143708 KB |
Output is correct |
11 |
Correct |
419 ms |
142900 KB |
Output is correct |
12 |
Correct |
504 ms |
141024 KB |
Output is correct |
13 |
Correct |
1006 ms |
237140 KB |
Output is correct |
14 |
Correct |
1029 ms |
236948 KB |
Output is correct |
15 |
Correct |
993 ms |
236940 KB |
Output is correct |
16 |
Correct |
986 ms |
237136 KB |
Output is correct |
17 |
Correct |
917 ms |
236940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
858 ms |
139048 KB |
Output is correct |
2 |
Correct |
773 ms |
139344 KB |
Output is correct |
3 |
Correct |
787 ms |
139184 KB |
Output is correct |
4 |
Correct |
778 ms |
139316 KB |
Output is correct |
5 |
Correct |
829 ms |
138416 KB |
Output is correct |
6 |
Correct |
741 ms |
134796 KB |
Output is correct |
7 |
Correct |
639 ms |
124520 KB |
Output is correct |
8 |
Correct |
734 ms |
128668 KB |
Output is correct |
9 |
Correct |
863 ms |
139020 KB |
Output is correct |
10 |
Correct |
720 ms |
138936 KB |
Output is correct |
11 |
Correct |
760 ms |
139156 KB |
Output is correct |
12 |
Correct |
650 ms |
128964 KB |
Output is correct |
13 |
Correct |
588 ms |
125764 KB |
Output is correct |
14 |
Correct |
639 ms |
127060 KB |
Output is correct |
15 |
Correct |
638 ms |
126804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
138344 KB |
Output is correct |
2 |
Correct |
186 ms |
136532 KB |
Output is correct |
3 |
Correct |
217 ms |
135332 KB |
Output is correct |
4 |
Correct |
193 ms |
137872 KB |
Output is correct |
5 |
Correct |
206 ms |
134100 KB |
Output is correct |
6 |
Correct |
85 ms |
115796 KB |
Output is correct |
7 |
Correct |
83 ms |
115536 KB |
Output is correct |
8 |
Correct |
70 ms |
113232 KB |
Output is correct |
9 |
Correct |
196 ms |
130900 KB |
Output is correct |
10 |
Correct |
226 ms |
131256 KB |
Output is correct |
11 |
Correct |
213 ms |
131408 KB |
Output is correct |
12 |
Correct |
185 ms |
127004 KB |
Output is correct |
13 |
Correct |
185 ms |
136952 KB |
Output is correct |
14 |
Correct |
44 ms |
110676 KB |
Output is correct |
15 |
Correct |
44 ms |
110684 KB |
Output is correct |
16 |
Correct |
47 ms |
110676 KB |
Output is correct |
17 |
Correct |
45 ms |
110648 KB |
Output is correct |
18 |
Correct |
49 ms |
110672 KB |
Output is correct |
19 |
Correct |
53 ms |
111188 KB |
Output is correct |
20 |
Correct |
56 ms |
111312 KB |
Output is correct |
21 |
Correct |
57 ms |
111444 KB |
Output is correct |
22 |
Correct |
62 ms |
111584 KB |
Output is correct |
23 |
Correct |
68 ms |
112720 KB |
Output is correct |
24 |
Correct |
91 ms |
114388 KB |
Output is correct |
25 |
Correct |
87 ms |
115708 KB |
Output is correct |
26 |
Correct |
95 ms |
115540 KB |
Output is correct |
27 |
Correct |
91 ms |
115660 KB |
Output is correct |
28 |
Correct |
94 ms |
115792 KB |
Output is correct |
29 |
Correct |
94 ms |
115792 KB |
Output is correct |
30 |
Correct |
75 ms |
113492 KB |
Output is correct |
31 |
Correct |
91 ms |
115372 KB |
Output is correct |
32 |
Correct |
83 ms |
114928 KB |
Output is correct |
33 |
Correct |
57 ms |
111024 KB |
Output is correct |
34 |
Correct |
58 ms |
110932 KB |
Output is correct |
35 |
Correct |
51 ms |
110904 KB |
Output is correct |
36 |
Correct |
248 ms |
144464 KB |
Output is correct |
37 |
Correct |
309 ms |
148304 KB |
Output is correct |
38 |
Correct |
227 ms |
142420 KB |
Output is correct |
39 |
Correct |
425 ms |
159568 KB |
Output is correct |
40 |
Correct |
868 ms |
192336 KB |
Output is correct |
41 |
Correct |
90 ms |
116048 KB |
Output is correct |
42 |
Correct |
89 ms |
115540 KB |
Output is correct |
43 |
Correct |
67 ms |
113744 KB |
Output is correct |
44 |
Correct |
553 ms |
145980 KB |
Output is correct |
45 |
Correct |
488 ms |
143708 KB |
Output is correct |
46 |
Correct |
419 ms |
142900 KB |
Output is correct |
47 |
Correct |
504 ms |
141024 KB |
Output is correct |
48 |
Correct |
1006 ms |
237140 KB |
Output is correct |
49 |
Correct |
1029 ms |
236948 KB |
Output is correct |
50 |
Correct |
993 ms |
236940 KB |
Output is correct |
51 |
Correct |
986 ms |
237136 KB |
Output is correct |
52 |
Correct |
917 ms |
236940 KB |
Output is correct |
53 |
Correct |
858 ms |
139048 KB |
Output is correct |
54 |
Correct |
773 ms |
139344 KB |
Output is correct |
55 |
Correct |
787 ms |
139184 KB |
Output is correct |
56 |
Correct |
778 ms |
139316 KB |
Output is correct |
57 |
Correct |
829 ms |
138416 KB |
Output is correct |
58 |
Correct |
741 ms |
134796 KB |
Output is correct |
59 |
Correct |
639 ms |
124520 KB |
Output is correct |
60 |
Correct |
734 ms |
128668 KB |
Output is correct |
61 |
Correct |
863 ms |
139020 KB |
Output is correct |
62 |
Correct |
720 ms |
138936 KB |
Output is correct |
63 |
Correct |
760 ms |
139156 KB |
Output is correct |
64 |
Correct |
650 ms |
128964 KB |
Output is correct |
65 |
Correct |
588 ms |
125764 KB |
Output is correct |
66 |
Correct |
639 ms |
127060 KB |
Output is correct |
67 |
Correct |
638 ms |
126804 KB |
Output is correct |
68 |
Correct |
49 ms |
110676 KB |
Output is correct |
69 |
Correct |
45 ms |
110676 KB |
Output is correct |
70 |
Correct |
815 ms |
147284 KB |
Output is correct |
71 |
Correct |
815 ms |
147168 KB |
Output is correct |
72 |
Correct |
804 ms |
147452 KB |
Output is correct |
73 |
Correct |
832 ms |
147300 KB |
Output is correct |
74 |
Correct |
966 ms |
146308 KB |
Output is correct |
75 |
Correct |
1046 ms |
143436 KB |
Output is correct |
76 |
Correct |
729 ms |
139396 KB |
Output is correct |
77 |
Correct |
763 ms |
139604 KB |
Output is correct |
78 |
Correct |
909 ms |
139684 KB |
Output is correct |
79 |
Correct |
874 ms |
142420 KB |
Output is correct |
80 |
Correct |
917 ms |
141908 KB |
Output is correct |
81 |
Correct |
912 ms |
142420 KB |
Output is correct |
82 |
Correct |
626 ms |
135740 KB |
Output is correct |
83 |
Correct |
664 ms |
142960 KB |
Output is correct |
84 |
Correct |
733 ms |
141500 KB |
Output is correct |
85 |
Correct |
719 ms |
142164 KB |
Output is correct |