The Preliminary Contest for ICPC Asia Shanghai 2019 A. Lightning Routing I
因为某些原因,所以我就去学了 $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的更多相关文章
- 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/ ...
- The Preliminary Contest for ICPC Asia Shanghai 2019
传送门 B. Light bulbs 题意: 起初\(n\)个位置状态为\(0\),\(m\)次操作,每次操作更换区间状态:\(0\)到\(1\),\(1\)到\(0\). 共有\(T,T\leq 1 ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 C. Triple
[传送门] FFT第三题! 其实就是要求有多少三元组满足两短边之和大于等于第三边. 考虑容斥,就是枚举最长边,另外两个数组里有多少对边之和比它小,然后就是 $n^3$ 减去这个答案. 当 $n \le ...
- 01背包方案数(变种题)Stone game--The Preliminary Contest for ICPC Asia Shanghai 2019
题意:https://nanti.jisuanke.com/t/41420 给你n个石子的重量,要求满足(Sum<=2*sum<=Sum+min)的方案数,min是你手里的最小值. 思路: ...
- 给定进制下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 ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 L. Digit sum
题目:https://nanti.jisuanke.com/t/41422 思路:预处理 #include<bits/stdc++.h> using namespace std; ][]= ...
- 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 ...
- 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补 ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 B. Light bulbs
题目:https://nanti.jisuanke.com/t/41399 思路:差分数组 区间内操作次数为奇数次则灯为打开状态 #include<bits/stdc++.h> using ...
随机推荐
- 关于多个py文件生成一个可运行exe文件(用pyinstaller)
首先下载Pyinstaller,在cmd命令下执行:pip installer Pyinstaller,不需要关心安装在哪 然后把所有相关的py文件都放在一个目录下 在那个目录下执行cmd命令:pyi ...
- 使用asio进行异步下载http
下面是官方demo, 给人耳目一新的感觉,以前是总是把c++当成有类功能的C,看完这个感觉不用自己造轮子了,还是要跟上时代的步伐 // // async_client.cpp // ~~~~~~~~~ ...
- 一、基础篇--1.1Java基础-什么是java的序列化和反序列化
什么是序列化和反序列化 序列化:把堆内存中的java对象数据,通过某种方式把对象存储到磁盘文件或者传递给其他网络节点.这个过程称为序列化.简单来说呢,就是将对象转换成二进流的过程. 反序列化:把磁盘 ...
- 算法 - 剪枝游戏 - Green Hackenbush
场景:给颗树,轮流剪掉一条枝,没枝可剪的人输. 题目:Deforestation | HackerRank 讲解:Games!: Green Hackenbush 哎,差点自己想出来答案,最后还是看了 ...
- pandas中根据列的值选取多行数据
# 选取等于某些值的行记录 用 == df.loc[df['column_name'] == some_value] # 选取某列是否是某一类型的数值 用 isin df.loc[df['column ...
- emqtt emq 的主题访问控制 acl.conf
访问控制(ACL) EMQ 消息服务器通过 ACL(Access Control List) 实现 MQTT 客户端访问控制. ACL 访问控制规则定义: 允许(Allow)|拒绝(Deny) 谁(W ...
- Keepalived + LVS-NAT 实现高可用四层 TCP/UDP 负载均衡器
目录 文章目录 目录 前文列表 在 LVS1/2 安装 Keepalived & LVS Keepalived + LVS-NAT 实现 TCP 负载均衡 IP 规划 网络架构参考 LVS1 ...
- 阶段3 2.Spring_09.JdbcTemplate的基本使用_2 JdbcTemplate的概述和入门
先看这张图 1.spring中的JdbcTemplate JdbcTemplate的作用: 它就是用于和数据库交互的,实现对表的CRUD操作 如何创建该对象: ...
- JPA访问数据库的几种方式
JPA访问数据库的几种方式 本文为原创,转载请注明出处:https://www.cnblogs.com/supiaopiao/p/10901793.html 1. Repository 1.1. 通过 ...
- 解密Go语言之 pprof
相信很多人都听过“雷神 3”关于性能优化的故事.在一个 3D 游戏引擎的源码里,John Carmack 将 1/sqrt(x) 这个函数的执行效率优化到了极致. 一般我们使用二分法,或者牛顿迭代法计 ...