【bzoj 2002】弹飞绵羊(LCT)

传送门biu~
最终的图一定是一棵树,算一个点被弹飞需要几次只需要计算一下这个点到根的路径上有多少点就可以了,LCT。
分块做法【点这里】

#include<bits/stdc++.h>
using namespace std;
struct Node{
    Node *ch[2],*fa;
    int siz;
    Node();
    inline int dir(){
        if(fa->ch[0]==this)     return 0;
        if(fa->ch[1]==this)     return 1;
        return -1;
    }
    inline void maintain();
}*null=new Node,tree[200005];
Node :: Node(){
    ch[0]=ch[1]=fa=null;
    siz=1;
}
inline void Node :: maintain(){
    if(this==null)  return;
    siz=ch[0]->siz+ch[1]->siz+1;
}
inline void Rotate(Node *o,int d){
    Node *p=o->ch[d^1];
    o->ch[d^1]=p->ch[d];p->ch[d]->fa=o;
    p->ch[d]=o;
    o->maintain();p->maintain();
    if(~o->dir()) o->fa->ch[o->dir()]=p;
    p->fa=o->fa;o->fa=p;
}
inline void Splay(Node *o){
    while(~o->dir()){
        if(o->dir()==o->fa->dir())      Rotate(o->fa->fa,o->dir()^1);
        Rotate(o->fa,o->dir()^1);
    }
}
inline void Access(Node *o){
    Node *p=null;
    while(o!=null){
        Splay(o);
        o->ch[1]=p; o->maintain();
        p=o;
        o=o->fa;
    }
}
inline void Link(Node *x,Node *y){
    Access(x);
    Splay(x);
    x->ch[0]->fa=null;
    x->ch[0]=null;
    x->fa=y;
    x->maintain();
}
int main(){
    null->siz=0;
    Node *root;
    int n,m;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        int x;
        scanf("%d",&x);
        if(i+x<=n)      tree[i].fa=&tree[i+x];
    }
    scanf("%d",&m);
    while(m--){
        int opt;
        scanf("%d",&opt);
        if(opt==1){
            int x;
            scanf("%d",&x);++x;
            Access(&tree[x]);
            Splay(&tree[x]);
            printf("%d\n",tree[x].siz);
        }
        else{
            int x,y;
            scanf("%d%d",&x,&y);++x;
            if(x+y<=n)  root=&tree[x+y];
            else        root=null;
            Link(&tree[x],root);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zP1nG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值