HDU 4584 splay
Shaolin
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3021 Accepted Submission(s): 1273
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.
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.
2 1
3 3
4 2
0
3 2
4 2
#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的更多相关文章
- hdu 3436 splay树+离散化*
Queue-jumpers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- hdu 4453 splay
Looploop Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 3487 Splay
给定两种操作,一种是把一个数列的某一段切下来插到剩余数列的某一个位置上. 一种是翻转操作,把数列的某一段进行翻转. 都是Splay的基本操作.标准的Rotateto调整出 [a,b]区间.然后对[a, ...
- hdu 1890 splay树
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- HDU 3487 Splay tree
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1754 splay tree伸展树 初战(单点更新,区间属性查询)
题意:与区间查询点更新,点有20W个,询问区间的最大值.曾经用线段树,1000+ms,今天的伸展树,890没ms,差不多. 第一次学习伸展树,一共花了2个单位时间,感觉伸展树真很有用,也很好玩.现在只 ...
- hdu 4584 水题爽一发 *
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #i ...
- HDU 4584 Building bridges (水题)
Building bridges Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) ...
- HDU 4584
//也是简单题,因为n太小,故暴力之! #include<stdio.h> #include<math.h> #include<string.h> #define ...
随机推荐
- Scala基础知识笔记2
1 类 1.1 定义一个简单的类 1.2 field的getter 和 setter方法 感觉成员变量定义成 var 属性名=属性值即可, 不需要定义成 val 或者 private就行, // ...
- 实现短信超链接调起APP
因APP推广的需求,需要给APP用户定期发送短信提醒登录使用,为了更好的用户体验在短信内容中嵌入了可以直接打开APP的超链接,下面介绍一下具体的代码实现. 编辑openApp.html文件: < ...
- P,V操作及同步互斥实例
无论是计算机考研.计算机软件水平考试.计算机操作系统期末考试还是其他计算机岗位考试,P.V原语操作都是一个常考点.下面笔者总结了关于P.V操作的一些知识. 信号量是最早出现的用来解决进程同步与互斥问题 ...
- 网络流dinic模板,邻接矩阵+链式前向星
//这个是邻接矩阵的#include<iostream> #include<queue> #include<string.h> #include<stdio. ...
- 算法笔记(c++)--求一个数的所有质数因子
算法笔记(c++)--求一个数的所有质数因子 先贴题目: 这题不难,恶心在理解上面.最后看评论知道了怎么回事: 2*2*3*3*5=180 按照这逻辑的话应该输入的数由一系列质数相乘出来,所以每次找到 ...
- Activity 在横竖屏切换情况下的生命周期变化
title: Activity 在横竖屏切换情况下的生命周期变化 date: 2018-04-26 23:05:57 tags: [Activity] categories: [Mobile,Andr ...
- 1.openldap介绍
1.openldap介绍 OpenLDAP是轻型目录访问协议(Lightweight Directory Access Protocol,LDAP)的自由和开源的实现,在其OpenLDAP许可证下发行 ...
- Python20-Day01
简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释 编译型语言是一种以编译器来实现的编程语言,优缺点:执行速度快,调试麻烦 编译型语言:Java,Go,C,C++ 解释性 ...
- CS小分队第一阶段冲刺站立会议(5月11日)
昨日成果:完成了倒计时器的制作,为其添加了声音:并对扫雷游戏的失败添加了动态效果: 遇到的困难:把图片放入picturebox中无法改变图片的大小,音乐格式只能使用.wav,该格式音乐比较大,增加了整 ...
- 周总结<1>
由于都不清楚周总结的格式,所以就没有写了.不过,上次听了老师的课,觉得应该要好好写写了,至少今后可以明白自己有做过什么事情,至少不会觉得自己在各个方面没有收获.不过,可能没有按照格式来写.希望老师体谅 ...