C++ using segment tree 🤭
c++\nstruct Node {\n    int left_, right_, cnt_;\n    Node *lc_, *rc_;\n    \n    Node (int left, int right, int cnt = 0) {\n        left_ = left;\n        righ
```c++\nstruct Node {\n    int left_, right_, cnt_;\n    Node *lc_, *rc_;\n    \n    Node (int left, int right, int cnt = 0) {\n        left_ = left;\n        right_ = right;\n        cnt_ = cnt;\n        lc_ = NULL;\n        rc_ = NULL;\n    }\n    \n    int cnt() {\n        return cnt_;\n    }\n    \n    int size() {\n        return right_ - left_ + 1;\n    }\n    \n    int mid() {\n        return (left_ + right_) >> 1;\n    }\n    \n    Node *lc() {\n        if (lc_ == NULL) {\n            lc_ = new Node(left_, mid());\n        }\n        return lc_;\n    }\n    \n    Node *rc() {\n        if (rc_ == NULL) {\n            rc_ = new Node(mid() + 1, right_);\n        }\n        return rc_;\n    }\n    \n    void insert(int left, int right) {\n        if (size() == cnt()) {\n            return;\n        }\n        if (left <= left_ && right >= right_) {\n            cnt_ = size();\n            return;\n        }\n        if (left <= mid()) {\n            lc()->insert(left, min(mid(), right));\n        }\n        if (right > mid()) {\n            rc()->insert(max(mid() + 1, left), right);\n        }\n        cnt_ = 0;\n        if (lc_ != NULL) {\n            cnt_ += lc_->cnt();\n        }\n        if (rc_ != NULL) {\n            cnt_ += rc_->cnt();\n        }\n    }\n};\n\nclass CountIntervals {\nprivate:\n    Node *root;\n    \npublic:\n    CountIntervals() {\n        root = new Node(1, 1E9);\n    }\n    \n    void add(int left, int right) {\n        root->insert(left, right);\n    }\n    \n    int count() {\n        return root->cnt();\n    }\n};\n\n/**\n * Your CountIntervals object will be instantiated and called as such:\n * CountIntervals* obj = new CountIntervals();\n * obj->add(left,right);\n * int param_2 = obj->count();\n */\n\n\n```
['Tree']