题单: 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. MongoDB 体系结构与数据模型

    每日一句 If no one else guards the world, then I will come forward. 如果没有别人保卫这个世界,那么我将挺身而出. 概述 MongoDB主要是 ...

  2. SpringSecurity的 loginProcessingUrl为什么不能用

    前情提要: 我在做一个springsecurity动态鉴权的项目时, 据网上说配置了 loginProcessingUrl("/login1"); 以后 就可以自定义login的请 ...

  3. Spring-boot整合Activiti7

    Spring-boot整合Activiti7 pom.xml    <properties>        <maven.compiler.source>15</mave ...

  4. Java - 四种引用类型及应用场景

    1. 强引用 new 一个对象的时候,就是强引用 Object object = new Object(); 只要强引用存在,垃圾回收就不会回收该对象,内存不足时会抛出OOM. 2. 软引用 定义:非 ...

  5. Linux Cgroup v1(中文翻译)(3):CPU Accounting Controller

    英文原文: https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v1/cpuacct.html CPU Accounting Contr ...

  6. C++ 炼气期之变量的生命周期和作用域

    1. 前言 什么是变量的生命周期? 从变量被分配空间到空间被收回的这一个时间段,称为变量的生命周期. 什么是变量的作用域? 在变量的生命周期内,其存储的数据并不是在任何地方都能使用,变量能使用的范围, ...

  7. 论文解读(GraphMAE)《GraphMAE: Self-Supervised Masked Graph Autoencoders》

    论文信息 论文标题:GraphMAE: Self-Supervised Masked Graph Autoencoders论文作者:Zhenyu Hou, Xiao Liu, Yukuo Cen, Y ...

  8. 【SpringBoot】YAML 配置文件

    博客主页:准Java全栈开发工程师 00年出生,即将进入职场闯荡,目标赚钱,可能会有人觉得我格局小.觉得俗,但不得不承认这个世界已经不再是以一条线来分割的平面,而是围绕财富旋转的球面,成为有钱人不是为 ...

  9. 文件上传漏洞靶场分析 UPLOAD_LABS

    文件上传漏洞靶场(作者前言) 文件上传漏洞 产生原理 PASS 1) function checkFile() { var file = document.getElementsByName('upl ...

  10. BUUCTF-来首歌吧

    来首歌吧 歌曲题目一般就是整个摩斯电码 看上面的样子应该就是摩斯电码解密一下 ..... -... -.-. ----. ..--- ..... -.... ....- ----. -.-. -... ...