- 1#include
- 2#include
- 3#include
- 4#include
- 5#include 6
- 7 #definesiz 1024 8
- 9inlineintget_c(void)
- 10 {
- 11 static char buf[siz];
- 12 static char*head = buf + siz;
- 13 static char*tail = buf + siz;
- 14
- 15 if(head == tail)
- 16fread(head = buf,1, siz, stdin);
- 17
- 18 return*head++;
- 19 }
- 20
- 21inlineintget_i(void)
- 22 {
- 23registerintret =0;
- 24registerintneg =false;
- 25registerintbit = get_c();
- 26
- 27 for(; bit <48; bit = get_c())
- 28 if(bit =='-')neg ^=true;
- 29
- 30 for(; bit >47; bit = get_c())
- 31ret = ret *10+ bit -48;
- 32
- 33 returnneg ? -ret : ret;
- 34 }
- 35
- 36 #definemaxn 205 37
- 38 int n, ans[maxn];
- 39
- 40 struct node
- 41 {
- 42node *lson;
- 43node *rson;
- 44node *father;
- 45
- 46node(void)
- 47 {
- 48lson = NULL;
- 49rson = NULL;
- 50father = NULL;
- 51 }
- 52
- 53inlinevoidswap(void)
- 54 {
- 55 staticnode *temp;
- 56
- 57temp = lson;
- 58lson = rson;
- 59rson = temp;
- 60 }
- 61}tree[maxn], *root = tree;
- 62
- 63inlineintlast(void)
- 64 {
- 65node *t = root;
- 66
- 67 while(t->rson)
- 68t = t->lson;
- 69
- 70 if(t->lson && !t->lson->lson)
- 71t = t->lson;
- 72
- 73 if(t == root)
- 74root = t->lson;
- 75 else
- 76t->father->lson = t->lson;
- 77
- 78 if(t->lson)
- 79t->lson->father = t->father;
- 80
- 81 for(node *p = t->father; p; p = p->father)
- 82p->swap();
- 83
- 84 return int(t - tree);
- 85 }
- 86
- 87signed main(void)
- 88 {
- 89n = get_i();
- 90
- 91 for(inti =1; i <= n; ++i)
- 92 {
- 93 intfa = get_i();
- 94
- 95 if(fa <100)
- 96tree[i].father = tree + fa, tree[fa].lson = tree + i;
- 97 elsefa -=100,
- 98tree[i].father = tree + fa, tree[fa].rson = tree + i;
- 99 }
- 100
- 101 for(inti = n; i >=0; --i)ans[i] = last();
- 102
- 103 for(inti =0; i <= n; ++i)printf("%d ", ans[i]);
- 104
- 105 //system("pause");
- 106}
来源: