LCT

刚学LCT,对LCT的性质不太熟练,还需要多多练习。。

对每一个点,将其与它能够到达的点连一条虚边。弹出去的话就用n+1这个节点表示。

第一种操作我们需要从LCT的性质入手,问的问题其实就是x通过多少条边可以到达n+1这个点。。那么我们可以把他们两拉成一条链(也就是split(n + 1, x)),这样就把x splay到根了,根据LCT的性质,在x和n+1联通的这棵splay中,x一定没有右子树,因为他是深度最大的点。那么左子树所有点就是原树中x到n+1的路径上的所有点了。。直接输出答案size就好了

第二种操作很简单断边再连边就行了,因为这题的边一定合法,所以没什么需要考虑的特殊情况

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
} const int N = 200005;
int n, tot, ch[N][2], size[N], fa[N], st[N], rev[N], f[N]; int newNode(){
size[++tot] = 1, fa[tot] = ch[tot][0] = ch[tot][1] = 0;
return tot;
} bool isRoot(int x){
return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
} void reverse(int x){
rev[x] ^= 1;
swap(ch[x][0], ch[x][1]);
} void push_up(int x){
size[x] = size[ch[x][0]] + size[ch[x][1]] + 1;
} void push_down(int x){
if(rev[x]){
reverse(ch[x][0]), reverse(ch[x][1]);
rev[x] ^= 1;
}
} void rotate(int x){
int y = fa[x], z = fa[y], p = (ch[y][1] == x) ^ 1;
ch[y][p^1] = ch[x][p], fa[ch[x][p]] = y;
if(!isRoot(y)) ch[z][ch[z][1] == y] = x;
fa[x] = z, fa[y] = x, ch[x][p] = y;
push_up(y), push_up(x);
} void splay(int x){
int pos = 0; st[++pos] = x;
for(int i = x; !isRoot(i); i = fa[i]) st[++pos] = fa[i];
while(pos) push_down(st[pos--]);
while(!isRoot(x)){
int y = fa[x], z = fa[y];
if(!isRoot(y)){
if((ch[y][0] == x) ^ (ch[z][0] == y)) rotate(x);
rotate(y);
}
rotate(x);
}
push_up(x);
} void access(int x){
for(int p = 0; x; p = x, x = fa[x]){
splay(x), ch[x][1] = p, push_up(x);
}
} void makeRoot(int x){
access(x), splay(x), reverse(x);
} void link(int x, int y){
makeRoot(x), fa[x] = y, push_up(y);
} void split(int x, int y){
makeRoot(x), access(y), splay(y);
} void cut(int x, int y){
split(x, y);
fa[x] = ch[y][0] = 0, push_up(y);
} int main(){ n = read();
for(int i = 1; i <= n + 1; i ++) newNode();
for(int i = 1; i <= n; i ++){
int k = read(), t = min(i + k, n + 1);
link(i, t), f[i] = t;
}
int m = read();
while(m --){
int opt = read();
if(opt == 1){
int x = read(); x ++;
split(n + 1, x); printf("%d\n", size[x] - 1);
}
else{
int x = read(), y = read(); x ++;
cut(x, f[x]);
int t = min(x + y, n + 1);
link(x, t), f[x] = t;
}
}
return 0;
}

BZOJ 2002 弹飞绵羊的更多相关文章

  1. BZOJ 2002 弹飞绵羊(分块)

    题目:弹飞绵羊 这道题,据说是lct裸题,但是lct那么高级的数据结构,我并不会,所以采取了学长讲过的分块做法,我们对序列分块,可以定义两个数组,其中一个表示从当前位置跳出当前块需要多少步,另一个数组 ...

  2. [bzoj] 2002 弹飞绵羊 || LCT

    原题 简单的LCT练习题. 我们发现对于一个位置x,他只能跳到位置x+k,也就是唯一的父亲去.加入我们将弹飞的绵羊定义为跳到了n+1,那么这就形成了一棵树.而因为要修改k,所以这颗树是动态连边的,那么 ...

  3. bzoj 2002: 弹飞绵羊 Link-Cut-Tree

    题目: Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置, ...

  4. bzoj 2002 弹飞绵羊 分块

    正解lct,然而本蒟蒻并不会.... 分块思路很清晰,处理出每个点弹出所在块所需要的步数及出去后的第一个位置 #include<cstdio> #include<cstring> ...

  5. bzoj 2002 弹飞绵羊 lct裸题

    上一次用分块过了, 今天换了一种lct(link-cut tree)的写法. 学lct之前要先学过splay. lct 简单的来说就是 一颗树, 然后每次起作用的都是其中的某一条链. 所以每次如果需要 ...

  6. bzoj 2002 Bounce 弹飞绵羊

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

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

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

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

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

  9. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...

随机推荐

  1. 线程GIL锁 线程队列 回调函数

    ----------------------------------无法改变风向,可以调整风帆;无法左右天气,可以调整心情.如果事情无法改变,那就去改变观念. # # ---------------- ...

  2. 修改docker0默认IP地址

    第一步:vim /etc/docker/daemon.json { "registry-mirrors": ["https://docker.mirrors.ustc.e ...

  3. Linux系统安装python3

    Centos7系统安装python3 在安装前需要安装依赖环境包,先安装gcc 编译器,命令如下: yum -y install gcc gcc-c++ make 1.首先查看是否安装python,系 ...

  4. poj2104 主席树裸题

    空间大小:n*lgn 复杂度:建树n*lgn  查询lgn #include <cstdio> #include <iostream> #include <algorit ...

  5. c++学习之字符串拼接

    在这里,强调这样一个问题: 可以看出,c++中,定义了string类,string 类方便我们进行字符串的一些操作,而不是像C语言中采用字符数组的方式或者指针的方式,通过上面的一些描述,可以发现: s ...

  6. 软件工程(FZU2015) 赛季得分榜,第二回合

    SE_FZU目录:1 2 3 4 5 6 7 8 9 10 11 12 13 积分规则 积分制: 作业为10分制,练习为3分制:alpha30分: 团队项目分=团队得分+个人贡献分 个人贡献分: 个人 ...

  7. semantic-ui 分割线

    分割线即原生html中的<hr>标签.不过semantic-ui中将<hr>美化了一下下. 1.基础分割线 需要注意的是分割线只能使用div标签和p标签,不能使用span标签. ...

  8. C\C++学习笔记 3

    C++记录7 函数指针: 函数名为地址, 地址指的是在机器指令存储的地址. double func(int line){ reture line*3.5;} void f(int line, doub ...

  9. spring IOC源码分析(ApplicationContext)

    在上一篇文章中,我们以BeanFactory这条主线进行IOC的源码解析的,这里,将以ApplicationContext这条线进行分析.先看使用方法: @Test public void testA ...

  10. Flutter的输入框TextField

    TextFiled组件的API 先来看一下TextFiled的构造方法: const TextField({ Key key, this.controller, this.focusNode, thi ...