lct 模版题 bzoj 2002 2049
很早就有人给我推荐的模版题,然后我最近才刷的(' ' ) 昨天的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的更多相关文章
- 以 BZOJ 2002 为例学习有根树LCT(Link-Cut Tree)
以BZOJ 2002 弹飞绵羊为例学习有根树LCT(Link-Cut Tree) 注:本文非常简单,只涉及有根树LCT,对于无根树,LCT还有几个本文没有提到的操作,以后慢慢更新 =v= 知识储备 [ ...
- BZOJ 2002 && BZOJ 2409 LCT && BZOJ 3282 初步练习
#include <cstdio> ; inline void Get_Int(int & x) { ; ') ch=getchar(); +ch-'; ch=getchar(); ...
- 【BZOJ】2049: [Sdoi2008]Cave 洞穴勘测 LCT
[题意]给定n个点和m个操作,每次操作:1.连接2个点.2.断开2个点.3.查询2个点是否连通.m<=2*10^5. [算法]Link-Cut Tree [题解]LCT模板题,Link,Cut, ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 9071 Solved: 4652[Submi ...
- bzoj 2002 Bounce 弹飞绵羊
bzoj 2002 Bounce 弹飞绵羊 设一个虚拟节点表示被弹飞,则每个点的后继点是唯一确定的,每个点向它的后继点连边,就形成了一颗树. 询问就是问某个节点到虚拟节点的路径长度,修改就删除原来向后 ...
- [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)
[BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...
- 【poj 1962】Corporative Network(图论--带权并查集 模版题)
P.S.我不想看英文原题的,但是看网上题解的题意看得我 炒鸡辛苦&一脸懵 +_+,打这模版题的代码也纠结至极了......不得已只能自己翻译了QwQ . 题意:有一个公司有N个企业,分成几个网 ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)
1008. Airline Routes (35) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a ...
随机推荐
- 【leetcode】915. Partition Array into Disjoint Intervals
题目如下: 解题思路:题目要求的是在数组中找到一个下标最小的index,使得index左边(包括自己)子序列的最大值小于或者等于右边序列的最小值.那么我们可以先把数组从最左边开始到数组最右边所有子序列 ...
- Vue学习笔记【27】——Vue路由(设置路由)
设置路由高亮 css: .router-link-active, /* vue-router*/ .myactive { color: red; font-weigh ...
- 这样才能正确解锁MaxCompute客户端
大数据计算服务(MaxCompute,原名ODPS)是一种快速.完全托管的TB/PB级数据仓库解决方案.MaxCompute向用户提供了完善的数据导入方案以及多种经典的分布式计算模型,能够更快速的解决 ...
- Qt的信号和槽机制
一.信号和槽机制 信号和槽用于两个对象之间的通信,我们希望任何对象都可以和其他对象进行通信. 当一个特殊的事情发生时便可以发射一个信号,而槽就是一个函数,它在信号发射后被调用来相应这个信号.( ...
- POJ 1426 Find The Multiple (dfs??!!)
Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...
- 常用github命令
常用github命令 git--版本控制软件 GitHub是一个基于Git的远程文件托管平台(同GitCafe.BitBucket和GitLab等). 在家里,开发完毕部分功能,推送到GitHub: ...
- [MySQL] innobackupex在线备份及恢复(全量和增量)
安装percona-xtrabackup 方法1: percona-xtrabackup-2.1.9-744-Linux-x86_64.tar.gz(D:\share\src\linux-mysql) ...
- 使用 nuxt+iview-admin+koa2 开发项目
公司最近在做的一个项目,依然是采用熟悉的vue开发,数据平台因为其数据量大的特点,采用传统的spa模式,首页加载时间很长,而SSR这种方式对于首屏的加载时间优化显而易见,同时还可以方便的进行SEO.因 ...
- 【Java架构:基础技术】一篇文章搞掂:Maven
本文篇幅较长,建议合理利用右上角目录进行查看(如果没有目录请刷新). 本文基于<Maven 实战>一书进行总结和扩展,大家也可以自行研读此书. 一.Maven简介 1.1.什么是Maven ...
- (59)C# IIS Express 设置
支持非localhost访问 本地测试时 修改 localhost改成127.0.0.1