1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| class SegmentTree { class Node { int left, right; boolean cover; int count; Node leftChild, rightChild;
Node(int left, int right) { this.left = left; this.right = right; this.count = 0; this.cover = false; } }
Node root;
void build(int left, int right) { root = new Node(left, right); build(root); }
void build(Node root) { int left = root.left; int right = root.right; if (right - left == 1) { return; } else if (right - left > 1) { int mid = (left + right) >> 1; Node leftNode = new Node(left, mid); Node rightNode = new Node(mid, right); root.leftChild = leftNode; root.rightChild = rightNode; build(leftNode); build(rightNode); } }
void insert(int left, int right) { insert(left, right, root); }
void insert(int left, int right, Node node) { if (node == null || left < node.left || right > node.right) { System.out.println("输入的参数不合法!" + "left:" + left + " " + "right:" + right); System.out.println("root:" + node.left + " " + node.right); return; } if (node.left == left && node.right == right) { node.count++; node.cover = true; return; } int mid = (node.left + node.right) >> 1; if (right <= mid) { insert(left, right, node.leftChild); } else if (left >= mid) { insert(left, right, node.rightChild); } else { insert(left, mid, node.leftChild); insert(mid, right, node.rightChild); } }
void delete(int left, int right) { delete(left, right, root); }
void delete(int left, int right, Node node) { if (node == null || left < node.left || right > node.right) { System.out.println("输入的参数不合法!"); return; } if (left == node.left && right == node.right) { node.count--; if (node.count == 0) { node.cover = false; } return; } int mid = (node.left + node.right) >> 1; if (right <= mid) { delete(left, right, node.leftChild); } else if (left >= mid) { delete(left, right, node.rightChild); } else { delete(left, mid, node.leftChild); delete(mid, right, node.rightChild); } }
void preOrder() { preOrder(root); }
void preOrder(Node root) { if (root.right - root.left == 1) { System.out.println("[" + root.left + "," + root.right + "]:" + root.count); return; } else if (root.right - root.left > 1) { System.out.println("[" + root.left + "," + root.right + "]:" + root.count); preOrder(root.leftChild); preOrder(root.rightChild); } }
int count() { return count(root); }
int count(Node node) { if (node.cover == true) { return node.right - node.left; } else { if (node.right - node.left == 1) { return 0; } else { return count(node.leftChild) + count(node.rightChild); } } } }
|