题单: 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的更多相关文章

  1. 一堆LCT板子

    搞了一上午LCT,真是累死了-- 以前总觉得LCT高大上不好学不好打,今天打了几遍感觉还可以嘛= =反正现在的水平应付不太难的LCT题也够用了,就这样好了,接下来专心搞网络流. 话说以前一直YY不出来 ...

  2. 动态树之LCT(link-cut tree)讲解

    动态树是一类要求维护森林的连通性的题的总称,这类问题要求维护某个点到根的某些数据,支持树的切分,合并,以及对子树的某些操作.其中解决这一问题的某些简化版(不包括对子树的操作)的基础数据结构就是LCT( ...

  3. 在此为LCT开一个永久的坑

    其实我连splay都还不怎么会. 今天先抄了黄学长的bzoj2049,以后一定要把它理解了. 写LCT怎么能不%数据结构大神yeweining呢?%%%chrysanthemums  %%%切掉大森林 ...

  4. 【BZOJ2157】旅游 LCT

    模板T,SB的DMoon..其实样例也是中国好样例...一开始不会复制,yangyang:找到“sample input”按住shift,按page down.... #include <ios ...

  5. 【BZOJ3669】[Noi2014]魔法森林 LCT

    终于不是裸的LCT了...然而一开始一眼看上去这是kruskal..不对,题目要求1->n的路径上的每个点的两个最大权值和最小,这样便可以用LCT来维护一个最小生成路(瞎编的...),先以a为关 ...

  6. 【BZOJ1180】: [CROATIAN2009]OTOCI & 2843: 极地旅行社 LCT

    竟然卡了我....忘记在push_down先下传父亲的信息了....还有splay里for():卡了我10min,但是双倍经验还是挺爽的,什么都不用改. 感觉做的全是模板题,太水啦,不能这么水了... ...

  7. 【BZOJ3282】Tree LCT

    1A爽,感觉又对指针重怀信心了呢= =,模板题,注意单点修改时splay就好,其实按吾本意是没写的也A了,不过应该加上能更好维护平衡性. ..还是得加上好= = #include <iostre ...

  8. BZOJ2888 资源运输(LCT启发式合并)

    这道题目太神啦! 我们考虑他的每一次合并操作,为了维护两棵树合并后树的重心,我们只好一个一个的把节点加进去.那么这样一来看上去似乎就是一次操作O(nlogn),但是我们拥有数据结构的合并利器--启发式 ...

  9. LCT裸题泛做

    ①洞穴勘测 bzoj2049 题意:由若干个操作,每次加入/删除两点间的一条边,询问某两点是否连通.保证任意时刻图都是一个森林.(两点之间至多只有一条路径) 这就是个link+cut+find roo ...

  10. 链剖&LCT总结

    在搞LCT之前,我们不妨再看看喜闻乐见的树链剖分. 树链剖分有一道喜闻乐见的例题:NOI2015 软件包管理器 如果你看懂题目了,你就会明白它是叫你维护一个树,这棵树是不会动的,要兹磁子树求和,子树修 ...

随机推荐

  1. frp 用于内网穿透的基本配置和使用

    frp 用于内网穿透的基本配置和使用 今天是端午节,先祝端午安康! frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP.UDP.HTTP.HTTPS 等多种协议.可以将内网服务以安全.便 ...

  2. 【摸鱼神器】UI库秒变LowCode工具——列表篇(二)维护json的小工具

    上一篇介绍了一下如何实现一个可以依赖 json 渲染的列表控件,既然需要 json 文件,那么要如何维护这个 json 文件就成了重点,如果没有好的维护方案的话,那么还不如直接用UI库. 所以需要我们 ...

  3. 腾讯QQ快捷登陆

    腾讯QQ快捷 相关各语言对接qq快捷登录教程 [C#]QQ开放平台(QQ站外登录)_流程和源码示例 j2ee中实现QQ第三方登陆 web实现QQ第三方登录 asp.net网站接入QQ登录 php实现q ...

  4. Volcano社区v1.6.0版本正式发布

    摘要:Volcano社区v1.6.0版本正式发布.此次版本增加了弹性作业管理.基于真实负载的动态调度. 基于真实负载的重调度.Volcano Job插件--MPI等多个新特性. 本文分享自华为云社区& ...

  5. 1.1 操作系统的第一个功能——虚拟化硬件资源 -《zobolの操作系统学习札记》

    1.1 操作系统的第一个功能--虚拟化硬件资源 目录 1.1 操作系统的第一个功能--虚拟化硬件资源 问1:操作系统一般处于计算机系统的哪一个位置? 问2:管理硬件资源为什么要单独交给操作系统? 问3 ...

  6. 领导:谁再用redis过期监听实现关闭订单,立马滚蛋!

    日前拜读阿牛老师的大作 领导:谁再用定时任务实现关闭订单,立马滚蛋! 发现其方案有若干瑕疵,特此抛砖引玉讨论一二. 在电商.支付等领域,往往会有这样的场景,用户下单后放弃支付了,那这笔订单会在指定的时 ...

  7. 新上线!3D单模型轻量化硬核升级,G级数据轻松拿捏!

    "3D模型体量过大.面数过多.传输展示困难",用户面对这样的3D数据,一定不由得皱起眉头.更便捷.快速处理三维数据,是每个3D用户对高效工作的向往. 在老子云最新上线的单模型轻量化 ...

  8. python-将print内容保存到文件

    通过sys.stdout得到print输出的内容,再进行保存 import sys class Logger(object): def __init__(self, file_path: str = ...

  9. 封装一个基础的vue-router

    前言主要知识点: 路由原理 Hash与History 实现路由 一.一个vue路由的工作原理前端路由与后端路由的区别: 后端路由:输入url>请求发送到服务器>服务器解析请求的路径> ...

  10. MySQL数据检索时,sql查询的结果如何加上序号

    1.sql语法 @i:类型java定义的变量 @i:=0:这里类似给i初始化值为0 @i:=@i+1 :每次从0开始递增+1 SELECT (@i:=@i+1) as id,TDLINE FROM Y ...