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. 【jpeg_Class 类】使用说明

    jpeg_Class类是针对图片操作类,可以获取图片属性.等比例缩略图片.裁切图片.图片上打印文字及打印水印等功能. 目录 原型 参数 返回 说明 Sub load(byVal path) path ...

  2. Linux环境下Java应用性能分析定位-CPU使用篇

    1     CPU热点分析定位背景 CPU资源还是很昂贵的,为了深刻感受到这种昂贵,间下图当前CPU的资源售价: 所以对于程序猿们来说,需要让程序合理高效的使用CPU资源.利用有限的CPU资源来解决完 ...

  3. 算法笔记(c++)--桶排序题目

    算法笔记(c++)--桶排序 记得题目是排序,输入n个1-1000的数字然后去重然后排序. 桶排序没毛病 #include<iostream> using namespace std; i ...

  4. JPA error org.hibernate.AnnotationException: No identifier specified for entity

    错误:org.hibernate.AnnotationException: No identifier specified for entity 原因:JPA所使用的Entity需要标注@Id,在引用 ...

  5. sshpass 指定密码远程 ssh 到服务器 或者 scp 发送文件到服务器

    在操作linux时,虽然可以对linux配置免秘钥登录,但是在配置免密码登录之前,是需要登录到其他节点主机的,这里提供一种类似ssh的方式,可以在命令后面加上相应的参数来设置你将要登录的远程主机的密码 ...

  6. Scrum立会报告+燃尽图(十一月十七日总第二十五次):设计调查问卷;修复上一阶段bug

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...

  7. VUE AXIOS 跨域问题

    背景: 后台跨域使用通配符:context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); ...

  8. 第一阶段android学习笔记

    1.学习<第一行代码> 第一个android项目: 项目的注意点,如创建项目时包名具有唯一性,在做项目的时候要手动改成Project模式.还知道了引用字符串的两种方式. AS项目的三种依赖 ...

  9. 缓存-MemoryCache Class

    这是使用MemoryCache缓存的一个例子. private void btnGet_Click(object sender, EventArgs e) { ObjectCache cache = ...

  10. 【Linux】- cat命令的源码历史

    转自:Cat 命令的源码历史 以前我和我的一些亲戚争论过计算机科学的学位值不值得读.当时我正在上大学,并要决定是不是该主修计算机.我姨和我表姐觉得我不应该主修计算机.她们承认知道如何编程肯定是很有用且 ...