BZOJ2002 [Hnoi2010]Bounce 弹飞绵羊


Solution

很早以前写的一道分块题,最近在搞LCT,又做了一遍.

1.LCT做法

看到这种动态修改,想下LCT怎么维护.

修改操作就是\(Cut(x,k[x])\)然后再\(Link(x,k[x]')\)

剩下的只有询问了.

我们如果把弹出设为一个新节点\(n+1\),那么显然就是直接:

\(makeroot(x)\),\(access(n+1)\),\(splay(n+1)\).

最后答案就是\(siz[n+1].\)

2.分块做法

我们如果把编号分块,那么显然每一次修改就维护一下弹出的到了哪个块,然后随便搞一下就可以了.

代码实现

LCT

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi()
{
    int f=1,sum=0;char ch=getchar();
    while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    return f*sum;
}
const int N=300010;
int K[N],n;
struct node
{
    int ff,ch[2],siz,rev;
}t[N];
int sta[N],top;
void pushup(int x)
{
    t[x].siz=t[t[x].ch[0]].siz+t[t[x].ch[1]].siz+1;
}
void reverse(int x)
{
    swap(t[x].ch[0],t[x].ch[1]);
    t[x].rev^=1;
}
void pushdown(int x)
{
    if(!t[x].rev)return;
    if(t[x].ch[0])reverse(t[x].ch[0]);
    if(t[x].ch[1])reverse(t[x].ch[1]);
    t[x].rev^=1;
}
bool isroot(int x)
{
    return (t[t[x].ff].ch[0]!=x) && (t[t[x].ff].ch[1]!=x);
}
void rotate(int x)
{
    int y=t[x].ff,z=t[y].ff;
    int k=t[y].ch[1]==x;
    if(!isroot(y))t[z].ch[t[z].ch[1]==y]=x;//只能在这一棵Splay里面Rotate
    t[x].ff=z;
    t[y].ch[k]=t[x].ch[k^1];
    t[t[x].ch[k^1]].ff=y;
    t[x].ch[k^1]=y;t[y].ff=x;
    pushup(y);pushup(x);
}
void splay(int x)
{
    sta[++top]=x;
    for(int i=x;!isroot(i);i=t[i].ff)sta[++top]=t[i].ff;
    while(top)pushdown(sta[top--]);
    while(!isroot(x))
    {
        int y=t[x].ff,z=t[y].ff;
        if(!isroot(y))//Splay是旋转到当前这棵Splay的根,这个道理就和上面的rotate是一样的.
            (t[y].ch[0]==x)^(t[z].ch[0]==y)?rotate(x):rotate(y);
        rotate(x);
    }
    pushup(x);
}
void access(int x)
{
    for(int y=0;x;y=x,x=t[x].ff)
    {
        splay(x);
        t[x].ch[1]=y;
        pushup(x);
    }
}
void makeroot(int x)
{
    access(x);splay(x);
    reverse(x);
}
int findroot(int x)
{
    access(x);splay(x);
    while(t[x].ch[0])x=t[x].ch[0];
    return x;
}
void split(int x,int y)
{
    makeroot(x);
    access(y);
    splay(y);
}
void link(int x,int y)
{
    makeroot(x);
    t[x].ff=y;
}
void cut(int x,int y)
{
    split(x,y);
    t[y].ch[0]=t[x].ff=0;
}
int judge(int x)
{
    return x>n?n+1:x;
}
int main()
{
    n=gi();
    for(int i=1;i<=n;i++)
        t[i].siz=1;
    for(int i=1;i<=n;i++)
        link(i,judge(i+(K[i]=gi())));
    int Q=gi();
    while(Q--)
    {
        int opt=gi(),u=gi();u++;
        if(opt==1)
        {
            makeroot(u);access(n+1);splay(n+1);
            printf("%d\n",t[n+1].siz-1);
        }
        if(opt==2)
        {
            int tmp=K[u];
            K[u]=gi();
            cut(u,judge(u+tmp));
            link(u,judge(u+K[u]));
        }
    }
    return 0;
}

分块

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std;
inline int gi(){
    int sum=0,f=1;char ch=getchar();
    while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    return f*sum;
}
const int N=400010;
int bl[N],nxt[N],sum[N],a[N];
int n,B,m;
void ask(int l,int r){
    for(int i=r;i>=l;i--)
        if(i+a[i]>min(n,bl[i]*B))sum[i]=1,nxt[i]=i+a[i];
        else sum[i]=sum[i+a[i]]+1,nxt[i]=nxt[i+a[i]];
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("in.in","r",stdin);
    freopen("out.out","w",stdout);
#endif
    n=gi();B=sqrt(n);
    for(int i=1;i<=n;i++)a[i]=gi();
    for(int i=1;i<=n;i++)
        bl[i]=(i-1)/B+1;
    ask(1,n);
    m=gi();int all=bl[n];
    while(m--){
        int opt=gi(),x=gi();x++;
        if(opt==1){
            int ans=sum[x],now=nxt[x],i=bl[x];
            while(now<=n && i<=all){ans+=sum[now];now=nxt[now];i++;}
            printf("%d\n",ans);
        }
        else{
            int k=gi();
            a[x]=k;
            ask((bl[x]-1)*B+1,min(bl[x]*B,n));
        }
    }
    return 0;
}

【BZOJ2002】 [Hnoi2010]Bounce 弹飞绵羊的更多相关文章

  1. BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 【LCT】【分块】

    BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始, ...

  2. [bzoj2002][Hnoi2010]Bounce弹飞绵羊_LCT

    Bounce弹飞绵羊 bzoj-2002 Hnoi-2010 题目大意:n个格子,每一个格子有一个弹簧,第i个格子会将经过的绵羊往后弹k[i]个,达到i+k[i].如果i+k[i]不存在,就表示这只绵 ...

  3. bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 [分块][LCT]

    Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...

  4. [BZOJ2002] [Hnoi2010] Bounce 弹飞绵羊 (LCT)

    Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...

  5. [bzoj2002][Hnoi2010]Bounce弹飞绵羊——分块

    Brief description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装 ...

  6. BZOJ2002: [Hnoi2010]Bounce 弹飞绵羊(LCT)

    Description 某天,Lostmonkey发明了一种超级弹力装置,为了在 他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装 ...

  7. bzoj2002 [Hnoi2010]Bounce 弹飞绵羊——分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 第一次用分块,感觉超方便啊: 如果记录每个点的弹力系数,那么是O(1)修改O(n)查询 ...

  8. bzoj2002 [Hnoi2010]Bounce 弹飞绵羊【分块】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 这一题除了LCT解法,还有一种更巧妙,代码量更少的解法,就是分块.先想,如果仅仅记录每 ...

  9. 题解【bzoj2002 [Hnoi2010]Bounce 弹飞绵羊】

    Description 给 \(n\) 个点以及它们的弹力系数 \(k_i\) ,含义为 可以弹到 \(i + k_i\) 的位置. 支持两个东西,修改一个点的弹力系数:求一个点要弹多少次弹出 \(n ...

  10. 【lct】bzoj2002 [Hnoi2010]Bounce 弹飞绵羊

    lct板子,此题主要有cut操作和link操作. #include<cstdio> #include<iostream> #include<cstring> #in ...

随机推荐

  1. SQL0668N 不允许对表"xxx"执行操作,原因码为 "1"

    使用db2 load导入30万条记录到某个表,成功后发现表被锁了,并显示: SQL0668N  不允许对表"xxx"执行操作,原因码为 "1" google了一 ...

  2. (转)用webbrowser做的网站登陆程序,如何获取cookie并且保存在程序中 (IE8有效) ,用途嘛,你懂的。

    今天帮朋友做了个工具,用webbrowser做的,用户使用用户名密码登陆网站后,需要在后台下载和分析一些页面. 分析页面使用的是htmlparser .net版 里面唯一需要解决的问题是,登陆后的co ...

  3. mybatis 为什么要设置jdbcType

    转载自:http://makemyownlife.iteye.com/blog/1610021 前天遇到一个问题 异常显示如下: 引用 Exception in thread "main&q ...

  4. 【Web】CSS实现绝对定位元素水平垂直居中

    网页中常常需用让绝对定位元素水平垂直居中,下面介绍2种方法: 一 元素宽度未知 <!DOCTYPE html> <html lang="en"> <h ...

  5. tensorflow下识别手写数字基于MLP网络

    # coding: utf-8 # In[1]: import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_da ...

  6. Le Chapitre IX

    Je crois qu'il profita, pour son évasion[evazjɔ̃]逃跑, d'une migration d'oiseaux sauvages[sovaʒ]未驯化的. ...

  7. (11)Are you a giver or a taker?

    https://www.ted.com/talks/adam_grant_are_you_a_giver_or_a_taker/transcript 00:00I want you to look a ...

  8. CodeForces 916B Jamie and Binary Sequence (changed after round) (贪心)

    题意:给定两个数字n,m,让你把数字 n 拆成一个长度为 m 的序列a1,a2,a3...am,并且∑2^ai = n,如果有多组,要求序列中最大的数最小,然后再相同就要求除了最大数字典序最大. 析: ...

  9. new命令简化的内部流程

    构造函数返回对象的一些问题: function fn(name,age){ this.name = name; this.age = age; //return 23; 忽略数字,直接返回原有对象 / ...

  10. Android 全局搜索条写成自定义控件-曹永思

    图文: 1.Android 自定义控件的布局文件 2.编写Android 自定义控件的要处理的逻辑代码(曹永思) 3.在调用自定义控件的 Activity的布局文件中调用Android 称之为控件,控 ...