bzoj 4764: 弹飞大爷 lct

题意

有n个点,编号为1到n,每个点有一个指向的点to[x]。每次会修改一个点的to或者询问从一个点开始,每次沿着to[x]走,要走多少步才能走到一个不存在的点。
n,m200000 n , m ≤ 200000

分析

显然这是一个环加内向树森林。那么对于每棵环加内向树,我们将环上的一条边断掉,然后将出度为0的点作为根,建一个森林。
对于询问,若当前点的根不是n+1,则输出-1,否则就输出其深度即可。
对于修改操作,若它是根,则考虑新连的边是否在其子树内。不然的话,先把它到它父亲的边断掉,然后看它所在子树的根连出的边是否在它子树内,是的话就连上。接着再看它连出去的边是否在自己的子树内即可。
本题的lct并不用打标记,所以十分爽歪歪。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

const int N=200005;

int n,m,to[N];
struct tree{int l,r,s,fa;}t[N];

int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

void updata(int x)
{
    t[x].s=t[t[x].l].s+t[t[x].r].s+1;
}

bool is_root(int x)
{
    return x!=t[t[x].fa].l&&x!=t[t[x].fa].r;
}

void rttl(int x)
{
    int y=t[x].r;
    t[x].r=t[y].l;t[t[y].l].fa=t[y].l?x:0;
    if (x==t[t[x].fa].l) t[t[x].fa].l=y;
    else if (x==t[t[x].fa].r) t[t[x].fa].r=y;
    t[y].fa=t[x].fa;t[y].l=x;t[x].fa=y;
    updata(x);updata(y);
}

void rttr(int x)
{
    int y=t[x].l;
    t[x].l=t[y].r;t[t[y].r].fa=t[y].r?x:0;
    if (x==t[t[x].fa].l) t[t[x].fa].l=y;
    else if (x==t[t[x].fa].r) t[t[x].fa].r=y;
    t[y].fa=t[x].fa;t[y].r=x;t[x].fa=y;
    updata(x);updata(y);
}

void splay(int x)
{
    while (!is_root(x))
    {
        int p=t[x].fa,g=t[p].fa;
        if (is_root(p))
        {
            if (x==t[p].l) rttr(p);
            else rttl(p);
            break;
        }
        if (x==t[p].l)
            if (p==t[g].l) rttr(g),rttr(p);
            else rttr(p),rttl(g);
        else
            if (p==t[g].r) rttl(g),rttl(p);
            else rttl(p),rttr(g);
    }
}

void access(int x)
{
    int y=0;
    while (x)
    {
        splay(x);
        t[x].r=y;
        updata(x);
        y=x;x=t[x].fa;
    }
}

void link(int x,int y)
{
    if (x==y) return;
    splay(x);t[x].fa=y;
}

void cut(int x,int y)
{
    if (x==y) return;
    access(x);splay(y);t[y].r=t[x].fa=0;
    updata(y);
}

int find_root(int x)
{
    access(x);splay(x);
    int y=0;
    while (x) y=x,x=t[x].l;
    return y;
}

int main()
{
    n=read();m=read();to[n+1]=n+1;
    for (int i=1;i<=n+1;i++) t[i].s=1;
    for (int i=1;i<=n;i++)
    {
        to[i]=i+read();
        if (to[i]<1||to[i]>n) to[i]=n+1;
        if (find_root(i)!=find_root(to[i])) link(i,to[i]);
    }
    while (m--)
    {
        int op=read(),x=read();
        if (op==1)
        {
            int rt=find_root(x);
            if (rt!=n+1) puts("-1");
            else splay(x),printf("%d\n",t[t[x].l].s);
        }
        else
        {
            int rt=find_root(x);
            if (rt==x)
            {
                to[x]=read()+x;
                if (to[x]<1||to[x]>n) to[x]=n+1;
                if (find_root(to[x])!=x) link(x,to[x]);
                continue;
            }
            cut(x,to[x]);
            if (find_root(to[rt])==x) link(rt,to[rt]);
            to[x]=read()+x;
            if (to[x]<1||to[x]>n) to[x]=n+1;
            if (find_root(to[x])!=x) link(x,to[x]);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值