Shaolin

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3021    Accepted Submission(s): 1273

Problem Description
Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The master of Shaolin evaluates a young man mainly by his talent on understanding the Buddism scripture, but fighting skill is also taken into account.
When a young man passes all the tests and is declared a new monk of Shaolin, there will be a fight , as a part of the welcome party. Every monk has an unique id and a unique fighting grade, which are all integers. The new monk must fight with a old monk whose fighting grade is closest to his fighting grade. If there are two old monks satisfying that condition, the new monk will take the one whose fighting grade is less than his.
The master is the first monk in Shaolin, his id is 1,and his fighting grade is 1,000,000,000.He just lost the fighting records. But he still remembers who joined Shaolin earlier, who joined later. Please recover the fighting records for him.
 
Input
There are several test cases.
In each test case:
The first line is a integer n (0 <n <=100,000),meaning the number of monks who joined Shaolin after the master did.(The master is not included).Then n lines follow. Each line has two integer k and g, meaning a monk's id and his fighting grade.( 0<= k ,g<=5,000,000)
The monks are listed by ascending order of jointing time.In other words, monks who joined Shaolin earlier come first.
The input ends with n = 0.
 
Output
A fight can be described as two ids of the monks who make that fight. For each test case, output all fights by the ascending order of happening time. Each fight in a line. For each fight, print the new monk's id first ,then the old monk's id.
 
Sample Input
3
2 1
3 3
4 2
0
 
Sample Output
2 1
3 2
4 2
 
Source
题意:求前驱后继板子题
题解:orz
 #include <bits/stdc++.h>
#define ll __int64
using namespace std;
ll tim=,n,root;
bool flag;
struct node
{
ll father,left,right,data;
ll id;
} tree[];
ll mins(ll aaa, ll bbb)
{
if(aaa<bbb)
return aaa;
else
return bbb;
}
ll abss(ll x)
{
if(x<)
return -x;
else
return x;
}
void rightrotate(ll x)
{
ll y=tree[x].father;
ll z=tree[y].father;
tree[y].left=tree[x].right;
if(tree[x].right!=-)
{
tree[tree[x].right].father=y;
}
tree[x].father=z;
if(z!=-)
{
if(tree[z].left==y) tree[z].left=x;
else tree[z].right=x;
}
tree[x].right=y;
tree[y].father=x;
}
void leftrotate(ll x)
{
ll y=tree[x].father;
ll z=tree[y].father;
tree[y].right=tree[x].left;
if(tree[x].left!=-)
{
tree[tree[x].left].father=y;
}
tree[x].father=z;
if(z!=-)
{
if(tree[z].left==y) tree[z].left=x;
else tree[z].right=x;
}
tree[x].left=y;
tree[y].father=x;
}
void splay(ll x)
{
while(tree[x].father!=-)
{
ll y=tree[x].father;
ll z=tree[y].father;
if(z==-)
{
if(tree[y].left==x) rightrotate(x);
else leftrotate(x);
}
else
{
if(tree[z].left==y&&tree[y].left==x)
{
rightrotate(y);
rightrotate(x);
}
else if(tree[z].left==y&&tree[y].right==x)
{
leftrotate(x);
rightrotate(x);
}
else if(tree[z].right==y&&tree[y].right==x)
{
leftrotate(y);
leftrotate(x);
}
else
{
rightrotate(x);
leftrotate(x);
}
}
}root=x;
}
ll qq(ll x)
{
ll y=tree[x].left;
if(y==-) return y;
while(tree[y].right!=-) {
y=tree[y].right;
}
return y;
}
ll hj(ll x)
{
ll y=tree[x].right;
if(y==-) return y;
while(tree[y].left!=-){
y=tree[y].left;
}
return y;
}
int BST_insert(ll idd,ll dat,ll x)
{
if(dat==tree[x].data)
{
flag=false ;
splay(x);
return ;
}
if(dat<tree[x].data)
{
if(tree[x].left==-)
{
tree[x].left=tim;
tree[tim].father=x;
tree[tim].left=tree[tim].right=-;
tree[tim].data=dat;
tree[tim].id=idd;
}
else
BST_insert(idd,dat,tree[x].left);
}
else
{
if(tree[x].right==-)
{
tree[x].right=tim;
tree[tim].father=x;
tree[tim].left=tree[tim].right=-;
tree[tim].data=dat;
tree[tim].id=idd;
}
else
BST_insert(idd,dat,tree[x].right);
}
}
ll insert1(ll idd,ll dat)
{
flag=true;
tim++;
BST_insert(idd,dat,root);
if(flag==false) return ;
splay(tim);
ll q=qq(tim);
ll h=hj(tim);
ll minx=;
ll iddd=;
if(q!=-) {
if(minx>abss(tree[q].data-dat)){
minx=abss(tree[q].data-dat);
iddd=tree[q].id;
}
}
if(h!=-) {
if(minx>abss(tree[h].data-dat)){
minx=abss(tree[h].data-dat);
iddd=tree[h].id;
}
}
printf("%I64d %I64d\n",idd,iddd);
}
int main()
{
int n;
ll aa=;
while(scanf("%d",&n)!=EOF)
{
if(n==)
return ;
tim=;
tim++;
tree[tim].father=-;
tree[tim].left=tree[tim].right=-;
tree[tim].data=;
tree[tim].id=;
root=tim;
for(ll i=; i<=n; i++)
{
ll aa=,bb;
scanf("%I64d %I64d",&aa,&bb);
insert1(aa,bb);
}
}
return ;
}

HDU 4584 splay的更多相关文章

  1. hdu 3436 splay树+离散化*

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  2. hdu 4453 splay

    Looploop Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  3. HDU 3487 Splay

    给定两种操作,一种是把一个数列的某一段切下来插到剩余数列的某一个位置上. 一种是翻转操作,把数列的某一段进行翻转. 都是Splay的基本操作.标准的Rotateto调整出 [a,b]区间.然后对[a, ...

  4. hdu 1890 splay树

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  5. HDU 3487 Splay tree

    Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. hdu 1754 splay tree伸展树 初战(单点更新,区间属性查询)

    题意:与区间查询点更新,点有20W个,询问区间的最大值.曾经用线段树,1000+ms,今天的伸展树,890没ms,差不多. 第一次学习伸展树,一共花了2个单位时间,感觉伸展树真很有用,也很好玩.现在只 ...

  7. hdu 4584 水题爽一发 *

    #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #i ...

  8. HDU 4584 Building bridges (水题)

    Building bridges Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) ...

  9. HDU 4584

    //也是简单题,因为n太小,故暴力之! #include<stdio.h> #include<math.h> #include<string.h> #define ...

随机推荐

  1. 小白初识 - 快速排序(QuickSort)

    我个人觉得快速排序和归并排序有相似之处,都是用到了分治的思想,将大问题拆分成若干个小问题. 不同的地方是归并排序是先把大问题拆分好了之后再排序,而快速排序则是一边拆分,一边排序. 快速排序的原理就是, ...

  2. ES6的新特性(1)——ES6 的概述

    ES6 的概述 首先,感谢马伦老师的ES6新特性的教程. ECMAScript 和 JavaScript 的关系是 ECMAScript 和 JavaScript 的关系是,前者是后者的规格,后者是前 ...

  3. “Hello World!”团队召开的第六次会议

    团队“Hello World!”团队召开的第六次会议. 博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.Todo List 六.会议照片 七.燃尽图 一.会议时间 2017年1 ...

  4. 01慕课网《进击Node.js基础(一)》Node.js安装,创建例子

    版本:偶数位为稳定版本,基数为非稳定版本 - 0.6.x - 0.7.x    - 0.8.x -0.9.x    -0.10.x  -0.11.x 概念:Node.js采用谷歌浏览器的V8引擎,用C ...

  5. python爬虫调用搜索引擎及图片爬取实战

    实战三-向搜索引擎提交搜索请求 关键点:利用搜索引擎提供的接口 百度的接口:wd="要搜索的内容" 360的接口:q="要搜索的内容" 所以我们只要把我们提交给 ...

  6. 软工1816 · Alpha冲刺(5/10)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 后端代码复审 福大各个食堂的菜品口味量化.属性标记 组织前后端线下协作 接下来 ...

  7. asp.netcore mvc 权限拦截

    1-背景介绍 需要做一个简单权限系统,基于 角色,用户,菜单 的模式 基于IActionFilter全局拦截,在内部跳转或者浏览器跳转的时候,拦截是成功的,当通过AJAX 请求的时候,页面就不会跳转 ...

  8. 读《it小小鸟》有感

    我一直认为大学就是一个自由的舒适的学习环境,没有人可以干扰你限制你,以至于我到了大学之后只剩下了颓废的生活.每天上课玩手机,下课玩电脑,吃饭叫外卖,从不去锻炼,周末就熬夜通宵,状态越来越差,导致我逐渐 ...

  9. 《我是一只IT小小鸟》 读书笔记

    <我是一只IT小小鸟>讲述了IT人员的成长经历,邀请了许多名IT行业的职员,学生,研究生写了自己的亲身经历和人生感悟,以书中可以看到我国IT行业的快速进步,以及看到IT员在这条道路上的坎坷 ...

  10. JAVA里面json和java对象之间的相互转换

    1. 把java 对象列表转换为json对象数组,并转为字符串 JSONArray array = JSONArray.fromObject(list);    String jsonstr = ar ...