题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002

题意:加边,删边,查询到根的距离。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int ch[maxn][2], pre[maxn], rev[maxn], Next[maxn];
int size[maxn];
bool rt[maxn];
void Update_Rev(int r){
if(!r) return;
swap(ch[r][0], ch[r][1]);
rev[r] ^= 1;
}
void push_down(int r){
if(rev[r]){
Update_Rev(ch[r][0]);
Update_Rev(ch[r][1]);
rev[r] = 0;
}
}
void push_up(int r){
size[r] = size[ch[r][0]]+size[ch[r][1]]+1;
}
void Rotate(int x){
int y = pre[x], kind = ch[y][1] == x;
ch[y][kind] = ch[x][!kind];
pre[ch[y][kind]] = y;
pre[x] = pre[y];
pre[y] = x;
ch[x][!kind] = y;
if(rt[y])
rt[y] = false, rt[x] = true;
else
ch[pre[x]][ch[pre[x]][1]==y]=x;
push_up(y);
}
void P(int r){
if(!rt[r]) P(pre[r]);
push_down(r);
}
void Splay(int r){
P(r);
while(!rt[r]){
int f = pre[r], ff = pre[f];
if(rt[f])
Rotate(r);
else if((ch[ff][1]==f)==(ch[f][1]==r))
Rotate(f), Rotate(r);
else Rotate(r), Rotate(r);
}
push_up(r);
}
int Access(int x){
int y = 0;
for(;x;x=pre[y=x]){
Splay(x);
rt[ch[x][1]] = true, rt[ch[x][1]=y] = false;
push_up(x);
}
return y;
}
void mroot(int r){
Access(r);
Splay(r);
Update_Rev(r);
}
void link(int u, int v){
mroot(u);
pre[u] = v;
}
void cut(int u, int v){
mroot(u);
Splay(v);
pre[ch[v][0]] = pre[v];
pre[v] = 0;
rt[ch[v][0]] = true;
ch[v][0] = 0;
push_up(v);
}
int n, m;
int main()
{
scanf("%d", &n);
for(int i=0; i<=n+1; i++){
pre[i]=0;
ch[i][0]=ch[i][1]=0;
rev[i]=0;
rt[i] = true;
size[i] = 0;
}
for(int i=1,t; i<=n; i++){
scanf("%d", &t);
t = min(i+t, n+1);
pre[i] = Next[i] = t;
size[i] = 1;
}
size[n+1] = 1;
scanf("%d", &m);
int op, x, y;
for(int i=1; i<=m; i++){
scanf("%d%d", &op,&x);
x++;
if(op==1){
mroot(n+1);
Access(x);
Splay(x);
printf("%d\n", size[ch[x][0]]);
}
else{
scanf("%d", &y);
y = min(x+y,n+1);
cut(x,Next[x]);
link(x,y);
Next[x]=y;
}
}
return 0;
}

BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 动态树的更多相关文章

  1. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 (动态树LCT)

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

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

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

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

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

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

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

  5. bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 動態樹

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

  6. bzoj 2002 : [Hnoi2010]Bounce 弹飞绵羊 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 题面: 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: ...

  7. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 【分块】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 ...

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

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意: 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆 ...

  9. 【刷题】BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊

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

随机推荐

  1. 进程池-限制同一时间在CPU上运行的进程数

    if __name__=='__main__' :  为了区分你是主动执行这个脚本,还是从别的地方把它当做一个模块去调用. 如果是主动执行,则执行.如果是调用的,则不执行主体. 1. 串行:切记切记: ...

  2. I/O复用----poll

    2018-08-01 (星期三)poll(): #include <sys/poll.h> int poll (struct pollfd *fd, unsigned int nfds, ...

  3. bzoj 3132: 上帝造题的七分钟 (二维树状数组)

    推推公式,最后变成四个东西的前缀和 然后不知道为什么一直wa,数据在本地测是没有错的& 好心的管理员还给了某位p党大神a了的代码,感人肺腑(虽然还是没发现到底我的程序是问题) var f1,f ...

  4. 923c C. Perfect Security

    Trie树. 要求字典序最小,所以由前到后贪心的选择.建一个trie树维护b数列. #include<cstdio> #include<algorithm> #include& ...

  5. [NOI2001]炮兵阵地 状压DP

    题面: 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用“H” 表示),也可能是平原(用“P”表示),如下图.在每一格平原地形上最多 ...

  6. Ubuntu安装teamviewer注意事项。

    Ubuntu安装teamviewer注意事项. 首先通过浏览器到官方下载ubuntu对应teamviewer的安装包 但是通过dpkg –i安装之后发现安装过程出问题,安装好的包打开之后也闪退. 这个 ...

  7. python安装方法- 3.6.3版本

    一. 官网下载安装包: 官网网址:https://www.python.org/ 我下载的是3.6.3版本,如下图:  二. 安装安装包, 1. 直接双击运行  2. 选择Customize inst ...

  8. BZOJ1503:[NOI2004]郁闷的出纳员——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1503 (题面复制的洛谷的,因为洛谷好看) 题目描述 OIER公司是一家大型专业化软件公司,有着数以万 ...

  9. 1 Easy Read/Write Splitting with PHP’s MySQLnd

    以下均是使用翻译软件翻译的! Note: This is part one in our Extending MySQL with PHP's MySQLnd Series, read part 2 ...

  10. Mac添加锁屏快捷键

    Mac要想添加锁屏快捷键,必须使用Automator. 1. 打开Automator,创建一个新的服务. 2. 在左侧栏中找到 启动屏幕保护 ,将其拖曳到右侧窗口内,并且修改 服务收到改为" ...