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 ...
随机推荐
- 天下武功,无快不破,Python开发必备的6个库
01 Python 必备之 PyPy PyPy 主要用于何处? 如果你需要更快的 Python 应用程序,最简单的实现的方法就是通过 PyPy ,Python 运行时与实时(JIT)编译器.与使用普通 ...
- datax 执行流程分析
https://www.jianshu.com/nb/29319571 https://www.jianshu.com/p/b10fbdee7e56
- 《图解 HTTP 》阅读 —— 第三章
第3章 HTTP 报文内的 HTTP 信息 用于 HTTP 协议交互的信息称为 HTTP 报文:请求报文和响应报文 HTTP 在传输数据时通过编码可以提升速率,能有效的处理大量数据,但是会消耗更多的C ...
- .net mvc5 不同view()的视图 代码
public class Test { public int id { set; get; } public string name { set; get; } } public ActionResu ...
- linux +redis 安装 +mongo 安装
Linux 下redis安装 本教程使用的最新文档版本为 2.8.17,下载并安装: $ wget http://download.redis.io/releases/redis-2.8.17.tar ...
- 6/2 sprint2 看板和燃尽图的更新
- 性能测试工具Loadrunner使用经验小结(原创更新版)
1. 引言 1.1. 简介 loadrunner是一种预测系统行为和性能的负载测试工具,它可以轻松创建虚拟用户.创建真实的负载.定位性能问题.重复测试保证系统的高性能 globa-100的注册码:AE ...
- 在selenium测试中使用XPATH功能函数starts-with、contains、descendant、ancestor、text()定位网页元素
项目中一些使用xpath函数的复杂例子,记录于此 1. 使用starts-with //div[starts-with(@id,'res')]//table//tr//td[2]//table//tr ...
- 为何php curl post模式发送数据速度变慢了?我来说说原因
事例: 今天要向一台服务器上传文件,原版是curl的get模式,现在改用了post模式,按照原本的思想,代码如下 <?php $post['c'] = 'config'; $post['t'] ...
- UVALive6434_Number Assignment
简单dp题. 这样的,意思为给你n个数,要你现在将这n个数分为m组,使得所有组内最大值与最小值的差的和最小. 其实可以这样来考虑这个问题,首先可以把所有的数字从小到大排个序,显然如果有一种取法是最优的 ...