总结-LCT
题单: https://www.zybuluo.com/xzyxzy/note/1027479
LuoguP3203 [HNOI2010]弹飞绵羊
动态加边,删边
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define ll long long
#define u32 unsigned int
#define u64 unsigned long long
//#define ON_DEBUGG
#ifdef ON_DEBUGG
#define D_e_Line printf("\n----------\n")
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#include <ctime>
#define TIME() fprintf(stderr, "\ntime: %.3fms\n", clock() * 1000.0 / CLOCKS_PER_SEC)
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
//char buf[1 << 21], *p1 = buf, *p2 = buf;
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
#endif
using namespace std;
struct ios{
template<typename ATP>inline ios& operator >> (ATP &x){
x = 0; int f = 1; char ch;
for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
x *= f;
return *this;
}
}io;
template<typename ATP>inline ATP Max(ATP a, ATP b){
return a > b ? a : b;
}
template<typename ATP>inline ATP Min(ATP a, ATP b){
return a < b ? a : b;
}
template<typename ATP>inline ATP Abs(ATP a){
return a < 0 ? -a : a;
}
const int N = 200007;
int n;
int val[N];
struct Tree{
int ch[2], fa, val, siz;
bool rev;
}t[N];
#define ls t[u].ch[0]
#define rs t[u].ch[1]
#define root (n + 1)
inline bool Ident(int u) {
return u == t[t[u].fa].ch[1];
}
inline bool IsRoot(int u) {
return t[t[u].fa].ch[0] != u && t[t[u].fa].ch[1] != u;
}
inline void Pushup(int u) {
t[u].siz = t[ls].siz + t[rs].siz + 1;
}
inline void Pushrev(int u) {
Swap(ls, rs);
t[u].rev ^= 1;
}
inline void Pushdown(int u) {
if(!t[u].rev) return;
if(ls) Pushrev(ls);
if(rs) Pushrev(rs);
t[u].rev = 0;
}
int sta[N], top;
inline void Rotate(int x) {
int y = t[x].fa, z = t[y].fa, k = Ident(x);
t[x].fa = z; if(!IsRoot(y)) t[z].ch[Ident(y)] = x;
t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y, t[y].fa = x;
Pushup(y), Pushup(x);
}
inline void Splay(int u){
int x = u;
while(!IsRoot(u)){
sta[++top] = u;
u = t[u].fa;
}
sta[++top] = u;
while(top) Pushdown(sta[top--]);
while(!IsRoot(x)) {
int y = t[x].fa;
if(!IsRoot(y)){
Ident(x) == Ident(y) ? Rotate(y) : Rotate(x);
}
Rotate(x);
}
Pushup(x);
}
inline void Access(int u) {
for(register int v = 0; u; v = u, u = t[u].fa){
Splay(u);
t[u].ch[1] = v;
Pushup(u);
}
}
inline void MakeRoot(int u) {
Access(u);
Splay(u);
Pushrev(u);
}
inline int FindRoot(int u) {
Access(u);
Splay(u);
while(ls) u = ls;
Splay(u);
return u;
}
inline void Split(int u, int v) {
MakeRoot(u);
Access(v);
Splay(v);
}
inline void Link(int u, int v) {
Split(u, v);
t[u].fa = v;
}
inline void Cut(int u, int v) {
MakeRoot(u);
if(FindRoot(v) == u && t[v].fa == u && !t[v].ch[0]){
t[v].fa = t[u].ch[1] = 0;
Pushup(u);
}
}
inline void Modify(int u, int w) {
// Access(u); // no access
Splay(u);
t[u].val = w;
Pushup(u);
}
inline int Query(int u) {
MakeRoot(u);
Access(root);
Splay(root);
return t[root].siz - 1;
}
int main(){
FileOpen();
FileSave();
int m;
io >> n;
R(i,1,n){
io >> val[i];
if(i + val[i] <= n)
Link(i, i + val[i]);
else
Link(i, root);
}
io >> m;
while(m--){
int opt;
io >> opt;
if(opt == 1){
int x;
io >> x;
++x;
printf("%d\n", Query(x));
}
else{
int x, w;
io >> x >> w;
++x;
Cut(x, x + val[x] <= n ? x + val[x] : root);
Modify(x, w);
Link(x, x + w <= n ? x + w : root);
val[x] = w;
}
}
TIME();
return 0;
}
LuoguP2387 [NOI2014]魔法森林
边权拆点两端,一维偏序排序,二维偏序类似\(Kruskal\)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define ll long long
#define u32 unsigned int
#define u64 unsigned long long
#define ON_DEBUGG
#ifdef ON_DEBUGG
#define D_e_Line printf("\n----------\n")
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#include <ctime>
#define TIME() fprintf(stderr, "\ntime: %.3fms\n", clock() * 1000.0 / CLOCKS_PER_SEC)
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
//char buf[1 << 21], *p1 = buf, *p2 = buf;
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
#endif
using namespace std;
struct ios{
template<typename ATP>inline ios& operator >> (ATP &x){
x = 0; int f = 1; char ch;
for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
x *= f;
return *this;
}
}io;
template<typename ATP>inline ATP Max(ATP a, ATP b){
return a > b ? a : b;
}
template<typename ATP>inline ATP Min(ATP a, ATP b){
return a < b ? a : b;
}
template<typename ATP>inline ATP Abs(ATP a){
return a < 0 ? -a : a;
}
const int N = 5e4 + 7;
const int M = 1e5 + 7;
struct Node{
int x, y, valA, valB;
bool operator < (const Node &com) const{
if(valA != com.valA) return valA < com.valA;
return valB < com.valB;
}
}a[M];
int fa[N];
inline int Find(int x) {
return x == fa[x] ? x : fa[x] = Find(fa[x]);
}
struct Tree{
int ch[2], fa, val, mx; // mx : maxPos
bool rev;
}t[N * 3];
#define ls t[u].ch[0]
#define rs t[u].ch[1]
inline int Ident(int u) {
return t[t[u].fa].ch[1] == u;
}
inline bool IsRoot(int u) {
return t[t[u].fa].ch[0] != u && t[t[u].fa].ch[1] != u;
}
inline void Pushup(int u) {
t[u].mx = u;
if(ls && t[t[ls].mx].val > t[t[u].mx].val) t[u].mx = t[ls].mx;
if(rs && t[t[rs].mx].val > t[t[u].mx].val) t[u].mx = t[rs].mx;
}
inline void Pushrev(int u) {
Swap(ls, rs);
t[u].rev ^= 1;
}
inline void Pushdown(int u) {
if(!t[u].rev) return;
if(ls) Pushrev(ls);
if(rs) Pushrev(rs);
t[u].rev = 0;
}
inline void Rotate(int x) {
int y = t[x].fa, z = t[y].fa, k = Ident(x);
t[x].fa = z; if(!IsRoot(y)) t[z].ch[Ident(y)] = x;
t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y, t[y].fa = x;
Pushup(y), Pushup(x);
}
int sta[N], top;
inline void Splay(int u) {
int x = u;
while(!IsRoot(u)){
sta[++top] = u;
u = t[u].fa; //
}
sta[++top] = u;
while(top) Pushdown(sta[top--]);
while(!IsRoot(x)){
int y = t[x].fa;
if(!IsRoot(y)){
Ident(x) == Ident(y) ? Rotate(y) : Rotate(x);
}
Rotate(x);
}
Pushup(x);
}
inline void Access(int u) {
for(register int v = 0; u; v = u, u = t[u].fa){ //
Splay(u);
t[u].ch[1] = v;
Pushup(u);
}
}
inline void MakeRoot(int u) {
Access(u);
Splay(u);
Pushrev(u);
}
inline void Split(int u, int v) {
MakeRoot(u);
Access(v);
Splay(v);
}
inline void Link(int u, int v) {
Split(u, v);
t[u].fa = v;
}
inline void Cut(int u, int v) {
Split(u, v);
t[u].fa = t[v].ch[1] = 0;
Pushup(v);
}
int main() {
int n, m;
io >> n >> m;
int tot = 0;
R(i,1,m){
int u, v, A, B;
io >> u >> v >> A >> B;
if(u == v) continue;
a[++tot] = (Node){ u, v, A, B};
}
m = tot;
sort(a + 1, a + m + 1);
R(i,1,n) fa[i] = i; //
R(i,n,n + m) t[i].val = a[i - n].valB;
int ans = 0x7fffffff;
R(i,1,m){
int u = a[i].x, v = a[i].y;
int p = Find(u), q = Find(v);
if(p == q){
Split(u, v);
int j = t[v].mx - n;
if(a[j].valB <= a[i].valB) continue;
Cut(a[j].x, j + n), Cut(a[j].y, j + n);
Link(u, i + n), Link(v, i + n);
}
else{
fa[p] = q;
Link(u, i + n), Link(v, i + n);
}
if(Find(1) == Find(n)){
Split(1, n);
int j = t[n].mx - n;
ans = Min(ans, a[i].valA + a[j].valB);
}
}
printf("%d", ans == 0x7fffffff ? -1 : ans);
return 0;
}
LuoguP4234 最小差值生成树
“亲爱的边,你喜欢LCT维护吗”
“不喜欢呀”
“那你就tmd给我变成点吧”
“呜呜呜”
“排了你,从小往大单调着走”
“嘤嘤嘤”
“然后就是最小生成树,LCT版本的呢”
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define ON_DEBUGG
#ifdef ON_DEBUGG
#define D_e_Line printf("\n-----------\n")
#define D_e(x) std::cout << (#x) << " : " <<x << "\n"
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define Pause() system("pause")
#include <ctime>
#define TIME() fprintf(stderr, "\nTIME : %.3lfms\n", clock() * 1000.0 / CLOCKS_PER_SEC)
#else
#define D_e_Line ;
#define D_e(x) ;
#define FileOpen() ;
#define FilSave ;
#define Pause() ;
#define TIME() ;
#endif
struct ios {
template<typename ATP> ios& operator >> (ATP &x) {
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x *= f;
return *this;
}
}io;
using namespace std;
template<typename ATP> inline ATP Min(ATP a, ATP b) {
return a < b ? a : b;
}
template<typename ATP> inline ATP Max(ATP a, ATP b) {
return a > b ? a : b;
}
template<typename ATP> inline ATP Abs(ATP a) {
return a < 0 ? -a : a;
}
const int N = 250007;
int n;
struct E {
int x, y, w;
bool operator < (const E &com) const {
return w < com.w;
}
} e[N];
struct Tree {
int ch[2], fa, id;
bool rev;
} t[N];
#define ls t[u].ch[0]
#define rs t[u].ch[1]
inline int Ident(int u) {
return t[t[u].fa].ch[1] == u;
}
inline bool IsRoot(int u) {
return t[t[u].fa].ch[1] != u && t[t[u].fa].ch[0] != u;
}
inline void Pushup(int u) {
t[u].id = u;
if(t[ls].id > n && (t[u].id <= n || t[u].id > t[ls].id)) t[u].id = t[ls].id;
if(t[rs].id > n && (t[u].id <= n || t[u].id > t[rs].id)) t[u].id = t[rs].id;
}
inline void Pushrev(int u) {
Swap(ls, rs);
t[u].rev ^= 1;
}
inline void Pushdown(int u) {
if(!t[u].rev) return;
if(ls) Pushrev(ls);
if(rs) Pushrev(rs);
t[u].rev = 0;
}
inline void Rotate(int x) {
int y = t[x].fa, z = t[y].fa, k = Ident(x);
t[x].fa = z; if(!IsRoot(y)) t[z].ch[Ident(y)] = x;
t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y, t[y].fa = x;
Pushup(y), Pushup(x);
}
int sta[N], top;
inline void Splay(int u) {
int x = u;
while(!IsRoot(u)){ //
sta[++top] = u;
u = t[u].fa;
}
sta[++top] = u;
while(top) Pushdown(sta[top--]);
while(!IsRoot(x)){
int y = t[x].fa;
if(!IsRoot(y)){
Ident(x) == Ident(y) ? Rotate(y) : Rotate(x);
}
Rotate(x);
}
Pushup(x);
}
inline void Access(int u) {
for(register int v = 0; u; v = u, u = t[u].fa){
Splay(u);
t[u].ch[1] = v;
Pushup(u);
}
}
inline void MakeRoot(int u) {
Access(u);
Splay(u);
Pushrev(u);
}
inline int FindRoot(int u) {
Access(u);
Splay(u);
while(ls) u = ls;
Splay(u);
return u;
}
inline void Split(int u, int v) {
MakeRoot(u);
Access(v);
Splay(v);
}
inline void Link(int u, int v) {
Split(u, v);
t[u].fa = v;
}
inline bool Check(int x, int y) {
MakeRoot(x);
return FindRoot(y) != x;
}
int vis[N];
int main() {
int m;
io >> n >> m;
int idx = n, ans = 0x7fffffff;
R(i,1,m){
io >> e[i].x >> e[i].y >> e[i].w;
}
sort(e + 1, e + m + 1);
int tot = 1, l = 1;
R(i,1,m){
++idx;
int x = e[i].x, y = e[i].y;
if(x == y){
vis[i] = 1;
continue;
}
if(Check(x, y)){
Link(x, idx), Link(idx, y);
++tot;
}
else{
Split(x, y);
int root = t[y].id;
vis[root - n] = 1;
Splay(root);
t[t[root].ch[0]].fa = t[t[root].ch[1]].fa = 0;
Link(x, idx), Link(idx, y);
}
while(l < i && vis[l]) ++l;
if(tot >= n) ans = Min(ans, e[i].w - e[l].w);
}
printf("%d", ans);
return 0;
}
没看的了,咕了

总结-LCT的更多相关文章
- 一堆LCT板子
搞了一上午LCT,真是累死了-- 以前总觉得LCT高大上不好学不好打,今天打了几遍感觉还可以嘛= =反正现在的水平应付不太难的LCT题也够用了,就这样好了,接下来专心搞网络流. 话说以前一直YY不出来 ...
- 动态树之LCT(link-cut tree)讲解
动态树是一类要求维护森林的连通性的题的总称,这类问题要求维护某个点到根的某些数据,支持树的切分,合并,以及对子树的某些操作.其中解决这一问题的某些简化版(不包括对子树的操作)的基础数据结构就是LCT( ...
- 在此为LCT开一个永久的坑
其实我连splay都还不怎么会. 今天先抄了黄学长的bzoj2049,以后一定要把它理解了. 写LCT怎么能不%数据结构大神yeweining呢?%%%chrysanthemums %%%切掉大森林 ...
- 【BZOJ2157】旅游 LCT
模板T,SB的DMoon..其实样例也是中国好样例...一开始不会复制,yangyang:找到“sample input”按住shift,按page down.... #include <ios ...
- 【BZOJ3669】[Noi2014]魔法森林 LCT
终于不是裸的LCT了...然而一开始一眼看上去这是kruskal..不对,题目要求1->n的路径上的每个点的两个最大权值和最小,这样便可以用LCT来维护一个最小生成路(瞎编的...),先以a为关 ...
- 【BZOJ1180】: [CROATIAN2009]OTOCI & 2843: 极地旅行社 LCT
竟然卡了我....忘记在push_down先下传父亲的信息了....还有splay里for():卡了我10min,但是双倍经验还是挺爽的,什么都不用改. 感觉做的全是模板题,太水啦,不能这么水了... ...
- 【BZOJ3282】Tree LCT
1A爽,感觉又对指针重怀信心了呢= =,模板题,注意单点修改时splay就好,其实按吾本意是没写的也A了,不过应该加上能更好维护平衡性. ..还是得加上好= = #include <iostre ...
- BZOJ2888 资源运输(LCT启发式合并)
这道题目太神啦! 我们考虑他的每一次合并操作,为了维护两棵树合并后树的重心,我们只好一个一个的把节点加进去.那么这样一来看上去似乎就是一次操作O(nlogn),但是我们拥有数据结构的合并利器--启发式 ...
- LCT裸题泛做
①洞穴勘测 bzoj2049 题意:由若干个操作,每次加入/删除两点间的一条边,询问某两点是否连通.保证任意时刻图都是一个森林.(两点之间至多只有一条路径) 这就是个link+cut+find roo ...
- 链剖&LCT总结
在搞LCT之前,我们不妨再看看喜闻乐见的树链剖分. 树链剖分有一道喜闻乐见的例题:NOI2015 软件包管理器 如果你看懂题目了,你就会明白它是叫你维护一个树,这棵树是不会动的,要兹磁子树求和,子树修 ...
随机推荐
- Python模块Ⅱ
Python模块2 part3 模块的分类: 内置模块200种左右:python自带的模块,time os sys hashlib等 第三方模块6000种左右:需要pip install beauti ...
- 题解 P3831 [SHOI2012]回家的路
什么叫分层图最短路,我不会/kk 感觉自己做法和其他题解不大一样所以过来发篇题解了. 未刻意卡常拿下最优解 题目大意 就是说给你一个 \(n \times n\) 的网格图和 \(m\) 个可换乘点, ...
- C++:最大子数组差
最大子数组差 内存限制:128 MiB 时间限制:1000 ms 题目描述: 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B) ...
- React设置proxy后依旧报CROS错误
1.判断表单数据是否为后端接收的类型 POST GET2.axios自动转换问题 手动添加标头这份表单数据包括了files (二进制数据)而标头显示是JSON格式 不符 所以报CROS 更多文章请移步 ...
- Spring bean到底是如何创建的?(上)
前言 众所周知,spring对于java程序员来说是一个及其重要的后端框架,几乎所有的公司都会使用的框架,而且深受广大面试官的青睐.所以本文就以常见的一个面试题"spring bean的生命 ...
- Wget命令解释
Wget主要用于下载文件,在安装软件时会经常用到,以下对wget做简单说明. 1.下载单个文件:wget http://www.baidu.com.命令会直接在当前目录下载一个index.html的文 ...
- 【Java面试】请你简单说一下Mysql的事务隔离级别
一个工作了6年的粉丝,去阿里面试,在第一面的时候被问到"Mysql的事务隔离级别". 他竟然没有回答上来,一直在私信向我诉苦. 我说,你只能怪年轻时候的你,那个时候不够努力导致现在 ...
- go程序添加远程调用tcpdump功能
最近开发的telemetry采集系统上线了.听起来高大上,简单来说就是一个grpc/udp服务端,用户的机器(路由器.交换机)将它们的各种统计数据上报采集.整理后交后端的各类AI分析系统分析.目前华为 ...
- SAP IDOC-Segment E1EDP19 Document Item Object Identification
PO创建时,通过IDOC EDI 接口自动创建SO 案例. BD54 配置逻辑系统 SCC4 给集团分配逻辑系统 SM59 新建RFC 链接 WE21 创建IDOC 处理端口 we20 创建合作伙伴 ...
- Spring IOC 为什么能降低耦合
有同学在学习 Spring 框架中可能会问这样的问题,为什么通过依赖注入就可以降低代码间的耦合呢?我通过 new 生产对象不也可以吗,不就是一行代码的不同,一个是 @Resource 注入,一个是 n ...