C++ segment tree
only need to insert the range\n1. if range is aleady there, early return\n2. add -> time complexity O(n*log( |ai| )), |ai| is the range of value domain, n is th
only need to insert the range\n1. if range is aleady there, early return\n2. add -> time complexity O(n*log( |ai| )), |ai| is the range of value domain, n is the # of add operations\n3. count ->  time = O(1) \n\n```\n\nstruct node{\n    int start, end, val;\n    node* left, *right;\n    node(int start, int end, int val) : start(start), end(end), val(val), left(nullptr), right(nullptr){\n        \n    }\n};\n\nclass CountIntervals {\n    node* root;\n    \n    void update(node* n, int start, int end){\n        if(n->start > end || n->end < start) \n            return;\n        \n        if(n->val == (  n->end - n->start + 1) ) return;\n        \n        if(n->start >= start && n->end <= end){\n            n->val = n->end - n->start +1;\n            return;\n        }\n        \n        int mid = (n->start + n->end)>>1;\n        if(!n->left){\n            n->left = new node(n->start, mid, 0);\n        }\n        if(!n->right){\n            n->right = new node(mid+1, n->end, 0);\n        }\n        update(n->left, start, end);\n        update(n->right, start, end);\n        n->val = n->left->val + n->right->val;\n    }\n    \npublic:\n    CountIntervals() {\n        root = new node(1, 1e9, 0);\n    }\n    \n    void add(int left, int right) {\n        update(root, left, right);\n    }\n    \n    int count() {\n        return root->val;\n    }\n};\n\n```
['Tree']