Tim's Programming Space  
Tim's Programming Space
日历
<2010年7月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
统计
  • 随笔 - 20
  • 文章 - 1
  • 评论 - 40
  • 引用 - 0

导航

常用链接

留言簿(3)

随笔档案

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 
听说有版权问题不能贴题目?。那就只能先忍一忍了。
题目抽象为:我们有一个由有根树构成的森林,对这个森林进行两种操作:
把某棵子树拔下来接到某一棵树(可能还是那个子树原来所在的树)的某个节点下面,询问某个节点在树中的深度。

因为把一棵边权为1的树的括号序列拿出来,树上某两点的距离就是在括号序列中两点间没匹配括号的个数(有左括号右括号选择的区别,具体分析处理)。当然,既然是对一群树操作,那就直接用动态树就行了。

于是就去学了动态树。发现其实不算很难(1.指时间复杂度均摊logn的算法,还有基于轻重边剖分的严格logn的算法 2.如果你对splay熟的话),写起来也就基本上就是一棵splay,也算比较好写的。。(以后就告别路径剖分了。。太麻烦了。。复杂度也没动态树好。。)

以下所说的动态树都是基于splay的时间复杂度均摊logn的动态树。

动态树的主要思想就是:类似轻重边剖分一样,把整棵树划分成若干实边(solid edge)和虚边(dashed edge),但这个都是根据你的需要来设定的,不像轻重边一样每个点往下都必须有一条重边(单独的叶子节点算长度为0的重边),而是每次把你所需要操作的点到根的边都改为实边(expose操作),且每个点往下的实边数不超过1。修改沿途如果有一个点已经有了实边边那么就把它原来的实边改成虚边。这样每次对一个点操作都是在一条实路径上(solid path)。对于每一条实路径,都用一棵splay来维护就行了。(splay可以乱转乱拔乱接太爽了。。- -!当然是在一定规则下的乱。。)


/*
 * $File: bounce.cpp
 * $Date: Fri Jul 09 20:59:27 2010 +0800
 * $Author: Tim
 * $Solution: Dynamic Tree with Splay Tree implementation
 * $Time complexity: O(mlogn) , per operation amorized O(logn);
 
*/

#include 
<cstdio>
#include 
<cstdlib>
#include 
<cstring>
#include 
<cassert>

#define MAXN 200005

using namespace std;


class SplayNode
{
    
public:
        
int fa, lt, rt, size;
};
SplayNode node[MAXN 
+ 1];

// functions below are belong to splay tree
// we can see that, this splay tree is quite
// simple, and just 'splay' function
// and size maintaining supported.
// but that what all we need to
// solve this problem

void Renew(int x)
{
    
if (!x)
        
return;
    node[x].size 
= node[node[x].lt].size + node[node[x].rt].size + 1;
}
void RightRotate(int x)
{
    
int lc = node[x].lt, fa = node[x].fa;
    node[x].lt 
= node[lc].rt; node[node[x].lt].fa = x;
    node[lc].rt 
= x; node[x].fa = lc;
    node[lc].fa 
= fa;
    
if (x == node[fa].lt)
        node[fa].lt 
= lc;
    
else
        node[fa].rt 
= lc;
    Renew(x);
    Renew(lc);
}


void LeftRotate(int x)
{
    
int rc = node[x].rt, fa = node[x].fa;
    node[x].rt 
= node[rc].lt; node[node[x].rt].fa = x;
    node[rc].lt 
= x; node[x].fa = rc;
    node[rc].fa 
= fa;
    
if (x == node[fa].lt)
        node[fa].lt 
= rc;
    
else
        node[fa].rt 
= rc;
    Renew(x);
    Renew(rc);
}

void splay(int x, int FA = 0)
{
    
int fa, Fa;
    
while ((fa = node[x].fa) != FA)
    {
        
if ((Fa = node[fa].fa) == FA)
        {
            
if (x == node[fa].lt)
                RightRotate(fa);
            
else
                LeftRotate(fa);
        }
        
else
        {
            
if (x == node[fa].lt)
            {
                
if (fa == node[Fa].lt)
                {
                    RightRotate(Fa);
                    RightRotate(fa);
                }
                
else
                {
                    RightRotate(fa);
                    LeftRotate(Fa);
                }
            }
            
else
            {
                
if (fa == node[Fa].rt)
                {
                    LeftRotate(Fa);
                    LeftRotate(fa);
                }
                
else
                {
                    LeftRotate(fa);
                    RightRotate(Fa);
                }
            }
        }
    }
}

// end splay

int root;
int query_rank(int id)
{
    splay(id);
    
return node[node[id].lt].size + 1;
}
int father[MAXN + 1];
int n;
void Init()
{
    scanf(
"%d"&n);
    
for (int i = 1, k; i <= n; i ++)
    {
        scanf(
"%d"&k);
        k 
+= i;
        
if (k > n + 1)
            k 
= n + 1;
        father[i] 
= k;
        node[i].size 
= 1;
    }
    node[n 
+ 1].size = 1;
}

int split(int id) 
    
// isolate id and the node right after it on the solid path 
    
// and return that node
{
    splay(id);
    
if (node[id].rt)
    {
        
int rc = node[id].rt;
        node[id].rt 
= node[rc].fa = 0;
        node[id].size 
-= node[rc].size;
        
return rc;
    }
    
else
        
return 0;
}

void Link(int id, int fa) 
    
// let fa be the father of id, 
    
// we assume that before this, 
    
// id is the head of a solid path,
    
// and fa is the tail of a solid path,
    
// this was done by function 'cut' and 'split'
{
    splay(id);
    assert(
!node[id].lt);
    splay(fa);
    assert(
!node[fa].rt);
    node[fa].rt 
= id;
    node[fa].size 
+= node[id].size;
    node[id].fa 
= fa;
}

int get_head(int x)
    
// get the head of the solid path which x is in.
{
    
while (node[x].fa)
        x 
= node[x].fa;
    
while (node[x].lt)
        x 
= node[x].lt;
    splay(x);
    
return x;
}

void expose(int id) 
    
// turn the edges between id and the root of the tree id is in
    
// all into solid edges. with this operation, we can query what
    
// we want conveniently in a splay tree.
{
    
while (true)
    {
        id 
= get_head(id);
        
if (!father[id])
            
break;
        split(father[id]);
        Link(id, father[id]);
    }
}

int query_depth(int id)
{
    expose(id);
    
return query_rank(id) - 1;
}

void cut(int id)
    
// this function isolated the subtree rooted id
{
    expose(id);
    split(father[id]);
}

void modify_father(int id, int fa)
{
    cut(id);
    split(fa);
    father[id] 
= fa;
    Link(id, fa);
}

void Solve()
{
    
int m, cmd, id, k;
    scanf(
"%d"&m);
    
while (m --)
    {
        scanf(
"%d%d"&cmd, &id);
        id 
++;
        
if (cmd == 1)
            printf(
"%d\n", query_depth(id));
        
else
        {
            scanf(
"%d"&k);
            k 
+= id;
            
if (k > n + 1)
                k 
= n + 1;
            modify_father(id, k);
        }
    }
}

int main()
{
    freopen(
"bounce.in""r", stdin);
    freopen(
"bounce.out""w", stdout);
    Init();
    Solve();
    
return 0;
}


posted on 2010-07-09 21:00 TimTopCoder 阅读(3165) 评论(5)  编辑 收藏 引用
评论:
  • # re: HNOI2010-弹飞绵羊-(splay维护括号序列 或 动态树)  treeboy Posted @ 2010-07-10 08:53
    弱弱地问下:怎么把文章放在首页,我是新用户  回复  更多评论   

  • # re: HNOI2010-弹飞绵羊-(splay维护括号序列 或 动态树)  zxytim Posted @ 2010-07-10 09:49
    @treeboy
    发表的时候选择 首页精华区。。
    但你要保证你的文章大家看了会受益。。  回复  更多评论   

  • # re: HNOI2010-弹飞绵羊-(splay维护括号序列 或 动态树)[未登录]  treeboy Posted @ 2010-07-10 14:21
    @zxytim
    博主,我的意思是把文章放在自己的首页,如我的www.cppblog.com/treeboy
    谢谢  回复  更多评论   

  • # re: HNOI2010-弹飞绵羊-(splay维护括号序列 或 动态树)  zxytim Posted @ 2010-07-10 16:20
    哦。。。
    点 发新随笔 不要点 发新文章。。。  回复  更多评论   

  • # re: HNOI2010-弹飞绵羊-(splay维护括号序列 或 动态树)  cnx Posted @ 2010-07-27 17:17
    求数据!bobchennan@163.com  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


 
Copyright © TimTopCoder Powered by: 博客园 模板提供:沪江博客