动态线段树
class CountIntervals { Node root; public CountIntervals() { root = new Node(1, (int)1e9); } public void add(int left, int right) { root.update(root, left, right); } public int count() { return root.val; } } class Node { int left; int right; int val; int mark; Node leftNode; Node rightNode; public Node(int l, int r) { left = l; right = r; } public Node getLeftNode() { if (leftNode == null) { leftNode = new Node(left, left + (right - left) / 2); } return leftNode; } public Node getRightNode() { if (rightNode == null) { rightNode = new Node(left + (right - left) / 2 + 1, right); } return rightNode; } public void update(Node node, int lo, int hi) { if (node.left > hi || node.right < lo) { return; } if (node.left >= lo && node.right <= hi) { node.mark = 1; } else { update(node.getLeftNode(), lo, hi); update(node.getRightNode(), lo, hi); } if (node.mark > 0) { node.val = node.right - node.left + 1; } else { node.val = node.getLeftNode().val + node.getRightNode().val; } } }