树型转线型。第一次听说这个概念. . . , 可是曾经已经接触过了,如LCA的预处理部分和树链剖分等。可是没想到还能这么用,三者虽说有不同可是大体思想还是非常相近的,学习了。

推荐博客http://blog.csdn.net/lyhypacm/article/details/6734748

转成线型之后。就变成了伸展树的模板题。

另外要注意,伸展树的特点是平均时间复杂度接近log(n)。所以一定要记得每次操作之后都要伸展。再次学习了。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map> #pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define _INF 0x3f3f3f3f
#define Mod 9999991
#define lowbit(x) (x&(-x)) using namespace std; const int MAXN = 50010; struct N
{
//info
int son[2],pre,ls,rs,s; //data
int id;
} st[MAXN*2]; int Top; void Updata(int root)
{
st[root].ls = (st[root].son[0] == -1 ? 0 : st[st[root].son[0]].s);
st[root].rs = (st[root].son[1] == -1 ? 0 : st[st[root].son[1]].s);
st[root].s = st[root].ls + st[root].rs + 1;
} void Push_Down(int root)
{
;
} void Rotate(int root,int dir)
{
st[st[root].pre].son[dir] = st[root].son[1^dir];
st[root].son[1^dir] = st[root].pre; if(st[st[st[root].pre].pre].son[0] == st[root].pre)
st[st[st[root].pre].pre].son[0] = root;
else
st[st[st[root].pre].pre].son[1] = root;
int temp = st[root].pre;
st[root].pre = st[st[root].pre].pre;
st[temp].pre = root; if(st[temp].son[dir] != -1)
st[st[temp].son[dir]].pre = temp;
Updata(temp);
Updata(root);
} int Splay(int root,int goal)
{
while(st[root].pre != goal)
{
Rotate(root,(st[st[root].pre].son[0] == root ? 0 : 1));
} return root;
} int Search_Site(int root,int site)
{
Push_Down(root); int temp; if(st[root].ls + 1 == site)
temp = root;
else if(st[root].ls + 1 < site)
temp = Search_Site(st[root].son[1],site-st[root].ls-1);
else
temp = Search_Site(st[root].son[0],site); Updata(root);
return temp;
} struct E
{
int v,next;
} edge[MAXN]; int head[MAXN]; int degree[MAXN]; int Top_E; void Link(int u,int v)
{
edge[Top_E].v = v;
edge[Top_E].next = head[u];
head[u] = Top_E++;
} int vis[MAXN*2]; int Top_S; int L[MAXN],R[MAXN]; void dfs(int root)
{
vis[Top_S++] = root; for(int p = head[root]; p != -1; p = edge[p].next)
{
dfs(edge[p].v);
} vis[Top_S++] = -root;
} void NewNode(int root,int id,int pre)
{
st[root].son[0] = -1,st[root].son[1] = -1;
st[root].pre = pre; st[root].id = id;
} void Init(int &root,int l,int r,int pre)
{
if(l > r)
return ; int mid = (l+r)>>1; root = Top++; NewNode(root,vis[mid],pre); if(vis[mid] > 0)
L[vis[mid]] = root;
else
R[-vis[mid]] = root;
Init(st[root].son[0],l,mid-1,root);
Init(st[root].son[1],mid+1,r,root); Updata(root);
} int Query(int v)
{
Splay(L[v],0);
return st[Search_Site(L[v],1)].id;
} void Move(int u,int v)
{
if(u == v)
return ;
int site = R[v]; Splay(R[u],0);
Splay(L[u],0); while(site)
{
if(st[site].pre == R[u] && st[R[u]].son[0] == site)
return ;
site = st[site].pre;
} if(st[L[u]].son[0] != -1)
{
int lt = st[L[u]].son[0];
int rt = st[R[u]].son[1]; st[L[u]].son[0] = -1;
st[R[u]].son[1] = -1; Updata(R[u]);
Updata(L[u]); st[lt].pre = 0; int root = Splay(Search_Site(lt,st[lt].s),0);
st[root].son[1] = rt;
st[rt].pre = root;
Updata(root);
} if(v)
{
Splay(L[v],0);
Splay(Search_Site(L[v],st[L[v]].ls+2),L[v]);
st[st[L[v]].son[1]].son[0] = L[u];
st[L[u]].pre = st[L[v]].son[1];
Updata(st[L[v]].son[1]);
Updata(L[v]);
}
} int main()
{
int n,m; int i,j,u,v; int root; bool blank = false; while(scanf("%d",&n) != EOF)
{
if(blank)
printf("\n");
else
blank = true;
Top_E = 0;
memset(head,-1,sizeof(int)*(n+2));
memset(degree,0,sizeof(int)*(n+2)); for(i = 1; i <= n; ++i)
{
scanf("%d",&u);
if(u)
{
Link(u,i);
degree[i]++;
}
} st[0].son[0] = -1,st[1].son[1] = -1; Top = 1; for(i = 1; i <= n; ++i)
{
if(degree[i] == 0)
{ Top_S = 1;
dfs(i); root = -1;
Init(root,1,Top_S-1,0);
}
} scanf("%d",&m);
L[0] = R[0] = 0;
char s[10];
while(m--)
{
scanf("%s",s);
if(s[0] == 'Q')
{
scanf("%d",&u);
printf("%d\n",Query(u)); }
else
{
scanf("%d %d",&u,&v);
Move(u,v);
}
}
}
return 0;
}

HDU 2475 Box 树型转线型 + 伸展树的更多相关文章

  1. HDU 2475 BOX 动态树 Link-Cut Tree

    Box Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem De ...

  2. [置顶] hdu 4699 2个栈维护 or 伸展树

    hdu 4699  Editor 题意:对一个数列进行操作,光标位置后面插入一个权值为x的数,删除光标前的那个数,光标左移一位,光标右移一位,求到k位置的最大的前缀和.. 注意这里的k是在光标之前的, ...

  3. hdu 2475 BOX (splay)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 2475 Splay树是一种神奇的东西... 题意: 有一些箱子,要么放在地上,要么放在某个箱子里面 . 现在有两种操作: (1) MOV ...

  4. php通用的树型类创建无限级树型菜单

    生成树型结构所需要的2维数组,var $arr = array()数组格式如下: array( 1 => array('id'=>'1','parentID'=>0,'name'=& ...

  5. HDU 2475 Box

    Box Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 247564 ...

  6. hdu 1754 I Hate It (splay tree伸展树)

    hdu 1754 I Hate It 其实我只是来存一下我的splay模板的..请大牛们多多指教 #include<stdio.h> #include<string.h> #i ...

  7. PHP算法 《树形结构》 之 伸展树(1) - 基本概念

    伸展树的介绍 1.出处:http://dongxicheng.org/structure/splay-tree/ A. 概述 二叉查找树(Binary Search Tree,也叫二叉排序树,即Bin ...

  8. poj_3468 伸展树

    题目大意 一个数列,每次操作可以是将某区间数字都加上一个相同的整数,也可以是询问一个区间中所有数字的和.(这里区间指的是数列中连续的若干个数)对每次询问给出结果. 思路 1. 伸展树的一般规律 对于区 ...

  9. poj_3580 伸展树

    自己伸展树做的第一个题 poj 3580 supermemo. 题目大意 对一个数组进行维护,包含如下几个操作: ADD x, y, d 在 A[x]--A[y] 中的每个数都增加d REVERSE ...

随机推荐

  1. 【前端福利】用grunt搭建自己主动化的web前端开发环境-完整教程

    jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用! 1. 前言 各位web前端开发者.假设你如今还不知道grunt或者听说过. ...

  2. 淘宝数据库OceanBase SQL编译器部分 源代码阅读--生成逻辑计划

    淘宝数据库OceanBase SQL编译器部分 源代码阅读--生成逻辑计划 SQL编译解析三部曲分为:构建语法树.生成逻辑计划.指定物理运行计划. 第一步骤,在我的上一篇博客淘宝数据库OceanBas ...

  3. POJ 题目3461 Oulipo(KMP)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26479   Accepted: 10550 Descript ...

  4. ZOJ 3633 Alice's present RMQ

     Alice's present Description As a doll master, Alice owns a wide range of dolls, and each of them ha ...

  5. oracle性能检测sql语句

    1. 监控事例的等待 select event,sum(decode(wait_Time,0,0,1)) "Prev",sum(decode(wait_Time,0,1,0)) & ...

  6. SQL语句之Insert

    插入常见的3种形式: 单条插入, 批量插入, 返回刚插入行的id http://www.cnblogs.com/yezhenhan/archive/2011/08/17/2142948.html

  7. git服务器搭建-gitosis

    需求 硬件需求:一台Ubuntu或者debian电脑(虚拟机),能通过网络访问到. 软件需求:git-core, gitosis, openssh-server, openssh-client, Ap ...

  8. php链接memcache操作

    设置值 set key 压缩标识 有效期 长度 set name 0 60 5 hello 压缩标识:用于告诉memcached服务器是否压所后存储数据,目的是为了节省磁盘空间,压所和解压缩会消耗时间 ...

  9. UWP 读取XML文件

    一.读取本地XML文件时要将xxx.xml文件的“生成操作”改为“嵌入的资源”会比较好,在手机上运行的话需要改为“内容” <?xml version="1.0" encodi ...

  10. [Reading] Asking while Reading

    Asking while Reading ——读Java垃圾收集器与内存分配策略 Java与C++之间有一堵由内存动态分配和垃圾收集技术所围成的“高墙”,墙外面的人想进去,墙里面的人却想出来. 为什么 ...