标签:ids each bool sts java mem splay define earlier
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3021 Accepted Submission(s): 1273
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 }
标签:ids each bool sts java mem splay define earlier
原文地址:http://www.cnblogs.com/hsd-/p/6628698.html