显然 $LCT$ 维护,除了翻转标记,还要维护加法,乘法和子树大小
注意模数看起来很小,但是乘的时候还是会爆 $int$ ,所以要用 $unsigned\ int$
代码中的标记为延时标记,就是当前节点的标记说明当前节点还没更新,要等到下传标记时才更新,个人认为会好写一些(不用考虑先乘还是先加)
注意乘法标记初始为 $1$ ,乘的时候有可能乘 $0$
#include#include #include #include #include using namespace std;typedef long long ll;typedef unsigned int uint;inline int read(){ int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return x*f;}const int N=2e5+7,mo=51061;uint c[N][2],fa[N],val[N],sz[N],sum[N],rev[N],add[N],mul[N];inline uint MOD(uint x) { return x>=mo ? x-mo : x; }//给加法取模inline void MUL(uint &x,uint c) { x=x*c%mo; }//乘inline void pushdown(int x)//下传标记{ if(!x||(!rev[x]&&!add[x]&&mul[x]==1)) return; uint &lc=c[x][0],&rc=c[x][1]; if(rev[x]) { swap(lc,rc); rev[x]=0; if(lc) rev[lc]^=1; if(rc) rev[rc]^=1; } if(mul[x]!=1) { MUL(sum[x],mul[x]); MUL(val[x],mul[x]); if(lc) MUL(add[lc],mul[x]),MUL(mul[lc],mul[x]); if(rc) MUL(add[rc],mul[x]),MUL(mul[rc],mul[x]); mul[x]=1; } if(add[x]) { sum[x]=(sum[x]+sz[x]*add[x])%mo; val[x]=MOD(val[x]+add[x]); if(lc) add[lc]=MOD(add[lc]+add[x]); if(rc) add[rc]=MOD(add[rc]+add[x]); add[x]=0; }}inline void pushup(int x)//更新节点{ uint &lc=c[x][0],&rc=c[x][1]; sz[x]=sz[lc]+sz[rc]+1; sum[x]=MOD(MOD(sum[lc]+sum[rc])+val[x]);}inline void rever(int x) { rev[x]=1; pushdown(x); }inline bool noroot(int x) { return (c[fa[x]][0]==x)|(c[fa[x]][1]==x); }inline void rotate(int x){ int y=fa[x],z=fa[y],d=(c[y][1]==x); if(noroot(y)) c[z][c[z][1]==y]=x; fa[x]=z; fa[y]=x; fa[c[x][d^1]]=y; c[y][d]=c[x][d^1]; c[x][d^1]=y; pushup(y); pushup(x);}inline void push_tag(int x)//清空标记{ if(noroot(x)) push_tag(fa[x]); else pushdown(x); pushdown(c[x][0]); pushdown(c[x][1]);}inline void splay(int x){ push_tag(x);//splay前记得先把上面的标记清空 while(noroot(x)) { int y=fa[x],z=fa[y]; if(noroot(y)) { if(c[y][0]==x ^ c[z][0]==y) rotate(x); else rotate(y); } rotate(x); }}inline void access(int x){ for(int y=0;x;y=x,x=fa[x]) splay(x),c[x][1]=y,pushup(x);}inline void makeroot(int x) { access(x); splay(x); rever(x); }inline void split(int x,int y) { makeroot(x); access(y); splay(y); }inline void link(int x,int y) { makeroot(x); fa[x]=y; }inline void cut(int x,int y) { split(x,y); fa[x]=c[y][0]=0; pushup(y); }int n,m;int main(){ //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); int a,b,c; char s[7]; n=read(),m=read(); for(int i=1;i<=n;i++) sum[i]=val[i]=sz[i]=mul[i]=1; for(int i=1;i