传送门

因为某些原因,所以我就去学了 $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 ;
}

The Preliminary Contest for ICPC Asia Shanghai 2019 A. Lightning Routing I的更多相关文章

  1. The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)

    The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力) 传送门:https://nanti.jisuanke.com/ ...

  2. The Preliminary Contest for ICPC Asia Shanghai 2019

    传送门 B. Light bulbs 题意: 起初\(n\)个位置状态为\(0\),\(m\)次操作,每次操作更换区间状态:\(0\)到\(1\),\(1\)到\(0\). 共有\(T,T\leq 1 ...

  3. The Preliminary Contest for ICPC Asia Shanghai 2019 C. Triple

    [传送门] FFT第三题! 其实就是要求有多少三元组满足两短边之和大于等于第三边. 考虑容斥,就是枚举最长边,另外两个数组里有多少对边之和比它小,然后就是 $n^3$ 减去这个答案. 当 $n \le ...

  4. 01背包方案数(变种题)Stone game--The Preliminary Contest for ICPC Asia Shanghai 2019

    题意:https://nanti.jisuanke.com/t/41420 给你n个石子的重量,要求满足(Sum<=2*sum<=Sum+min)的方案数,min是你手里的最小值. 思路: ...

  5. 给定进制下1-n每一位数的共享(Digit sum)The Preliminary Contest for ICPC Asia Shanghai 2019

    题意:https://nanti.jisuanke.com/t/41422 对每一位进行找循环节规律就行了. #define IOS ios_base::sync_with_stdio(0); cin ...

  6. The Preliminary Contest for ICPC Asia Shanghai 2019 L. Digit sum

    题目:https://nanti.jisuanke.com/t/41422 思路:预处理 #include<bits/stdc++.h> using namespace std; ][]= ...

  7. The Preliminary Contest for ICPC Asia Shanghai 2019 J. Stone game

    题目:https://nanti.jisuanke.com/t/41420 思路:当a(a∈S′)为最小值 如果Sum(S′)−a≤Sum(S−S′)成立 那么(∀t∈S′,Sum(S′)−t≤Sum ...

  8. The Preliminary Contest for ICPC Asia Shanghai 2019 D. Counting Sequences I

    题目:https://nanti.jisuanke.com/t/41412思路:dfs           先取ai>2  2^12>3000 因此至多取11个 其余用1补        ...

  9. The Preliminary Contest for ICPC Asia Shanghai 2019 B. Light bulbs

    题目:https://nanti.jisuanke.com/t/41399 思路:差分数组 区间内操作次数为奇数次则灯为打开状态 #include<bits/stdc++.h> using ...

随机推荐

  1. Linux命令-文本编辑(一)

    Linux命令-文本编辑(一) Linux col命令 Linux col命令用于过滤控制字符. 在许多UNIX说明文件里,都有RLF控制字符.当我们运用shell特殊字符">&quo ...

  2. [NLP] The Annotated Transformer 代码修正

    1. RuntimeError: "exp" not implemented for 'torch.LongTensor' class PositionalEncoding(nn. ...

  3. 用JavaScript实现div的鼠标拖拽效果

    实现原理鼠标按下时根据onmousemove事件来动态获取鼠标坐标位置以此来更新div的位置,实现的前提时div要有一个定位效果,不然的话是移动不了它的. HTML <div class=&qu ...

  4. echarts之bootstrap选项卡不能显示其他标签echarts图表

    在echarts跟bootstrap选项卡整合的时候,默认第一个选中选项卡可以正常加载echarts图表,但是切换其他选项的时候不能渲染出其他选项卡echarts图表. 解决方法: 在js中添加代码: ...

  5. Java-Thread 线程

    一.进程与线程的概念 进程和线程都是一个CPU工作时间段的描述,只是关注点不同. 进程(Process): 资源(CPU,内存等,文件,网络等)分配的基本单位.系统中有很多进程,它们都会使用内存.为了 ...

  6. Servlet请求参数的方式

    今天整理了以下几种常用的Servlet请求参数的方式,下面简单地介绍 1)getParameter(String key)返回一个字符串,获得name和key 一样的表单控件的数据,如果有重复的nam ...

  7. leetcode 140 单词拆分2 word break II

    单词拆分2,递归+dp, 需要使用递归,同时使用记忆化搜索保存下来结果,c++代码如下 class Solution { public: //定义一个子串和子串拆分(如果有的话)的映射 unorder ...

  8. Android共享元素场景切换动画的实现

    安卓5.0系统引入了共享元素,能做出非常炫酷的场景切换效果,这让人非常兴奋同时非常蛋疼,因为低版本没法使用啊,所以今天就跟大家分享一下自己写的一个库,其实只有2个文件而已就可以兼容安卓5.0以下的版本 ...

  9. PowerDesigner 入门使用

    <转载于--https://www.cnblogs.com/biehongli/p/6025954.html> PowerDesigner最基础的使用方法入门学习   1:入门级使用Pow ...

  10. JavaScript日常学习6

    JavaScript的运算符.比较符.条件语句.循环语句.跳出循环(break.continue).标签 JavaScript的运算符.比较符.条件语句.循环语句.跳出循环(break.continu ...