Dynamic Segment Tree || C++ || Fully Commented Code
Code\n\nclass node{\n    public:\n    int val, lazy;\n    node *left, *right;\n    node() {\n        val = 0, lazy = 0;\n        left = right = NULL;\n    }\n};
# Code\n```\nclass node{\n    public:\n    int val, lazy;\n    node *left, *right;\n    node() {\n        val = 0, lazy = 0;\n        left = right = NULL;\n    }\n};\n\nclass ST{\n    public:\n    node *root = new node();\n\n    node* getRoot() {\n        return root;\n    }\n\n    void update(node *root, int low, int high, int l, int r) {\n        // non overlap -> low high l r, l r low high\n        if(high < l || r < low)return;\n\n        if(!root->left) {\n            root->left = new node();\n            root->right = new node();\n        }\n\n        //update current node and propagate laziness to the child nodes\n        if(root->lazy) {\n            root->val = high - low + 1;\n            if(low != high) {\n                root->left->lazy = 1;\n                root->right->lazy = 1;\n            }\n            root->lazy = 0;\n        }\n\n        // all ranges are covered then return \n        if(root->val == high - low + 1)return;\n\n        // comp overlap -> l low high r\n        // assign value (high-low+1) and propagate laziness to the child nodes \n        if(low >= l && high <= r) {\n            root->val = high - low + 1;\n            if(low != high) {\n                root->left->lazy = 1;\n                root->right->lazy = 1;\n            }\n            root->lazy = 0;\n            return;\n        }\n\n        int mid = low + (high - low) / 2;\n        update(root->left, low, mid, l, r);\n        update(root->right, mid+1, high, l, r);\n        // update node range from its child nodes\' ranges\n        root->val = root->left->val + root->right->val;\n    }\n};\n\nclass CountIntervals {\npublic:\n    ST obj;\n    node *root = obj.getRoot();\n\n    CountIntervals() {\n    }\n    \n    void add(int left, int right) {\n        obj.update(root, 0, 1e9, left, right);\n    }\n    \n    int count() {\n        return root->val;\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```
['Segment Tree', 'C++']