|
| 1 | +#include <cstdio> |
| 2 | +#include <cstdlib> |
| 3 | +#include <ctime> |
| 4 | +#include <utility> |
| 5 | + |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +struct node |
| 9 | +{ |
| 10 | + node *lch, *rch; |
| 11 | + int key, pr; |
| 12 | + node(int _key); |
| 13 | +}*root, *nil; |
| 14 | + |
| 15 | +node::node(int _key = 0) : lch(nil), rch(nil), key(_key), pr(rand()) { } |
| 16 | + |
| 17 | +void init() |
| 18 | +{ |
| 19 | + nil = new node; |
| 20 | + root = nil; |
| 21 | +} |
| 22 | + |
| 23 | +pair<node*, node*> split(node *u, int x) |
| 24 | +{ |
| 25 | + if (u == nil) |
| 26 | + { |
| 27 | + return make_pair(nil, nil); |
| 28 | + } |
| 29 | + if (x <= u->key) |
| 30 | + { |
| 31 | + pair<node*, node*> res = split(u->lch, x); |
| 32 | + u->lch = res.second; |
| 33 | + return make_pair(res.first, u); |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + pair<node*, node*> res = split(u->rch, x); |
| 38 | + u->rch = res.first; |
| 39 | + return make_pair(u, res.second); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +node* merge(node *u, node *v) |
| 44 | +{ |
| 45 | + if (u == nil) |
| 46 | + { |
| 47 | + return v; |
| 48 | + } |
| 49 | + else if (v == nil) |
| 50 | + { |
| 51 | + return u; |
| 52 | + } |
| 53 | + if (u->pr > v->pr) |
| 54 | + { |
| 55 | + u->rch = merge(u->rch, v); |
| 56 | + return u; |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + v->lch = merge(u, v->lch); |
| 61 | + return v; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +int find(node *u, int x) |
| 66 | +{ |
| 67 | + if (u == nil) |
| 68 | + { |
| 69 | + return 0; |
| 70 | + } |
| 71 | + if (u->key == x) |
| 72 | + { |
| 73 | + return 1; |
| 74 | + } |
| 75 | + if (x < u->key) |
| 76 | + { |
| 77 | + return find(u->lch, x); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + return find(u->rch, x); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +int find(int x) |
| 86 | +{ |
| 87 | + return find(root, x); |
| 88 | +} |
| 89 | + |
| 90 | +void insert(int x) |
| 91 | +{ |
| 92 | + if (find(root, x) == 1) |
| 93 | + { |
| 94 | + return; |
| 95 | + } |
| 96 | + node *u = new node(x); |
| 97 | + pair<node*, node*> rts = split(root, x); |
| 98 | + rts.first = merge(rts.first, u); |
| 99 | + root = merge(rts.first, rts.second); |
| 100 | +} |
| 101 | + |
| 102 | +void print_tree(node *u) |
| 103 | +{ |
| 104 | + if (u != nil) |
| 105 | + { |
| 106 | + printf("("); |
| 107 | + print_tree(u->lch); |
| 108 | + printf("%d", u->key); |
| 109 | + print_tree(u->rch); |
| 110 | + printf(")"); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +int main() |
| 115 | +{ |
| 116 | + char op[10]; |
| 117 | + int x; |
| 118 | + init(); |
| 119 | + while (true) |
| 120 | + { |
| 121 | + scanf("%s", op); |
| 122 | + if (strcmp(op, "insert") == 0) |
| 123 | + { |
| 124 | + scanf("%d", &x); |
| 125 | + insert(x); |
| 126 | + } |
| 127 | + else if (strcmp(op, "find") == 0) |
| 128 | + { |
| 129 | + scanf("%d", &x); |
| 130 | + printf("%d\n", find(x)); |
| 131 | + } |
| 132 | + else if (strcmp(op, "exit") == 0) |
| 133 | + { |
| 134 | + break; |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + printf("Command not found.\n"); |
| 139 | + } |
| 140 | + } |
| 141 | + return 0; |
| 142 | +} |
0 commit comments