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. sqoop安装与简单实用

    一,sqoop安装 1.解压源码包 2.配置环境变量 3.在bin目录下的 /bin/configsqoop 注释掉check报错信息 4.配置conf目录下 /conf/sqoop-env.sh 配 ...

  2. Python爬虫入门(1-2):综述、爬虫基础了解

    大家好哈,最近博主在学习Python,学习期间也遇到一些问题,获得了一些经验,在此将自己的学习系统地整理下来,如果大家有兴趣学习爬虫的话,可以将这些文章作为参考,也欢迎大家一共分享学习经验. Pyth ...

  3. PHP中的闭包详解

    PHP闭包(Closure)使用详解 作者: 字体:[增加 减小] 类型:转载 时间:2013-05-02我要评论 本篇文章介绍了,PHP闭包(Closure)的使用介绍,需要的朋友参考下   不知不 ...

  4. python中 try、except、finally 的执行顺序

        def test1(): try: print('to do stuff') raise Exception('hehe') print('to return in try') return ...

  5. 团队目标WBS及具体任务分工

    • 首先我们讨论了实验第一个冲刺周期要实现的功能,我们的初期目标. •  然后我们进一步梳理了第一阶段的任务和需求. •  之后对任务进行了划分和领取. •  最后每个人对自己的任务进行了估算,并约定 ...

  6. myeclipse和ecplise中安装git插件的问题

    我的myeclipse10.7和ecplise helis一直安装不了git插件,myeclipse中details说我的myeclipse少了team_features等之类文件,helis的情况大 ...

  7. (六)Jmeter重要组件的执行顺序及作用域

    一.Jmeter重要组件: 1)配置元件---Config Element: 用于初始化默认值和变量,以便后续采样器使用.配置元件大其作用域的初始阶段处理,配置元件仅对其所在的测试树分支有效,如,在同 ...

  8. Redis 备份数据的两种方式

    既然是数据库,那就一定有数据备份方式了,而且 Redis 是内存形式的数据库,更需要数据备份了,要不然断电数据就全都丢失了. Redis 数据备份有两种方式: RDB(数据快照) AOF(记录操作日志 ...

  9. 第130天:移动端-rem布局

    一.关于布局方案 当拿到设计师给的UI设计图,前端的首要任务就是布局和样式,相信这对于大部分前端工程师来说已经不是什么难题了.移动端的布局相对PC较为简单,关键在于对不同设备的适配.之前介绍了一篇关于 ...

  10. Solr实现SQL的查询与统计--转载

    原文地址:http://shiyanjun.cn/archives/78.html Cloudera公司已经推出了基于Hadoop平台的查询统计分析工具Impala,只要熟悉SQL,就可以熟练地使用I ...