HDU 2475 Box 树型转线型 + 伸展树
树型转线型。第一次听说这个概念. . . , 可是曾经已经接触过了,如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 树型转线型 + 伸展树的更多相关文章
- HDU 2475 BOX 动态树 Link-Cut Tree
Box Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) [Problem De ...
- [置顶] hdu 4699 2个栈维护 or 伸展树
hdu 4699 Editor 题意:对一个数列进行操作,光标位置后面插入一个权值为x的数,删除光标前的那个数,光标左移一位,光标右移一位,求到k位置的最大的前缀和.. 注意这里的k是在光标之前的, ...
- hdu 2475 BOX (splay)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 2475 Splay树是一种神奇的东西... 题意: 有一些箱子,要么放在地上,要么放在某个箱子里面 . 现在有两种操作: (1) MOV ...
- php通用的树型类创建无限级树型菜单
生成树型结构所需要的2维数组,var $arr = array()数组格式如下: array( 1 => array('id'=>'1','parentID'=>0,'name'=& ...
- HDU 2475 Box
Box Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 247564 ...
- hdu 1754 I Hate It (splay tree伸展树)
hdu 1754 I Hate It 其实我只是来存一下我的splay模板的..请大牛们多多指教 #include<stdio.h> #include<string.h> #i ...
- PHP算法 《树形结构》 之 伸展树(1) - 基本概念
伸展树的介绍 1.出处:http://dongxicheng.org/structure/splay-tree/ A. 概述 二叉查找树(Binary Search Tree,也叫二叉排序树,即Bin ...
- poj_3468 伸展树
题目大意 一个数列,每次操作可以是将某区间数字都加上一个相同的整数,也可以是询问一个区间中所有数字的和.(这里区间指的是数列中连续的若干个数)对每次询问给出结果. 思路 1. 伸展树的一般规律 对于区 ...
- poj_3580 伸展树
自己伸展树做的第一个题 poj 3580 supermemo. 题目大意 对一个数组进行维护,包含如下几个操作: ADD x, y, d 在 A[x]--A[y] 中的每个数都增加d REVERSE ...
随机推荐
- CC2540 与 CC2541 差别 1
CC2540 的 1234 PIN 是 USB 功能,4 PIN 是 USB 的电压输入引脚. CC2541 没有 USB 功能.它的 1234 PIN 是 I2C 功能,为了与 CC2540 引脚兼 ...
- 【Android 应用开发】Android 平台 HTTP网速測试 案例 API 分析
作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/25996817 工信部规定的网速測试标准 : 除普通网页測速 ...
- [寒江孤叶丶的Cocos2d-x之旅_33]RichTextEx一款通过HTML标签控制文字样式的富文本控件
RichTextEx一款通过HTML标签控制文字样式的富文本控件 原创文章,欢迎转载.转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列] 博客地址:http://blog.csdn.net ...
- audio_coding模块分析和audio_conference_mixer模块分析
audio_coding 1. 主要接口 AudioCodingModuleImpl::RegisterReceiveCodec 初始化Codec AudioCodingModul ...
- UVA 11346 - Probability 数学积分
Consider rectangular coordinate system and point L(X, Y ) which is randomly chosen among all pointsi ...
- Android安全攻防战,反编译与混淆技术全然解析(下)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/50451259 在上一篇文章其中,我们学习了Android程序反编译方面的知识,包括 ...
- Android持久化保存cookie
在解析网页信息的时候,需要登录后才能访问,所以使用httpclient模拟登录,然后把cookie保存下来,以供下一次访问使用,这时就需要持久化cookie中的内容. 在之前先科普一下基础知识: 什么 ...
- Swift - 判断是否有某功能访问权限,没有则提示,并自动跳转到设置页
由于 iOS 系统的安全限制,App 如果需要访问设备的通讯录.麦克风. 相册. 相机.地理位置等时,需要请求用户是否允许访问. 有时用户不小心点了“不允许”,后面可能就不知道要去哪里再开启这个权 ...
- NPAPI——实现非IE浏览器的类似ActiveX的本地程序(插件)调用
一.Netscape Plugin Interface(NPAPI) 大致的说明可以看下官方文档Plugin 本文主要针对于JavaScript与插件交互部分做一些交流,比如用于数字证书的操作(淘宝和 ...
- SPFA(Shortest Path Faster Algorithm)
特别说明 本文转载自三金(frinemore)的博客: 点这 前言 1.关于SPFA,它没死. 2.接下来的所有代码,都是自己手写的(未检查正确性,补充的代码有检查过,是对的),有错误请帮忙指出. S ...