树型转线型。第一次听说这个概念. . . , 可是曾经已经接触过了,如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. Android 自己定义View学习(2)

    上一篇学习了基本使用方法,今天学一下略微复杂一点的.先看一下效果图 为了完毕上面的效果还是要用到上一期开头的四步 1,属性应该要有颜色,要有速度 <?xml version="1.0& ...

  2. thinkPHP 空模块和空操作、前置操作和后置操作 具体介绍(十四)

    本章节:介绍 TP 空模块和空操作.前置操作和后置操作 具体介绍 一.空模块和空操作 1.空操作 function _empty($name){ $this->show("$name ...

  3. Codeforces Round #282 (Div. 1)B. Obsessive String KMP+DP

    B. Obsessive String   Hamed has recently found a string t and suddenly became quite fond of it. He s ...

  4. oracle如何创建数据库

    第一步: 从Windows桌面执行“开始”→“Database Configuration Assistant”命令,打开Database Configuration Assistant对话框的欢迎界 ...

  5. poj2947Widget Factory

    对于同余方程的高斯消元啊. 其实也差不多吧.先同一位通分,然后减一下就好了. 主要是判无解和多解的麻烦,需要注意即使有自由元也可能先无解 #include<cstdio> #include ...

  6. 剑指offer——03从尾至头打印列表(Python3)

    思路:相当于数据结构中的链表就地逆置,可以使用头插法来实现. 代码: class Solution:     # 返回从尾部到头部的列表值序列,例如[1,2,3]     def printListF ...

  7. python中数字的排序

    lst = [2,22,4,7,18]for j in range(len(lst)): #记录内部排序的次数 i = 0 while i < len(lst)-1: if lst[i] > ...

  8. WebView简单用法

    1.空布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andr ...

  9. Hello World Spring MVC

    1, Setup Development Environment 1.1, Java SDK | ~ @ yvan-mac (yvan) | => java -version java vers ...

  10. JS 数值转换、加减乘除

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...