九度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 题目描述: 二叉树的前序.中序.后序遍历 ...
随机推荐
- Hadoop2.6.0(2.4~2.7)完全分布式搭建-入门向 新手向 详细流程
.caret,.dropup>.btn>.caret{border-top-color:#000 !important}.label{border:1px solid #000}.tabl ...
- 学习总结 vs软件简单了解
using System;using System.Collections.Generic;using System.Linq;using System.Text;//调用命名空间 using Sys ...
- python字符串常用操作方法
python字符串操作常用操作,如字符串的替换.删除.截取.复制.连接.比较.查找.分割等,需要的朋友可以参考下. 1.去除空格str.strip():删除字符串两边的指定字符,括号的写入指定字符,默 ...
- check version cordova
cordova plugin add https://github.com/whiteoctober/cordova-plugin-app-version.git 这个插件很简单,但是要注意点的是:需 ...
- PAT1038. Recover the Smallest Number
//意识到一个重要错误,一直以为atoi,itoa是windows独有的,linux下不可用,直到刚刚... //string+=比strcat好用多了,字符比较也方便的多,但是用scanf读入str ...
- WWF3事件类型活动<第三篇>
WWF将工作流分为两大类: 面向Human:在工作流运行时通过用户对外部应用程序的操作来影响工作流的业务流转. 面向System:应用程序控制流程. 工作流与应用程序都是可以单独存在的,因此它们之间的 ...
- HTML中,按钮button与submit区别是什么?
type=button 单纯是按钮功能:type=submit 是发送表单:既然是这样,那么能否全部用submit代替button? Sumit提交表单,Button需要绑定事件才可以用提交数据不可以 ...
- tcp timestamp
Description Protocol suite: TCP/IP. Protocol type: Transport layer protocol. Option length: 10 bytes ...
- hbase查询,scan详解
一.shell 查询 hbase 查询相当简单,提供了get和scan两种方式,也不存在多表联合查询的问题.复杂查询需通过hive创建相应外部表,用sql语句自动生成mapreduce进行.但是这种简 ...
- 关于解决pyinstaller2.1将.py打包成exe文件在中文目录下不能执行的问题
关于解决pyinstaller2.1将.py打包成exe文件在中文目录下不能执行的问题 这个问题困扰我好久了,今天终于非常偶然的在http://www.v2ex.com/t/113856#reply1 ...