原题链接: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 二叉树的更多相关文章

  1. 九度OJ 1541 二叉树【数据结构】

    题目地址:http://ac.jobdu.com/problem.php?pid=1541 题目描述: 旋转是二叉树的基本操作,我们可以对任意一个存在父亲节点的子节点进行旋转,包括如下几种形式(设被旋 ...

  2. 九度oj 1184 二叉树遍历

    原题链接:http://ac.jobdu.com/problem.php?pid=1184 简单的二叉树重建,遍历. 如下: #include<cstdio> #include<cs ...

  3. [九度OJ]1078.二叉树的遍历(重建)

    原题链接:http://ac.jobdu.com/problem.php?pid=1078 题目描述: 二叉树的前序.中序.后序遍历的定义:前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其 ...

  4. [九度OJ]1113.二叉树(求完全二叉树任意结点所在子树的结点数)

    原题链接:http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3……组成了一颗特殊二叉树.我们已知这个二叉树的最后一个结点是n.现在 ...

  5. 九度OJ 1113 二叉树

    题目地址:http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3……组成了一颗特殊二叉树.我们已知这个二叉树的最后一个结点是n.现在 ...

  6. 九度OJ 1078 二叉树遍历

    题目地址:http://ac.jobdu.com/problem.php?pid=1078 题目描述: 二叉树的前序.中序.后序遍历的定义: 前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历 ...

  7. 九度oj 1521 二叉树的镜像

    原题链接:http://ac.jobdu.com/problem.php?pid=1521 水题,如下.. #include<algorithm> #include<iostream ...

  8. 【九度OJ】题目1113:二叉树 解题报告

    [九度OJ]题目1113:二叉树 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3-- ...

  9. 【九度OJ】题目1078:二叉树遍历 解题报告

    [九度OJ]题目1078:二叉树遍历 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1078 题目描述: 二叉树的前序.中序.后序遍历 ...

随机推荐

  1. ubuntu server unable to resolve host

    cat /etc/resolv.conf (查看resolv.conf中的内容: nameserver 是动态添加的……) #通过添加/etc/resolvconf/resolv.conf.d/bas ...

  2. ZoneMinder配置与使用

    ZoneMinder是一套基于Linux操作系统的摄像机的视像数据监控的应用软件.应用范围广泛,包括商业或家居防盗等.ZoneMinder支持单一或多台视像镜头应用,包括摄取.分析.记录.和监视来源, ...

  3. Python 编程规范-----转载

    Python编程规范及性能优化 Ptyhon编程规范 编码 所有的 Python 脚本文件都应在文件头标上 # -*- coding:utf-8 -*- .设置编辑器,默认保存为 utf-8 格式. ...

  4. ionic cordova social media sharing plugin

    https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin.git 从上面的连接下载 插件 1.肯定是要加入 下面的那个文件的吖 从 ...

  5. 自适应中overflow的作用

    最近在做东西的时候发现overflow还有这样的妙处:可以实现自适应,之前没加overflow实现起来是有点问题的 代码如下: <!DOCTYPE html><html> &l ...

  6. Bootstrap <第一篇>

    一.使用Bootstrap要引用的文件 要使用Bootstrap,基本架构要引用如下文件: <link href="bootstrap.min.css" rel=" ...

  7. C#导出

    #region  DataReader 的数据导出到Excle 中        //public string Exports(string str_sql)        //{        / ...

  8. JS常用的设计模式(11)—— 中介者模式

    中介者对象可以让各个对象之间不需要显示的相互引用,从而使其耦合松散,而且可以独立的改变它们之间的交互. 打个比方,军火买卖双方为了安全起见,找了一个信任的中介来进行交易.买家A把钱交给中介B,然后从中 ...

  9. leetcode 67

    67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...

  10. 实现在Android简单封装类似JQuery异步请求

    在android开发中经常会使用异步请求数据,通常会使用handler或者AsyncTask去做,handler 配合message 使用起来比较麻烦,AsyncTask 线程池只允许128个线程工作 ...