BZOJ2631 tree LCT
欢迎访问~原文出处——博客园-zhouzhendong
去博客园看该题解
题目传送门 - BZOJ2631
题意概括
一棵n个节点的树,每一个节点有一个权值,m次操作。
要支持操作有:删边、连边、区间求和、区间加、区间乘。
保证操作过程中不出现环。
n,m<=100000
题解
差不多是基础的LCT,加个懒标记。
2个懒标记,一个是乘的,一个是加的,下传的时候先乘后加。
注意用无符号的int,用LL会超时。
(数据较为友善,不卡无符号int)
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | #include <cstring> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> using namespace std; typedef long long LL; typedef unsigned int U; const int N=100005; const U Mod=51061; int n,m; int fa[N],son[N][2],rev[N]; U sum[N],addt[N],addp[N],val[N],size[N]; bool isd( char ch){ return '0' <=ch&&ch<= '9' ; } void read( int &x){ char ch= getchar (); x=0; while (!isd(ch)) ch= getchar (); while (isd(ch)) x=x*10+ch-48,ch= getchar (); } bool isroot( int x){ return son[fa[x]][0]!=x&&son[fa[x]][1]!=x; } void pushup( int x){ if (!x) return ; sum[x]=(sum[son[x][0]]+sum[son[x][1]]+val[x])%Mod; size[x]=(size[son[x][0]]+size[son[x][1]]+1)%Mod; } void pushson( int x,U t,U p){ if (!x) return ; addt[x]=addt[x]*t%Mod; addp[x]=(addp[x]*t+p)%Mod; val[x]=(val[x]*t+p)%Mod; sum[x]=(sum[x]*t+p*size[x])%Mod; } void pushdown( int x){ if (!x) return ; int &ls=son[x][0],&rs=son[x][1]; if (rev[x]){ rev[x]=0; rev[ls]^=1; rev[rs]^=1; swap(ls,rs); } U &t=addt[x],&p=addp[x]; pushson(ls,t,p); pushson(rs,t,p); t=1,p=0; } void pushadd( int x){ if (!x) return ; if (!isroot(x)) pushadd(fa[x]); pushdown(x); } int wson( int x){ return son[fa[x]][1]==x; } void rotate( int x){ if (isroot(x)) return ; int y=fa[x],z=fa[y],L=wson(x),R=L^1; if (!isroot(y)) son[z][wson(y)]=x; fa[x]=z,fa[y]=x,fa[son[x][R]]=y; son[y][L]=son[x][R],son[x][R]=y; pushup(y); pushup(x); } void splay( int x){ pushadd(x); for ( int y=fa[x];!isroot(x);rotate(x),y=fa[x]) if (!isroot(y)) rotate(wson(x)==wson(y)?y:x); } void access( int x){ int t=0; while (x){ splay(x); son[x][1]=t; pushup(x); t=x; x=fa[x]; } } void rever( int x){ access(x); splay(x); rev[x]^=1; } void link( int x, int y){ rever(x); fa[x]=y; } void cut( int x, int y){ rever(x); access(y); splay(y); fa[x]=son[y][0]=0; } int main(){ read(n),read(m); for ( int i=1;i<=n;i++){ val[i]=sum[i]=size[i]=addt[i]=1; fa[i]=son[i][0]=son[i][1]=addp[i]=rev[i]=0; } for ( int i=1,a,b;i<n;i++){ read(a),read(b); link(a,b); } while (m--){ char op[4]; int a,b,c,d; scanf ( "%s" ,op); if (op[0]== '+' ){ read(a),read(b),read(c); rever(a); access(b); splay(b); pushson(b,1,c); } if (op[0]== '-' ){ read(a),read(b),read(c),read(d); cut(a,b); link(c,d); } if (op[0]== '*' ){ read(a),read(b),read(c); rever(a); access(b); splay(b); pushson(b,c,0); } if (op[0]== '/' ){ read(a),read(b); rever(a); access(b); splay(b); printf ( "%u\n" ,sum[b]); } } return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)