【题目分析】

以前用分块的方法做过这道题目,现在再用LCT水一边,发现思路确实巧妙。

每次弹射,可以看作在一条边上走了过去,而且很重要的性质,每一个点的出边只有一条。

那么就很容易知道,可以用LCT维护连通性,然后把n+1这个虚点当作根,把起点旋转上去,然后n+1的深度就是结果的值。

更进一步,由于偏爱路径只是起点到n+1,深度又改为了子树大小size的查询。

均摊复杂度NlogN

【代码】

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>

#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>

using namespace std;

#define maxn 1000005
#define inf (0x3f3f3f3f)

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;
}

int fa[maxn],ch[maxn][2],siz[maxn],rev[maxn],top=0,sta[maxn];

bool isroot(int x)
{
    return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;
}

void update(int x)
{
    siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
}

void pushdown(int x)
{
    if (rev[x])
    {
        rev[x]^=1;
        rev[ch[x][0]]^=1;
        rev[ch[x][1]]^=1;
        swap(ch[x][0],ch[x][1]);
    }
}

void rot(int x)
{
    int y=fa[x],z=fa[y],l,r;
    if (ch[y][0]==x) l=0; else l=1;
    r=l^1;
    if (!isroot(y))
    {
        if (ch[z][0]==y) ch[z][0]=x;
        else ch[z][1]=x;
    }
    fa[x]=z; fa[y]=x; fa[ch[x][r]]=y;
    ch[y][l]=ch[x][r]; ch[x][r]=y;
    update(y); update(x);
}

void splay(int x)
{
    int top=0; sta[++top]=x;
    for (int i=x;!isroot(i);i=fa[i]) sta[++top]=fa[i];
    while (top) pushdown(sta[top--]);
    while (!isroot(x))
    {
        int y=fa[x],z=fa[y];
        if (!isroot(y))
        {
            if (ch[y][0]==x^ch[z][0]==y) rot(x);
            else rot(y);
        }
        rot(x);
    }
}

void access(int x)
{
    for (int t=0;x;t=x,x=fa[x])
    {
        splay(x);
        ch[x][1]=t;
        update(x);
    }
}

void makeroot(int x)
{
    access(x);
    splay(x);
    rev[x]^=1;
}

int find(int x)
{
    access(x);
    splay(x);
    while (ch[x][0]) x=ch[x][0];
    return x;
}

void link(int x,int y)
{
//  printf("link %d %d\n",x,y);
    makeroot(x);
    fa[x]=y;
//  update(x);update(y);
}

void cut(int x,int y)
{
//  printf("cut %d %d\n",x,y);
    makeroot(x);
    access(y);
    splay(y);
    ch[y][0]=fa[x]=0;
//  update(x);update(y);
}

int m,n,a[maxn];

int main()
{
    n=read(); for (int i=1;i<=n;++i) a[i]=read(),link(i,min(n+1,a[i]+i));
    m=read();
    while (m--)
    {
        int opt,x,y;
        opt=read();x=read(); x++;
        switch(opt)
        {
            case 1:
//              x=read();x++;
                makeroot(n+1);
                access(x);
                splay(x);
                printf("%d\n",siz[x]-1);
                break;
            case 2:
//              x=read();x++;
                y=read();
                cut(x,min(n+1,a[x]+x));
                a[x]=y;
                link(x,min(n+1,a[x]+x));
            break;
        }
    }
}

  

BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊 ——Link-Cut Tree的更多相关文章

  1. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...

  2. bzoj 2002 : [Hnoi2010]Bounce 弹飞绵羊 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 题面: 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: ...

  3. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 9071  Solved: 4652[Submi ...

  4. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...

  5. bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 動態樹

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 4055  Solved: 2172[Submi ...

  6. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 (动态树LCT)

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2843  Solved: 1519[Submi ...

  7. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 【分块】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 ...

  8. 【刷题】BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊

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

  9. BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊:分块

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意: 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆 ...

  10. 洛谷 P3203 BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊

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

随机推荐

  1. Httpsqs的安装以及安装过程错误的解决方法 转

    需求 :进行商品搜索的时候,要从索引中进行搜索,由于后台要更新商品和插入商品,当时考虑到了怎么来插入新的索引和更新索引的问题,通过讨论,大家决定用Httpsqs这个消息中间来通知插入新索引和删除索引最 ...

  2. ODBC连接发生错误:未发现数据源名称并且未指定默认驱动程序

    程序在使用ODBC方式连接数据库时发生错误: ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序. 什么原因造成的呢? 本人使用&l ...

  3. -[__NSCFNumber length]: unrecognized selector sent to instance 0xb0000000000000e3

    网络数据解析出现-[__NSCFNumber length]: unrecognized selector sent to instance 0xb0000000000000e3这样的错误,具体 re ...

  4. 使用Apache+Dreamweaver(或者H-builder)搭建php开发环境

    使用得工具说明 php+Apache服务器+Dreamweaver+mysql数据库 下载安装好wamp,可以在网上直接百度下载,为了方便,我给放个百度云的链接.wamp下载:链接:http://pa ...

  5. 移动端hrml模板

    <!DOCTYPE html><html><head> <title>时钟</title> <meta charset="u ...

  6. JavaScript基础——定义变量

    在JavaScript中使用变量来临时存储和访问来自JavaScript文件的数据.变量既可以指向简单的数据类型,如数字或者字符串:也可以指向更复杂的数据类型,比如对象. 在JavaScript中定义 ...

  7. ROW_NUMBER()函数的使用

    SQL Server数据库ROW_NUMBER()函数的使用是本文我们要介绍的内容,接下来我们就通过几个实例来一一介绍ROW_NUMBER()函数的使用. 实例如下: .使用row_number()函 ...

  8. JavaScript Canvas 根据像素点取位置

    <html> <body> <canvas id="canvas" width="100" height="100&qu ...

  9. at org.apache.jsp.index_jsp._jspInit(index_jsp.java:22) 报空指针

    原来发布到weblogic 的项目,想改动发布到tomcat上.启动发布一切都正常.出入项目请求路径却包错: java.lang.NullPointerException at org.apache. ...

  10. windows下Tomcat配置多实例

    详情参见tomcat安装目录下RUNNING.txt中Advanced Configuration - Multiple Tomcat Instances部分. 问题源于下面这段tomcat官方文档的 ...