传送门

因为某些原因,所以我就去学了 $LCT$ 维护直径, $LCT$ 维护直径我上一个博客讲得很详细了:传送门

这里维护虚儿子用的是 $multiset$ ,没写可删堆

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=8e5+;
const ll INF=1e18;
int n,m;
struct edge {
int x,y;
}e[N];
namespace LCT {
int c[N][],fa[N],sz[N],val[N];
ll sum[N],lmx[N],rmx[N],mxs[N];
bool rev[N];
multiset <ll> H[N],P[N];
inline void ins(int u,int v) { H[u].insert(lmx[v]); P[u].insert(mxs[v]); }
inline void del(int u,int v) { H[u].erase(H[u].find(lmx[v])); P[u].erase(P[u].find(mxs[v])); }
inline ll fir(multiset <ll> &S) { return S.size() ? *S.rbegin() : -INF; }
inline ll sec(multiset <ll> &S) { return S.size()> ? *(++S.rbegin()) : -INF; }
inline void pushup(int x)
{
int &lc=c[x][],&rc=c[x][];
sum[x]=sum[lc]+sum[rc]+val[x];
ll t=max(0ll,fir(H[x])), L=max(t,rmx[lc])+val[x], R=max(t,lmx[rc])+val[x];
lmx[x]=max(lmx[lc], sum[lc]+R ); rmx[x]=max(rmx[rc], sum[rc]+L );
mxs[x]=max( max( rmx[lc]+R , lmx[rc]+L ) , max(mxs[lc],mxs[rc]) );
mxs[x]=max(mxs[x],fir(P[x])); mxs[x]=max(mxs[x], t+val[x] );
mxs[x]=max(mxs[x], t+val[x]+sec(H[x]) );
}
inline void pushdown(int x)
{
if(!x||!rev[x]) return;
int &lc=c[x][],&rc=c[x][]; rev[x]=;
swap(lc,rc); swap(lmx[x],rmx[x]);
if(lc) rev[lc]^=;
if(rc) rev[rc]^=;
}
inline void rever(int x) { rev[x]=; pushdown(x); }
inline bool noroot(int x) { return (c[fa[x]][]==x)|(c[fa[x]][]==x); }
inline void rotate(int x)
{
int y=fa[x],z=fa[y],d=(c[y][]==x);
if(noroot(y)) c[z][c[z][]==y]=x;
fa[x]=z; fa[y]=x; fa[c[x][d^]]=y;
c[y][d]=c[x][d^]; c[x][d^]=y;
pushup(y);
}
void push_rev(int x)
{
if(noroot(x)) push_rev(fa[x]);
else pushdown(x);
pushdown(c[x][]); pushdown(c[x][]);
}
inline void splay(int x)
{
push_rev(x);
while(noroot(x))
{
int y=fa[x],z=fa[y];
if(noroot(y))
(c[y][]==x ^ c[z][]==y) ? rotate(x) : rotate(y);
rotate(x);
} pushup(x);
}
inline void access(int x)
{
for(int y=;x;y=x,x=fa[x])
{
splay(x); if(y) del(x,y);
if(c[x][]) ins(x,c[x][]);
c[x][]=y; pushup(x);
}
}
inline void makeroot(int x) { access(x); splay(x); rever(x); }
inline int findroot(int x)
{
access(x); splay(x); pushdown(x);
while(c[x][]) x=c[x][],pushdown(x);
splay(x); return x;
}
inline void split(int x,int y) { makeroot(x); access(y); splay(y); }
inline void link(int x,int y)
{
makeroot(x); if(findroot(y)==x) return;
makeroot(y); fa[x]=y; ins(y,x); pushup(y);
}
inline void cut(int x,int y)
{
makeroot(x);
if(findroot(y)!=x||fa[y]!=x||c[y][]) return;
fa[y]=c[x][]=; pushup(x);
}
inline void Link(int k) { link(e[k].x,n+k); link(e[k].y,n+k); }
inline void Cut(int k) { cut(e[k].x,n+k); cut(e[k].y,n+k); }
inline ll query(int x) { access(x); splay(x); return rmx[x]; }
}
int main()
{
n=read();
for(int i=;i<n;i++)
e[i].x=read(),e[i].y=read(),LCT::val[n+i]=read();
for(int i=;i<n;i++) LCT::Link(i);
m=read(); char s[]; int a;
for(int i=;i<=m;i++)
{
scanf("%s",s); a=read();
if(s[]=='Q') printf("%lld\n",LCT::query(a));
else LCT::Cut(a),LCT::val[n+a]=read(),LCT::Link(a);
}
return ;
}

最新文章

  1. 机器指令翻译成 JavaScript —— No.3 流程分割
  2. ASP.NET Aries JSAPI 文档说明:AR.Utility
  3. cms真实问题的来源以及模拟解决方案
  4. 高效 Java Web 开发框架 JessMA v3.2.3 正式发布
  5. Linux vi 操作命令整理
  6. Gym 100703K Word order 贪心
  7. 遍历HashMap的四种方法
  8. 框架模式 MVC 在Android中的使用
  9. Linux常用配置文件解析
  10. Python Numpy
  11. HDU4734(数位dp)
  12. Enum入门【原】
  13. tomcat下的Cookie特殊符号问题
  14. #个人博客作业Week2——关于代码规范的讨论
  15. 奇妙的CSS3—导航栏下划线跟随效果
  16. cookie,session,token
  17. Spring AOP前置通知实例讲解与AOP详细解析
  18. [原创]java WEB学习笔记04:Servlet 简介及第一个Servlet程序(配置注册servlet,生命周期)
  19. 【Spring MVC】 - @ModelAttribute使用
  20. JDBC连接池一 自定义连接池

热门文章

  1. 二级索引-phoenix-单机部署
  2. C++入门经典-例3.12-使用if-else语句实现根据输入的字符输出字符串
  3. 【学习】SpringBoot之自定义拦截器
  4. Maven报错,没有有效的生命周期
  5. 【转】diamond专题(四)—— 容灾机制
  6. hdfs、zookeepeer之HA模式
  7. HTML功能框架
  8. zeppelin安装使用
  9. React组件库集锦及学习视频
  10. hdu5747 Aaronson 贪心