【lct】bzoj2002 [Hnoi2010]Bounce 弹飞绵羊
lct板子,此题主要有cut操作和link操作。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 200005
#define INF 2147483647
int fa[maxn],val[maxn],c[maxn][2],root,tot,siz[maxn];
bool delta[maxn],is_root[maxn];
void Maintain(int x)
{
siz[x]=siz[c[x][0]]+siz[c[x][1]]+1;
}
//void Mark(int x){
// if(x){
// delta[x]^=1;
// }
//}
//void pushdown(int x){
// if(delta[x]){
// Mark(c[x][0]);
// Mark(c[x][1]);
// swap(c[x][0],c[x][1]);
// delta[x]=0;
// Maintain(x);
// }
//}
//void NewNode(int &x,int Fa)
//{
// x=++tot;
// fa[x]=Fa;
// c[x][0]=c[x][1]=0;
// siz[x]=1;
//}
void Rotate(int x,bool flag)
{
int y=fa[x];
// pushdown(y);
// pushdown(x);
c[y][!flag]=c[x][flag];
if(c[x][flag]){
fa[c[x][flag]]=y;
}
if(fa[y] && c[fa[y]][c[fa[y]][1]==y]==y){
c[fa[y]][c[fa[y]][1]==y]=x;
}
fa[x]=fa[y];
c[x][flag]=y;
fa[y]=x;
if(is_root[y]){
is_root[y]=0;
is_root[x]=1;
}
Maintain(y);
Maintain(x);
}
void Splay(int x)
{
if(!x || is_root[x]){
return;
}
// pushdown(x);
int y;
while(y=fa[x],(!is_root[x])){
if(is_root[y]){
Rotate(x,c[y][0]==x);
}
else{
if((c[y][0]==x)==(c[fa[y]][0]==y)){
Rotate(y,c[fa[y]][0]==y);
}
else{
Rotate(x,c[y][0]==x);
y=fa[x];
}
Rotate(x,c[y][0]==x);
}
}
Maintain(x);
}
void Access(int x){
int y;
Splay(x);
while(fa[x]){
y=fa[x];
Splay(y);
if(c[y][1]){
is_root[c[y][1]]=1;
}
is_root[x]=0;
c[y][1]=x;
Splay(x);
}
if(c[x][1]){
is_root[c[x][1]]=1;
c[x][1]=0;
}
}
int FindRoot(int x){
Access(x);
Splay(x);
while(c[x][0]){
x=c[x][0];
}
return x;
}
void cut(int x){
Access(x);
Splay(x);
fa[c[x][0]]=0;
if(c[x][0]){
is_root[c[x][0]]=1;
}
c[x][0]=0;
}
void link(int x,int y){
Access(x);
Splay(x);
// Mark(c[x][0]);
Access(y);
Splay(y);
c[y][1]=x;
fa[x]=y;
is_root[x]=0;
}
int calc(int x){
Access(x);
Splay(x);
return siz[c[x][0]]+1;
}
int n,m;
int main(){
// freopen("bzoj2002.in","r",stdin);
// freopen("bzoj2002.out","w",stdout);
int op,d,g;
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&d);
fa[i]=(i+d<=n ? i+d : 0);
c[i][0]=c[i][1]=0;
siz[i]=1;
delta[i]=0;
is_root[i]=1;
}
scanf("%d",&m);
for(int i=1;i<=m;++i){
scanf("%d%d",&op,&d); ++d;
if(op==1){
printf("%d\n",calc(d));
}
else{
scanf("%d",&g);
cut(d);
if(d+g<=n){
link(d,d+g);
}
}
}
return 0;
}
【lct】bzoj2002 [Hnoi2010]Bounce 弹飞绵羊的更多相关文章
- BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 【LCT】【分块】
		
BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始, ...
 - [bzoj2002][Hnoi2010]Bounce弹飞绵羊_LCT
		
Bounce弹飞绵羊 bzoj-2002 Hnoi-2010 题目大意:n个格子,每一个格子有一个弹簧,第i个格子会将经过的绵羊往后弹k[i]个,达到i+k[i].如果i+k[i]不存在,就表示这只绵 ...
 - bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 [分块][LCT]
		
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
 - [BZOJ2002] [Hnoi2010] Bounce 弹飞绵羊 (LCT)
		
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
 - BZOJ2002: [Hnoi2010]Bounce 弹飞绵羊(LCT)
		
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在 他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装 ...
 - [bzoj2002][Hnoi2010]Bounce弹飞绵羊——分块
		
Brief description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装 ...
 - bzoj2002 [Hnoi2010]Bounce 弹飞绵羊【分块】
		
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 这一题除了LCT解法,还有一种更巧妙,代码量更少的解法,就是分块.先想,如果仅仅记录每 ...
 - bzoj2002 [Hnoi2010]Bounce 弹飞绵羊——分块
		
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 第一次用分块,感觉超方便啊: 如果记录每个点的弹力系数,那么是O(1)修改O(n)查询 ...
 - 【分块】【LCT】bzoj2002 [Hnoi2010]Bounce 弹飞绵羊
		
分块,每个点统计还有几步弹出该块,以及它弹出块后的下一个节点是哪个点. 注意:update某个点的时候,会可能对当前块内 该点及以前的点 产生影响,所以对这部分点进行更新. #include<c ...
 
随机推荐
- mysql-front导入数据失败:“在多字节的目标代码页中,没有此 Unicode 字符可以映射到的字符”
			
mysql-front导入sql文件失败,弹出框显示如下: 解决方法:在选择文件时,选择合适的字符集即可 参考:http://www.th7.cn/db/mysql/201604/185149.sht ...
 - git push multiple repo
			
git remote add xxx https://git.github.com
 - python基础===python自带idle的快捷键
			
Ctrl + [ Ctrl + ] 缩进代码Alt+3 Alt+4 注释.取消注释代码行Alt+5 Alt+6 切换缩进方式 空格<=>TabAlt+/ 单词完成,只要文中出现过,就可 ...
 - 牛B的日本精神
			
在汤森路透评选出的<2015全球创新企业百强>榜单里,日本以40家高居榜首,力压美国的35家.而中国内地无一入围. 在中国媒体上,我们见到的日本是“失去的20年”,经济衰退.创新能力丧 ...
 - linux自动获得mac地址,修改网络配置
			
1.修改网络配置,自动获得mac地址 删除 /etc/udev/rules.d/70-persistent-net.rules 文件 删除 /etc/sysconfig/network-scripts ...
 - python 面试
			
知识总结 面试(一)
 - python进行des加密解密,而且可以与JAVA进行互相加密解密
			
import binasciifrom pyDes import des, CBC, PAD_PKCS5import uuidimport time # pip install -i https:// ...
 - 服务器fsockopen函数和pfsockopen函数开启及作用
			
摘要: fsockopen()函数的作用是可以用来打开一个socket连接,另一个函数pfsockopen()也有相似的功能,只不过后者是一个“持续”(persistent)的fsockopen()函 ...
 - (转)Where与Having的总结
			
Where 是一个约束声明,使用Where来约束来之数据库的数据,Where是在结果返回之前起作用的,且Where中不能使用聚合函数. Having 是一个过滤声明,是在查询返回结果集以后对查询结果进 ...
 - Java 大小写转换
			
Java 大小写转换 public class CaseConversion { /** * @param character: a character * @return: a character ...