[BZOJ2002][Hnoi2010]Bounce 弹飞绵羊

原题地址

早期的LCT简直无法直视OLZ…(我写的是splay时自顶向下一条路清标记的方法…

AC code:

#include <cstdio>
#include <stack>
using namespace std;
const int N=200010;
int n,m;

struct nod{
    bool rev;
    int  size;
    nod  *ch[2],*pr,*pp;
}*NIL,pt[N],*tmp[N];

void clear(nod *x){
    if(!x->rev) return ;
    nod *t=x->ch[0];x->ch[0]=x->ch[1];x->ch[1]=t;
    x->rev=0;x->ch[0]->rev^=1;x->ch[1]->rev^=1;
}

void update(nod *x){
    x->size=x->ch[0]->size+x->ch[1]->size+1;
}

void rotate(nod *x,bool t){
    nod *y=x->pr,*z=y->pr,*b=x->ch[t^1];
    b->pr=y;y->pr=x;x->pr=z;y->ch[t]=b;x->ch[t^1]=y;
    if(z->ch[0]==y) z->ch[0]=x;
    else if(z->ch[1]==y) z->ch[1]=x;
    update(y);update(x);
}

void splay(nod *x){
    int tot=0;
    nod *r=x;
    for(nod *i=x;i!=NIL;i=i->pr) tmp[++tot]=i;
    for(int i=tot;i>0;i--) clear(tmp[i]);
    while(x->pr!=NIL){
        nod *y=x->pr,*z=y->pr;
        r=(z==NIL?y:z);
        if(z==NIL&&y->ch[0]==x) rotate(x,0);
        else if(z==NIL&&y->ch[1]==x) rotate(x,1);
        else if(z->ch[0]==y&&y->ch[0]==x) {rotate(y,0);rotate(x,0);}
        else if(z->ch[1]==y&&y->ch[1]==x) {rotate(y,1);rotate(x,1);}
        else if(z->ch[1]==y&&y->ch[0]==x) {rotate(x,0);rotate(x,1);}
        else {rotate(x,1);rotate(x,0);}
    }
    x->pp=r->pp;
}

void access(nod *x){
    for(nod *u=x,*v=NIL;u!=NIL;v=u,u=u->pp)
        {splay(u);u->ch[1]->pr=NIL;u->ch[1]->pp=u;u->ch[1]=v;v->pr=u;update(u);}
}

void link(nod *x,nod *y){
    access(x);splay(x);x->pp=y;x->rev^=1;
}

void cut(nod *x){
    access(x);splay(x);x->ch[0]->pr=x->ch[0]->pp=NIL;x->ch[0]=NIL;update(x);
}

int getdep(nod *x){
    access(x);splay(x);
    return x->ch[0]->size+1;
}

int main(){
    NIL=&pt[N-1];
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        pt[i].size=1;
        pt[i].ch[0]=pt[i].ch[1]=pt[i].pr=pt[i].pp=NIL;
    }
    for(int i=0;i<n;i++){
        int k;
        scanf("%d",&k);
        if(i+k<n) link(&pt[i],&pt[i+k]);
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++){
        int a,b,c;
        scanf("%d%d",&a,&b);
        if(a==1) printf("%d\n",getdep(&pt[b]));
        else{
            scanf("%d",&c);
            cut(&pt[b]);
            if(b+c<n) link(&pt[b],&pt[b+c]);
        }
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值