很早就有人给我推荐的模版题,然后我最近才刷的(' '    ) 昨天的tree 不知道比他们高到哪里去了,我和他谈笑风生啊!

bzoj 2002 弹飞绵羊

重点:这道题的cut和link 由于这道题链的特殊性所以不能用提根的方法搞,可以注意到每一次cut位置一定是前面的一个元素,所以access 上去之后直接把左边的儿子丢掉就行了(我原来的cut 是在不知道两个点的儿子关系时就强行提根(' '    )) 然后link的时候直接把cut的那一棵splay接过去就行了

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = ;
struct node {
int size, p, data;
node* son[], *fa;
}tr[maxn]; void test(node* x) {
if(x) {
cout << x-> data <<" " << x-> size <<" "<< x-> p << endl;
for(int i = ; i < ; ++ i) test(x-> son[i]);
}
}
void update(node* x) {
x-> size = ;
for(int i = ; i < ; ++ i) if(x-> son[i]) x-> size += x-> son[i]-> size;
} void rotate(node* x, int f) {
node* y = x-> fa;
if(y-> fa) {
if(y-> fa-> son[] == y) y-> fa-> son[] = x;
else y-> fa-> son[] = x;
}
x-> fa = y-> fa, y-> fa = x, x-> p = y-> p, x-> size = y-> size;
y-> son[f] = x-> son[!f];
if(x->son[!f]) x-> son[!f]-> fa = y;
x-> son[!f] = y;
update(y);
} void splay(node* x, node* f) {
while(x-> fa != f) {
if(x-> fa-> fa == f) {
int a = x-> fa-> son[] == x ? : ;
rotate(x, a);
}
else {
node* y = x-> fa, *z = y-> fa;
int a = z-> son[] == y ? : ;
int b = y-> son[] == x ? : ;
if(a == b) rotate(y, a), rotate(x, b);
else rotate(x, b), rotate(x, a);
}
}
} void access(int cur) {
node* x = tr + cur; node* y; int pp;
splay(x, NULL);
if(x-> son[]) x-> son[]-> p = cur, x-> son[]-> fa = NULL, x-> son[] = NULL, update(x);
while((pp = x-> p)) {
y = tr + pp; splay(y, NULL);
if(y-> son[]) y-> son[]-> p = pp, y-> son[]-> fa = NULL, y-> son[] = NULL, update(y);
y-> son[] = x, x-> fa = y; update(y);
splay(x, NULL);
}
} int int_get() {
int x = ; char c = (char)getchar(); bool f = ;
while(!isdigit(c) && c != '-') c = (char)getchar();
if(c == '-') f = , c = (char)getchar();
while(isdigit(c)) {
x = x * + (int)(c - '');
c = (char)getchar();
}
if(f) x = -x;
return x;
} int n, m, px[maxn]; void read() {
n = int_get();
for(int i = ; i <= n; ++ i) {
int ne = int_get();
px[i] = ne + i > n ? : i + ne;
(tr + i)-> size = , (tr + i)-> p = px[i], (tr + i)-> data = i;
}
} void sov() {
int m = int_get();
while(m --) {
int op = int_get();
if(op == ) {
int x = int_get() + ; access(x); printf("%d\n", (tr + x)-> size);
}
else {
int a, b;
a = int_get() + , b = int_get();
access(a); node* x = tr + a;
if(px[a]) x-> son[]-> p = , x-> son[]-> fa = NULL, x-> son[] = NULL; update(x);
px[a] = a + b > n ? : a + b;
x-> p = px[a];
}
}
} int main() {
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
read();
sov();
return ;
}

bzoj 2049 洞穴勘探

非常裸的操作,不过我查询是在splay上暴力,应该有更好的方法(' '    )

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = ; struct node {
int size, p, lr, data;
node *son[], *fa;
}tr[maxn]; void test(node* x) {
if(x) {
cout << x-> data <<" "<< x-> size <<" "<< x-> p <<" "<< x-> lr << endl;
for(int i = ; i < ; ++ i) test(x-> son[i]);
}
} void update(node* x) {
x-> size = ;
for(int i = ; i < ; ++ i) if(x-> son[i]) x-> size += x-> son[i]-> size;
} void swap(node* &a, node* &b) {
node* mid = a; a = b, b = mid;
} void pushdown(node* x) {
if(x && x-> lr) {
swap(x-> son[], x-> son[]);
for(int i = ; i < ; ++ i) if(x-> son[i]) x-> son[i]-> lr ^= ;
x-> lr = ;
}
} void rotate(node* x, int f) {
node* y = x-> fa;
if(y-> fa) {
if(y-> fa-> son[] == y) y-> fa-> son[] = x;
else y-> fa-> son[] = x;
}
x-> fa = y-> fa, y-> fa = x, x-> size = y-> size, x-> p = y-> p;
y-> son[f] = x-> son[!f];
if(x-> son[!f]) x-> son[!f]-> fa = y;
x-> son[!f] = y;
update(y);
} void splay(node* x, node* f) {
pushdown(x);
while(x-> fa != f) {
pushdown(x-> fa-> fa), pushdown(x-> fa), pushdown(x);
if(x-> fa-> fa == f) {
int a = x-> fa-> son[] == x ? : ;
rotate(x, a);
}
else {
node* y = x-> fa, *z = y-> fa;
int a = z-> son[] == y ? : ;
int b = y-> son[] == x ? : ;
if(a == b) rotate(y, a), rotate(x, b);
else rotate(x, b), rotate(x, a);
}
}
} void access(int cur) {
node* x = tr + cur, *y; int pp;
splay(x, NULL); if(x-> son[]) x-> son[]-> fa = NULL, x-> son[]-> p = cur, x-> son[] = NULL, update(x);
while((pp = x-> p)) {
y = tr + pp; splay(y, NULL);
if(y-> son[]) y-> son[]-> fa = NULL, y-> son[]-> p = pp, y-> son[] = NULL, update(y);
y-> son[] = x, x-> fa = y; update(y);
splay(x, NULL);
}
} void reserve(int cur) {
access(cur);
(tr + cur)-> lr ^= ;
} void cut(int a, int b) {
reserve(a), access(b);
(tr + a)-> p = , (tr + a)-> fa = NULL, (tr + b)-> son[] = NULL;
update(tr + a), update(tr + b);
} void link(int a, int b) {
reserve(a), access(b);
(tr + a)-> p = b;
update(a + tr), update(b + tr);
} bool query(int a, int b) {
reserve(a); access(b);
node* x = tr + a;
while(x-> fa) x = x-> fa;
return x == (tr + b);
} int int_get() {
int x = ; char c = (char)getchar(); bool f = ;
while(!isdigit(c) && c != '-' ) c = (char)getchar();
if(c == '-') c = (char)getchar(), f = ;
while(isdigit(c)) {
x = x * + (int)(c - '');
c = (char)getchar();
}
if(f) x = -x;
return x;
} int n, m; void sov() {
n = int_get(), m = int_get();
for(int i = ; i <= n; ++ i) (tr + i)-> size = , (tr + i)-> p = (tr + i)-> lr = , (tr + i)-> data = i;
while(m --) {
char s[]; scanf("%s", s + );
int a, b; a = int_get(), b = int_get();
if(s[] == 'C') link(a, b);
if(s[] == 'D') cut(a, b);
if(s[] == 'Q') {
if(query(a, b)) printf("Yes\n");
else printf("No\n");
}
}
} int main() {
//freopen("test.in", "r", stdin);
sov();
// test(tr + 123);
}

lct 模版题 bzoj 2002 2049的更多相关文章

  1. 以 BZOJ 2002 为例学习有根树LCT(Link-Cut Tree)

    以BZOJ 2002 弹飞绵羊为例学习有根树LCT(Link-Cut Tree) 注:本文非常简单,只涉及有根树LCT,对于无根树,LCT还有几个本文没有提到的操作,以后慢慢更新 =v= 知识储备 [ ...

  2. BZOJ 2002 && BZOJ 2409 LCT && BZOJ 3282 初步练习

    #include <cstdio> ; inline void Get_Int(int & x) { ; ') ch=getchar(); +ch-'; ch=getchar(); ...

  3. 【BZOJ】2049: [Sdoi2008]Cave 洞穴勘测 LCT

    [题意]给定n个点和m个操作,每次操作:1.连接2个点.2.断开2个点.3.查询2个点是否连通.m<=2*10^5. [算法]Link-Cut Tree [题解]LCT模板题,Link,Cut, ...

  4. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 9071  Solved: 4652[Submi ...

  5. bzoj 2002 Bounce 弹飞绵羊

    bzoj 2002 Bounce 弹飞绵羊 设一个虚拟节点表示被弹飞,则每个点的后继点是唯一确定的,每个点向它的后继点连边,就形成了一颗树. 询问就是问某个节点到虚拟节点的路径长度,修改就删除原来向后 ...

  6. [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)

    [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...

  7. 【poj 1962】Corporative Network(图论--带权并查集 模版题)

    P.S.我不想看英文原题的,但是看网上题解的题意看得我 炒鸡辛苦&一脸懵 +_+,打这模版题的代码也纠结至极了......不得已只能自己翻译了QwQ . 题意:有一个公司有N个企业,分成几个网 ...

  8. HDU 2222 Keywords Search(AC自动机模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)

    1008. Airline Routes (35) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a ...

随机推荐

  1. 什么是http协议(二)

    一.概念 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器. ...

  2. 【leetcode】937. Reorder Log Files

    题目如下: You have an array of logs.  Each log is a space delimited string of words. For each log, the f ...

  3. jstat性能分析

    垃圾回收统计 S0C:第一个幸存区的大小 S1C:第二个幸存区的大小 S0U:第一个幸存区的使用大小 S1U:第二个幸存区的使用大小 EC:伊甸园区的大小 EU:伊甸园区的使用大小 OC:老年代大小 ...

  4. Hibernate 单项多对1

    自己理解: 单向1对多. 一个客户可以发出多个订单.但是一个订单只属于一个客户. 写对象的时候.在多的那个类对象把1的作为自己的一个属性. 写配置文件 <many-to-one name=1的属 ...

  5. Linux下安装Tomcat服务器

    Linux下安装Tomcat服务器 一.总结 一句话总结: linux多用才能熟 1.阿里云上面我们买的服务器,怎么让它可以访问特定的端口? 就是给服务器的安全组添加规则:实例-->更多--&g ...

  6. js判断变量未定义

    js判断变量未定义 控制台输出未定义变量a会报错: 我们打印出a的数据类型是: 我们可以看到未定义变量的数据类型是 "undefined" 所以判断js变量是否未定义的方法就是 t ...

  7. FATFS模块应用笔记

    FATFS模块应用笔记 如何港 范围 内存使用 模块尺寸缩小 长文件名 统一的API 重入 复制文件访问 性能有效文件访问 对闪存介质考虑 关键的第 延长使用FATFS API 关于FATFS许可证 ...

  8. 析构中delete this

    查看下面代码如何出错 #include <iostream> using namespace std; class A { public: A() { p = this; } ~A() { ...

  9. 12. MySQL简单使用

    关于MySQL的使用,大家可以去网上看相关教程,但是为了保证阅读的连贯性,这里会做简单介绍. 创建数据库 我们双击刚刚新建的数据库,然后双击mysql,点击新建查询,可以在编辑器里面输入一些mysql ...

  10. 判断是否英文字母或数字的C#正则表达式

    private int IsDigitOrNumber(string str) { if(System.Text.RegularExpressions.Regex.IsMatch(str,@" ...