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.
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.
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
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 ",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 }