九度oj 1541 二叉树
原题链接:http://ac.jobdu.com/problem.php?pid=1541
简答题如下:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
const int Max_N = ;
struct Node{
int v, s;
Node *pre, *ch[];
inline void set(int _v = , int _s = , Node *p = NULL){
v = _v, s = _s;
pre = ch[] = ch[] = p;
}
inline void push_up(){
s = ch[]->s + ch[]->s + ;
}
inline void link(Node *x, bool d){
ch[d] = x;
x->pre = this;
}
};
struct SplayTree{
Node *tail, *root, *null;
Node stack[Max_N], *ptr[Max_N];
void init(){
tail = &stack[];
null = tail++;
null->set();
root = null;
}
inline Node *newNode(int v){
Node *p = tail++;
p->set(v, , null);
return p;
}
inline void rotate(Node *x, int d){
Node *y = x->pre;
y->ch[!d] = x->ch[d];
if (x->ch[d] != null) x->ch[d]->pre = y;
x->pre = y->pre;
if (y->pre != null) y->pre->ch[y->pre->ch[] != y] = x;
x->ch[d] = y;
y->pre = x;
y->push_up(), x->push_up();
if (y == root) root = x;
}
inline void update(Node *x){
if (x != null){
update(x->ch[]);
update(x->ch[]);
x->push_up();
}
}
void gogo(int n){
int i, a, b;
for (i = ; i <= n; i++) ptr[i] = newNode(i);
for (i = ; i <= n; i++){
scanf("%d %d", &a, &b);
if (- == a || - == b){
if (- == a && b != -) ptr[i]->link(ptr[b], );
if (- == b && a != -) ptr[i]->link(ptr[a], );
} else {
ptr[i]->link(ptr[a], );
ptr[i]->link(ptr[b], );
}
ptr[i]->push_up();
if (ptr[i]->pre == null) root = ptr[i];
}
update(root);
}
inline void work(){
int i, t;
char buf[];
scanf("%d", &t);
while (t--){
scanf("%s %d", buf, &i);
if ('s' == buf[]){
printf("%d\n", ptr[i]->s);
} else if ('r' == buf[]){
if (ptr[i] == root) continue;
rotate(ptr[i], ptr[i]->pre->ch[] == ptr[i]);
} else {
if (ptr[i] == root) printf("-1\n");
else printf("%d\n", ptr[i]->pre->v);
}
}
}
}spt;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
while (~scanf("%d", &n)){
spt.init(), spt.gogo(n), spt.work();
}
return ;
}
九度oj 1541 二叉树的更多相关文章
- 九度OJ 1541 二叉树【数据结构】
题目地址:http://ac.jobdu.com/problem.php?pid=1541 题目描述: 旋转是二叉树的基本操作,我们可以对任意一个存在父亲节点的子节点进行旋转,包括如下几种形式(设被旋 ...
- 九度oj 1184 二叉树遍历
原题链接:http://ac.jobdu.com/problem.php?pid=1184 简单的二叉树重建,遍历. 如下: #include<cstdio> #include<cs ...
- [九度OJ]1078.二叉树的遍历(重建)
原题链接:http://ac.jobdu.com/problem.php?pid=1078 题目描述: 二叉树的前序.中序.后序遍历的定义:前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其 ...
- [九度OJ]1113.二叉树(求完全二叉树任意结点所在子树的结点数)
原题链接:http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3……组成了一颗特殊二叉树.我们已知这个二叉树的最后一个结点是n.现在 ...
- 九度OJ 1113 二叉树
题目地址:http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3……组成了一颗特殊二叉树.我们已知这个二叉树的最后一个结点是n.现在 ...
- 九度OJ 1078 二叉树遍历
题目地址:http://ac.jobdu.com/problem.php?pid=1078 题目描述: 二叉树的前序.中序.后序遍历的定义: 前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历 ...
- 九度oj 1521 二叉树的镜像
原题链接:http://ac.jobdu.com/problem.php?pid=1521 水题,如下.. #include<algorithm> #include<iostream ...
- 【九度OJ】题目1113:二叉树 解题报告
[九度OJ]题目1113:二叉树 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3-- ...
- 【九度OJ】题目1078:二叉树遍历 解题报告
[九度OJ]题目1078:二叉树遍历 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1078 题目描述: 二叉树的前序.中序.后序遍历 ...
随机推荐
- 吉布斯现象( Gibbs)
在连续傅里叶级数(或积分)变换中,信号所对应的离散频谱(或连续频谱)为(或),其频率是无限离散分布的(或频谱的分布范围是无限区间的).很显然,单位时间内,频率较低(简称低频,即较小)的简谐波相对频率较 ...
- Dinic
BFS构造分层网络,DFS多路增广 #include<iostream> #include<vector> #include<queue> #include< ...
- C# 扩展
“扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.” 这是msdn上说的,也就是你可以对String,Int,DataRow,DataTable等这些 ...
- 使用JS对HTML标签进行增删改查
以下为通过JS对li标签进行简单的增删改查: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" ...
- UIBezierPath IOS贝塞尔曲线
//记录 贝塞尔曲线使用 //根据一个矩形画曲线 + (UIBezierPath *)bezierPathWithRect:(CGRect)rect //根据矩形框的内切圆画曲线 + (UIBezi ...
- jquery学习记录
1.选择器实例 语法 描述 $(this) 当前 HTML 元素 $("p") 所有 <p> 元素 $("p.intro") 所有 class=&q ...
- trap在shell中捕捉信号
一.trap捕捉到信号之后,可以有三种反应方式:(1)执行一段程序来处理这一信号(2)接受信号的默认操作(3)忽视这一信号 二.trap对上面三种方式提供了三种基本形式:第一种形式的trap命令在sh ...
- CentOS 卸载已安装软件
根据软件的安装类型,选择合适的卸载方式: 卸载前确定软件的完整的版本号:#rpm -qa ×××*#rpm -ql xxx-xxx //查找安装目录 执行卸载命令:#rpm -e xxx-xxx # ...
- ios球体弹跳游戏源码
一款耐玩的ios游戏源码,画面上有很多小星星,球体落下的时候,你需要在画面上画出一条条的线条让球体弹跳起来然后吃掉小星星,如果没借助球体就失败了.游戏有很多关卡.注意: <ignore_js_o ...
- css3- border
css3-border 1.border-color 2.border-image 3.border-radius ( none | <length>{1,4} [ / <leng ...