BZOJ 2002 弹飞绵羊
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 弹飞绵羊的更多相关文章
- BZOJ 2002 弹飞绵羊(分块)
题目:弹飞绵羊 这道题,据说是lct裸题,但是lct那么高级的数据结构,我并不会,所以采取了学长讲过的分块做法,我们对序列分块,可以定义两个数组,其中一个表示从当前位置跳出当前块需要多少步,另一个数组 ...
- [bzoj] 2002 弹飞绵羊 || LCT
原题 简单的LCT练习题. 我们发现对于一个位置x,他只能跳到位置x+k,也就是唯一的父亲去.加入我们将弹飞的绵羊定义为跳到了n+1,那么这就形成了一棵树.而因为要修改k,所以这颗树是动态连边的,那么 ...
- bzoj 2002: 弹飞绵羊 Link-Cut-Tree
题目: Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置, ...
- bzoj 2002 弹飞绵羊 分块
正解lct,然而本蒟蒻并不会.... 分块思路很清晰,处理出每个点弹出所在块所需要的步数及出去后的第一个位置 #include<cstdio> #include<cstring> ...
- bzoj 2002 弹飞绵羊 lct裸题
上一次用分块过了, 今天换了一种lct(link-cut tree)的写法. 学lct之前要先学过splay. lct 简单的来说就是 一颗树, 然后每次起作用的都是其中的某一条链. 所以每次如果需要 ...
- bzoj 2002 Bounce 弹飞绵羊
bzoj 2002 Bounce 弹飞绵羊 设一个虚拟节点表示被弹飞,则每个点的后继点是唯一确定的,每个点向它的后继点连边,就形成了一颗树. 询问就是问某个节点到虚拟节点的路径长度,修改就删除原来向后 ...
- [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)
[BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 9071 Solved: 4652[Submi ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
随机推荐
- c++入门之初话指针
先上代码:再进行总结知识: # include "iostream" struct ant_year_end { int year; }; int main() { using n ...
- pandas数据清洗策略1
Pandas常用的数据清洗5大策略如下: 1.删除 DataFrame 中的不必要 columns 2.改变 DataFrame 的 index 3.使用 .str() 方法来清洗 columns 4 ...
- MySQL数据类型--日期和时间类型
MySQL中的多种时间和格式数据类型 日期和时间类型是为了方便在数据库中存储日期和时间而设计的.MySQL中有多种表示日期和时间的数据类型. 其中,year类型表示时间,date类型表示日期,time ...
- 启动Tomcat的时候8080被占用
异常来源:启动Tomcat服务器报错: Several ports (8080, 8009) required by Tomcat v7.0 Server at localhost are alrea ...
- laravel 关联中的预加载
预加载 当作为属性访问 Eloquent 关联时,关联数据是「懒加载」的.意味着在你第一次访问该属性时,才会加载关联数据.不过,是当你查询父模型时,Eloquent 可以「预加载」关联数据.预加载避免 ...
- mybatis事务管理机制详解
1.mybatis事务的配置和使用 mybatis事务有两种使用方式: (a):使用JDBC的事务管理机制:即使用java.Sql.Connection对象完成对事务的提交,回滚和关闭操作. (b): ...
- (自用 )npm --save和--save-dev区别
https://blog.csdn.net/playboyanta123/article/details/78349034(原文) 目前大多数基于vue的项目都是用vue-cli 创建的.当创建项目完 ...
- jQuery 事件 - triggerHandler() 方法
定义和用法 triggerHandler() 方法触发被选元素的指定事件类型.但不会执行浏览器默认动作,也不会产生事件冒泡. triggerHandler() 方法与 trigger() 方法类似.不 ...
- Spark join连接
内链接
- windows10企业版2016长期服务版激活
win10 2016 长期服务版的ISO文件中本身就带有KMS激活KEY,不用输入任何KEY,连接网络进入CMD,只要输入:slmgr /skms kms.digiboy.irslmgr /ato这两 ...