动态树LCT||分块(BZOJ2002)

2002: [Hnoi2010]Bounce 弹飞绵羊

Time Limit: 10 Sec   Memory Limit: 259 MB
Submit: 4721   Solved: 2503
[ Submit][ Status][ Discuss]

Description

某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏。游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系数ki,当绵羊达到第i个装置时,它会往后弹ki步,达到第i+ki个装置,若不存在第i+ki个装置,则绵羊被弹飞。绵羊想知道当它从第i个装置起步时,被弹几次后会被弹飞。为了使得游戏更有趣,Lostmonkey可以修改某个弹力装置的弹力系数,任何时候弹力系数均为正整数。

Input

第一行包含一个整数n,表示地上有n个装置,装置的编号从0到n-1,接下来一行有n个正整数,依次为那n个装置的初始弹力系数。第三行有一个正整数m,接下来m行每行至少有两个数i、j,若i=1,你要输出从j出发被弹几次后被弹飞,若i=2则还会再输入一个正整数k,表示第j个弹力装置的系数被修改成k。对于20%的数据n,m<=10000,对于100%的数据n<=200000,m<=100000

Output

对于每个i=1的情况,你都要输出一个需要的步数,占一行。

Sample Input

4
1 2 1 1
3
1 1
2 1 1
1 1

Sample Output

2
3

HINT

Source

思路:

两种操作:i=1,询问跳几次能跳出去

                i=2,修改第j个装置的弹跳力为k

维护一个虚拟节点,表示跳出去的节点,维护一个size数组,表示Splay的节点和子树的节点数的和(跟splay的一样);

对于第一种操作,首先access(i),然后Splay(i),那么i的左子树的size表示的就是i的祖先节点的个数,也就是答案

对于第二种操作,把j节点与其父节点分开,然后link到相应的节点

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=300010;
const int INF=1000000000;
int N,Q;
int a[maxn];
struct Edge
{
    int v,next;
}edge[maxn*2];
int head[maxn],tot;
void add_edge(int u,int v)
{
    edge[tot].v=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}
struct LCT
{
    int ch[maxn][2],pre[maxn],key[maxn];
    int size[maxn],rev[maxn];
    bool rt[maxn];
    void dfs(int u)
    {
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(pre[v]!=0)continue;
            pre[v]=u;
            dfs(v);
        }
    }
    void update_rev(int r)
    {
        if(!r)return;
        swap(ch[r][0],ch[r][1]);
        rev[r]^=1;
    }
    void pushdown(int r)
    {
        if(rev[r])
        {
            update_rev(ch[r][1]);
            update_rev(ch[r][0]);
            rev[r]=0;
        }
    }
    void pushup(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;
        pushup(y);
    }
    //将根节点到r的路径上的所有及诶单的标记下方
    void P(int r)
    {
        if(!rt[r])P(pre[r]);
        pushdown(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);
        }
        pushup(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;
            pushup(x);
        }
        return y;
    }
     int getroot(int x)
     {
        Access(x);
        Splay(x);
        while (ch[x][0])
            x = ch[x][0];
        return x;
    }
    //判断是否同根
    bool judge(int u,int v)
    {
        while(pre[u])u=pre[u];
        while(pre[v])v=pre[v];
        return u==v;
    }
    //将r变成他所在根
    void mroot(int r)
    {
        Access(r);
        Splay(r);
        update_rev(r);
    }
    //调用后u是原来u和v的lca,v和ch[u][1]分别存折lca的两个儿子
    void lca(int &u,int &v)
    {
        Access(v),v=0;
        while(u)
        {
            Splay(u);
            if(!pre[u])return;
            rt[ch[u][1]]=true;
            rt[ch[u][1]=v]=false;
            pushup(u);
            u=pre[v=u];
        }
    }
    //将u合并到v上
    void link(int u,int v)
    {
        mroot(u);
        pre[u]=v;
    }
    //将v和他的父节点分离
    void cut(int v)
    {
        //mroot(v);
        Access(v);
        Splay(v);
        pre[ch[v][0]]=0;
        pre[v]=0;
        rt[ch[v][0]]=true;
        ch[v][0]=0;
        pushup(v);
    }
    void init()
    {
        memset(head,-1,sizeof(head));
        memset(pre,0,sizeof(pre));
        memset(ch,0,sizeof(ch));
        memset(rev,0,sizeof(rev));
        for(int i=0;i<=N+1;i++)rt[i]=true,size[i]=1;
        size[0]=0;
    }
}tree;
int main()
{
    while(scanf("%d",&N)!=EOF)
    {
        for(int i=1;i<=N;i++)scanf("%d",&a[i]);
        scanf("%d",&Q);
        tree.init();
        for(int i=1;i<=N;i++)
        {
            if(i+a[i]>N+1)
                tree.pre[i]=N+1;
            else tree.pre[i]=i+a[i];
        }
        while(Q--)
        {
            int i,j,k;
            scanf("%d%d",&i,&j);
            if(i==1)
            {
                j++;
                tree.Access(j);
                tree.Splay(j);
                printf("%d\n",tree.size[tree.ch[j][0]]);
            }
            else
            {
                scanf("%d",&k);
                j++;
                tree.Access(j);
                tree.cut(j);
                tree.mroot(j);
                int tmp=k+j;
                if(tmp>N+1)tmp=N+1;
                tree.link(j,tmp);
            }
        }
    }
    return 0;
}

分块做法:

对于每个弹簧 我们记录一下从这个弹簧出发直到弹到块外为止的弹跳次数及落点

查询沿着落点弹到出去为止 修改从块开始到这个点为止修改一遍

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=200010;
int N,M;
int a[maxn];
int size;
int pos[maxn];
int cnt[maxn];
void solve(int l,int r)
{
    for(int i=r;i>=l;i--)
    {
        int block=i/size;
        if(i+a[i]>=N)pos[i]=-1,cnt[i]=1;
        else if(i+a[i]>=(block+1)*size)pos[i]=i+a[i],cnt[i]=1;
        else pos[i]=pos[i+a[i]],cnt[i]=cnt[i+a[i]]+1;
    }
}
int main()
{
    int op,x,y;
    while(scanf("%d",&N)!=EOF)
    {
        for(int i=0;i<N;i++)scanf("%d",&a[i]);
        memset(pos,-1,sizeof(pos));
        memset(cnt,0,sizeof(cnt));
        size=sqrt(N);
        solve(0,N-1);
        scanf("%d",&M);
        while(M--)
        {
            scanf("%d%d",&op,&x);
            if(op==1)
            {
                int ans=0;
                while(x!=-1)
                {
                    ans+=cnt[x];
                    x=pos[x];
                }
                printf("%d\n",ans);
            }
            else
            {
                scanf("%d",&y);
                int x_pos=x/size;
                a[x]=y;
                solve(x_pos*size,x);
            }
        }
    }
    return 0;
}







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值