[国家集训队]Tree II
V.[国家集训队]Tree II
LCT维护这种东西是要比线段树要恶心的多的……毕竟线段树的区间大小是可以直接通过区间左右端点算出的,但是LCT就不行,必须手动维护。并且,线段树的维护是(左儿子+右儿子),但是LCT的维护是(左儿子+自己+右儿子)!
请务必先把线段树模板做掉,关于运算的优先级什么的实在不应该放到这道题里讲吧……
注意细节,比如修改时,乘法,加法,本身的值,区间的和,都要修改,并且每个修改的方式都还不一样。
代码:
#include<bits/stdc++.h>
using namespace std;
const int mod=51061;
#define lson t[x].ch[0]
#define rson t[x].ch[1]
int n,q;
struct LCT{
int ch[2],fa,plu,mul,rev,sum,val,sz;
}t[100100];
int identify(int x){
if(t[t[x].fa].ch[0]==x)return 0;
if(t[t[x].fa].ch[1]==x)return 1;
return -1;
}
void REV(int x){
t[x].rev^=1,swap(lson,rson);
}
void pushdown(int x){
if(t[x].rev){
if(lson)REV(lson);
if(rson)REV(rson);
t[x].rev=0;
}
if(lson)t[lson].val=(1ll*t[lson].val*t[x].mul+t[x].plu)%mod,t[lson].plu=(1ll*t[lson].plu*t[x].mul+t[x].plu)%mod,t[lson].mul=(1ll*t[lson].mul*t[x].mul)%mod,t[lson].sum=(1ll*t[lson].sum*t[x].mul+1ll*t[x].plu*t[lson].sz)%mod;
if(rson)t[rson].val=(1ll*t[rson].val*t[x].mul+t[x].plu)%mod,t[rson].plu=(1ll*t[rson].plu*t[x].mul+t[x].plu)%mod,t[rson].mul=(1ll*t[rson].mul*t[x].mul)%mod,t[rson].sum=(1ll*t[rson].sum*t[x].mul+1ll*t[x].plu*t[rson].sz)%mod;
t[x].plu=0,t[x].mul=1;
}
void pushup(int x){
t[x].sum=(t[lson].sum+t[rson].sum+t[x].val)%mod;
t[x].sz=t[lson].sz+t[rson].sz+1;
}
void rotate(int x){
int y=t[x].fa;
int z=t[y].fa;
int dirx=identify(x);
int diry=identify(y);
int b=t[x].ch[!dirx];
if(diry!=-1)t[z].ch[diry]=x;t[x].fa=z;
if(b)t[b].fa=y;t[y].ch[dirx]=b;
t[x].ch[!dirx]=y,t[y].fa=x;
pushup(y),pushup(x);
}
void pushall(int x){
if(identify(x)!=-1)pushall(t[x].fa);
pushdown(x);
}
void splay(int x){
pushall(x);
while(identify(x)!=-1){
int fa=t[x].fa;
if(identify(fa)==-1)rotate(x);
else if(identify(fa)==identify(x))rotate(fa),rotate(x);
else rotate(x),rotate(x);
}
}
void access(int x){
for(int y=0;x;x=t[y=x].fa)splay(x),rson=y,pushup(x);
}
void makeroot(int x){
access(x),splay(x),REV(x);
}
int findroot(int x){
access(x),splay(x);
pushdown(x);
while(lson)x=lson,pushdown(x);
splay(x);
return x;
}
void link(int x,int y){
makeroot(x),t[x].fa=y;
}
int split(int x,int y){
makeroot(x),access(y),splay(y);
return t[y].sum;
}
void cut(int x,int y){
split(x,y),t[x].fa=t[y].ch[0]=0,pushup(y);
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)t[i].val=t[i].mul=t[i].sum=1;
for(int i=1,x,y;i<n;i++)scanf("%d%d",&x,&y),link(x,y);
for(int i=1,t1,t2,t3,t4;i<=q;i++){
char s[10];
scanf("%s",s);
if(s[0]=='+')scanf("%d%d%d",&t1,&t2,&t3),split(t1,t2),t[t2].plu=(t[t2].plu+t3)%mod,t[t2].val=(t[t2].val+t3)%mod,t[t2].sum=(1ll*t[t2].sz*t3+t[t2].sum)%mod,pushup(t2);
if(s[0]=='-')scanf("%d%d%d%d",&t1,&t2,&t3,&t4),cut(t1,t2),link(t3,t4);
if(s[0]=='*')scanf("%d%d%d",&t1,&t2,&t3),split(t1,t2),t[t2].mul=(1ll*t[t2].mul*t3)%mod,t[t2].val=(1ll*t[t2].val*t3)%mod,t[t2].sum=(1ll*t[t2].sum*t3)%mod,pushup(t2);
if(s[0]=='/')scanf("%d%d",&t1,&t2),printf("%d\n",split(t1,t2));
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?