随缘悬2~4关注,萌新刚学LCT $10^{\epsilon}$ 秒,求调/kk

P1501 [国家集训队] Tree II

```pusha(ch[u][0],lza[u]);pusha(ch[u][1],lza[u]);lza[u]=1;``` 蝴蝶要不要康康自己在干嘛 谁教你的清加法标记=1 S喵吗QwQ
by 洛苡hh @ 2024-05-13 16:50:26


AC代码: ```cpp #include<bits/stdc++.h> #define int long long using namespace std; const int N=114514,mod=51061; int n,q,sum[N],sz[N],w[N],ch[N][2],fa[N],lzr[N],lza[N],lzm[N]; string op; bool isheavy(int u) { return ch[fa[u]][0]==u||ch[fa[u]][1]==u; } void pushup(int u) { sz[u]=sz[ch[u][0]]+sz[ch[u][1]]+1; sum[u]=(sum[ch[u][0]]+sum[ch[u][1]]+w[u])%mod; } void pushr(int u) { // cout<<"r "<<u<<'\n'; swap(ch[u][0],ch[u][1]); lzr[u]^=1; } void pusha(int u,int c) { // cout<<"a "<<u<<' '<<c<<'\n'; (w[u]+=c)%=mod; (sum[u]+=c*sz[u])%=mod; (lza[u]+=c)%=mod; } void pushm(int u,int c) { // cout<<"m "<<u<<' '<<c<<'\n'; (w[u]*=c)%=mod; (sum[u]*=c)%=mod; (lza[u]*=c)%=mod; (lzm[u]*=c)%=mod; } void pushdown(int u) { // cout<<u<<'\n'; pushm(ch[u][0],lzm[u]);pushm(ch[u][1],lzm[u]);lzm[u]=1; pusha(ch[u][0],lza[u]);pusha(ch[u][1],lza[u]);lza[u]=0; if(lzr[u]) { if(ch[u][0])pushr(ch[u][0]); if(ch[u][1])pushr(ch[u][1]); lzr[u]=0; } } void rotate(int u) { int Fa,gfa,lr,son; Fa=fa[u];gfa=fa[Fa];lr=(ch[Fa][1]==u);son=ch[u][lr^1]; if(isheavy(Fa))ch[gfa][ch[gfa][1]==Fa]=u; if(son)fa[son]=Fa; ch[u][lr^1]=Fa; ch[Fa][lr]=son; fa[Fa]=u; fa[u]=gfa; pushup(Fa); } int chain[N]; void splay(int u) { int Fa=u,gfa,tot=1; chain[1]=u; while(isheavy(Fa))chain[++tot]=Fa=fa[Fa]; while(tot)pushdown(chain[tot--]); while(isheavy(u)) { Fa=fa[u];gfa=fa[Fa]; if(isheavy(Fa))rotate((ch[Fa][0]==u)^(ch[gfa][0]==Fa)?u:Fa); rotate(u); } pushup(u); } void access(int u) { int tmp=0; while(u) { splay(u); ch[u][1]=tmp; pushup(u); tmp=u; u=fa[u]; } } void makeroot(int u) { access(u); splay(u); pushr(u); } void split(int u,int v) { makeroot(u); access(v); splay(v); } void link(int u,int v) { makeroot(u); fa[u]=v; } void cut(int u,int v) { split(u,v); fa[u]=ch[v][0]=0; } signed main() { ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>n>>q; for(int i=1;i<=n;i++)w[i]=sz[i]=lzm[i]=1; for(int i=1,u,v;i<n;i++) { cin>>u>>v; link(u,v); } for(int i=1,u,v,k,l;i<=q;i++) { cin>>op>>u>>v; if(op=="+") { cin>>k; split(u,v); pusha(v,k); } if(op=="-") { cin>>k>>l; cut(u,v); link(k,l); } if(op=="*") { cin>>k; split(u,v); pushm(v,k); } if(op=="/") { split(u,v); cout<<sum[v]<<'\n'; } } } ``` 以及代码求调但不给全代码差评(
by 洛苡hh @ 2024-05-13 16:51:42


|