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 ...
随机推荐
- Python3 获取一大段文本之间两个关键字之间的内容
用re或者string.find.以下是re代码 123456789101112131415import re#文本所在TXT文件file = '123.txt' #关键字1,2(修改引号间的内容)w ...
- Horizon7.9部署和克隆问题汇总
1 基础环境说明 采用Windows server +SQL Server 2014进行部署,对接现有环境中的AD预控,系统版本为Windows server .桌面虚拟化软件版本采用Horizon ...
- Win10上安装Awvs 12原版程序和完美破解补丁详细步骤
环境: Win10 Awvs12安装包 链接:https://pan.baidu.com/s/1FIwYHIEKfLf4XAyeXfhVnA 提取码:6sa8 复制这段内容后打开百度网盘手机App,操 ...
- hdu 4763 看毛片(单纯next数组的应用--纯正O(n))
因为需要负责队内的字符串题,开始刷,做到这道,开始想不出来,上网找题解, 然后就惊了,为什么你们这么暴力都可以过的啊,1e6啊,后来又想了下会做了 贴下代码 #include <iostream ...
- JAVA使用easyexcel操作Excel
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本 ...
- HTMLHint 配置文件
HTMLHint 工具可以对 HTML 代码做静态代码检查,从而保证 HTML 代码的规范和质量.HTMLHint 工具内置 23 条规则,建议在 .htmlhintrc 配置文件中将规则尽可能都打开 ...
- C++入门经典-例2.2-使用格式输出函数printf
1:使用printf函数对不同类型变量进行输出,%符号,代表输出类型,\n代表换行,代码如下: // 2.2.cpp : 定义控制台应用程序的入口点. // #include "stdafx ...
- 第十四周课程总结 & 实验报告
一.JDBC JDBC概述 JDBC提供了一种与平台无关的用于执行SQL语句的标准JavaAPI,可以方便的实现多种关系型数据库的统一操作,它由一组用Java语言编写的类和接口组成 JDBC的主要操作 ...
- 八、MD5加密并封装,并调用封装方法
一.MD5加密 封装Md5 public class Md5 { //十六进制下数字到字符的映射数组 private static final char hexDigits[] = { '0', '1 ...
- hibernate注意
1.设置id <id name="ID"> <!-- ????????????????? --> <generator class="ide ...