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

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

2013ACM-ICPC杭州赛区全国邀请赛

题意:求前驱后继板子题

题解:orz

  1 #include <bits/stdc++.h>
  2 #define ll  __int64
  3 using namespace  std;
  4 ll tim=0,n,root;
  5 bool flag;
  6 struct node
  7 {
  8     ll father,left,right,data;
  9     ll id;
 10 } tree[100005];
 11  ll mins(ll aaa, ll bbb)
 12 {
 13     if(aaa<bbb)
 14         return aaa;
 15     else
 16         return bbb;
 17 }
 18  ll abss(ll x)
 19 {
 20     if(x<0)
 21         return -x;
 22     else
 23         return x;
 24 }
 25 void rightrotate(ll x)
 26 {
 27     ll y=tree[x].father;
 28     ll z=tree[y].father;
 29     tree[y].left=tree[x].right;
 30     if(tree[x].right!=-1)
 31     {
 32         tree[tree[x].right].father=y;
 33     }
 34     tree[x].father=z;
 35     if(z!=-1)
 36     {
 37         if(tree[z].left==y) tree[z].left=x;
 38         else tree[z].right=x;
 39     }
 40     tree[x].right=y;
 41     tree[y].father=x;
 42 }
 43 void leftrotate(ll x)
 44 {
 45     ll y=tree[x].father;
 46     ll z=tree[y].father;
 47     tree[y].right=tree[x].left;
 48     if(tree[x].left!=-1)
 49     {
 50         tree[tree[x].left].father=y;
 51     }
 52     tree[x].father=z;
 53     if(z!=-1)
 54     {
 55         if(tree[z].left==y) tree[z].left=x;
 56         else tree[z].right=x;
 57     }
 58     tree[x].left=y;
 59     tree[y].father=x;
 60 }
 61 void splay(ll x)
 62 {
 63     while(tree[x].father!=-1)
 64     {
 65         ll y=tree[x].father;
 66         ll z=tree[y].father;
 67         if(z==-1)
 68         {
 69             if(tree[y].left==x) rightrotate(x);
 70             else leftrotate(x);
 71         }
 72         else
 73         {
 74             if(tree[z].left==y&&tree[y].left==x)
 75             {
 76                 rightrotate(y);
 77                 rightrotate(x);
 78             }
 79             else if(tree[z].left==y&&tree[y].right==x)
 80             {
 81                 leftrotate(x);
 82                 rightrotate(x);
 83             }
 84             else if(tree[z].right==y&&tree[y].right==x)
 85             {
 86                 leftrotate(y);
 87                 leftrotate(x);
 88             }
 89             else
 90             {
 91                 rightrotate(x);
 92                 leftrotate(x);
 93             }
 94         }
 95     }root=x;
 96 }
 97 ll qq(ll x)
 98 {
 99     ll y=tree[x].left;
100     if(y==-1) return y;
101     while(tree[y].right!=-1) {
102             y=tree[y].right;
103     }
104     return y;
105 }
106 ll hj(ll x)
107 {
108     ll y=tree[x].right;
109     if(y==-1) return y;
110     while(tree[y].left!=-1){
111             y=tree[y].left;
112     }
113     return y;
114 }
115 int BST_insert(ll idd,ll dat,ll x)
116 {
117     if(dat==tree[x].data)
118     {
119         flag=false ;
120         splay(x);
121         return 0;
122     }
123     if(dat<tree[x].data)
124     {
125         if(tree[x].left==-1)
126         {
127             tree[x].left=tim;
128             tree[tim].father=x;
129             tree[tim].left=tree[tim].right=-1;
130             tree[tim].data=dat;
131             tree[tim].id=idd;
132         }
133         else
134             BST_insert(idd,dat,tree[x].left);
135     }
136     else
137     {
138         if(tree[x].right==-1)
139         {
140             tree[x].right=tim;
141             tree[tim].father=x;
142             tree[tim].left=tree[tim].right=-1;
143             tree[tim].data=dat;
144             tree[tim].id=idd;
145         }
146         else
147             BST_insert(idd,dat,tree[x].right);
148     }
149 }
150 ll insert1(ll idd,ll dat)
151 {
152     flag=true;
153     tim++;
154     BST_insert(idd,dat,root);
155     if(flag==false) return 0;
156     splay(tim);
157     ll q=qq(tim);
158     ll h=hj(tim);
159     ll minx=2000000000;
160     ll iddd=0;
161     if(q!=-1) {
162        if(minx>abss(tree[q].data-dat)){
163            minx=abss(tree[q].data-dat);
164            iddd=tree[q].id;
165        }
166     }
167     if(h!=-1) {
168         if(minx>abss(tree[h].data-dat)){
169            minx=abss(tree[h].data-dat);
170            iddd=tree[h].id;
171        }
172     }
173     printf("%I64d %I64d\n",idd,iddd);
174 }
175 int main()
176 {
177     int n;
178     ll aa=0;
179     while(scanf("%d",&n)!=EOF)
180     {
181     if(n==0)
182         return 0;
183     tim=0;
184     tim++;
185     tree[tim].father=-1;
186     tree[tim].left=tree[tim].right=-1;
187     tree[tim].data=1000000000;
188     tree[tim].id=1;
189     root=tim;
190     for(ll i=1; i<=n; i++)
191     {
192         ll aa=0,bb;
193         scanf("%I64d %I64d",&aa,&bb);
194         insert1(aa,bb);
195     }
196     }
197     return 0;
198 }
时间: 2024-12-21 00:06:18

HDU 4584 splay的相关文章

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

题意:与区间查询点更新,点有20W个,询问区间的最大值.曾经用线段树,1000+ms,今天的伸展树,890没ms,差不多. 第一次学习伸展树,一共花了2个单位时间,感觉伸展树真很有用,也很好玩.现在只学了一点点.切个点更新试试. 大致思路:用编号(数组)作为树的键值建树,每插一个数,沿路节点更新最大值(每个结点有一个附加信息标记以之为子树的树所有点的最大值).所以,查询时[i,j],只要把i-1伸展到树根,把j+1伸展到I-1下面,那么j+1的左子树就是要的区间了!查该子树根值信息即可(特判端点

HDU 1890 Splay区间翻转

D - Robotic Sort Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1890 Appoint description:  System Crawler  (2014-11-27) Description Somewhere deep in the Czech Technical University buildings, t

HDU 3487 Splay tree

Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6779    Accepted Submission(s): 2678 Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamond

HDU 4453 (splay 插入删除翻转区间加单点查)

//白色上的模板,先静态申请结构体数组,再动态使用,时间应该更快:还有个小技巧,它的空指针用真实的null指针代替,这样即使访问了null的内容也没关系,减少出错的可能性 #include<cstdio> #include<algorithm> #include<vector> using namespace std; struct Node { Node *ch[2]; int s; int flip; int v; int add; int cmp(int k) c

HDU 1890(splay tree 区间翻转)

前后折腾了一天半时间才搞定..从学习lazy到理解代码..—_—|| 题意是说每次把第i大的数所在位置和第i个位置之间翻转,输出每个数在翻转前的位置. 首先我们要想到,在splay tree 中,对于根节点来说,左子树的大小+1就是它在数组中的位置(从1开始标号),左子树的各元素也是在数列中位于根结点左边的 我们现在用splay tree来维护未排序的序列,那对于题目要求的翻转操作来说,对象就仅仅是根节点的左子树了 我们要做的事情可以抽象成这样:每次把第i大的结点旋转至根,再把根节点的左子树打上

hdu 3487 splay

切割的话就split再merge,区间修改就splay然后lazy标记. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 300000; 7 int ans[N]; 8 int n, m, cnt; 9 10 struct Node 11 { 12 Node * ch[2]; 13 int val; 14 int

HDU 3487 splay区间翻转切割

Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3986    Accepted Submission(s): 1633 Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamon

HDU 4584 Building bridges

题意: 思路: #include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<stdlib.h> #include<vector> #include<queue> #include<stack> #include<algorithm> using namespace std; const int MAXN

hdu 4584 水题爽一发 *

1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #define MOD 1000000007 10 const int INF=0x3f3f3f3f; 11