C++| Segment Tree
\nclass node{\npublic:\n    int l,r,lz,k;\n    node* left; node* right;\n    node(int l,int r,int k,int lz){\n        this->l= l;this->r= r;this->k= k;this->lz=
```\nclass node{\npublic:\n    int l,r,lz,k;\n    node* left; node* right;\n    node(int l,int r,int k,int lz){\n        this->l= l;this->r= r;this->k= k;this->lz= lz;\n        left=right=NULL;\n    }\n};\nclass CountIntervals {\n    node* root;  int m = 0;\n    void find(node* root){\n        if(root->l<root->r){\n            int m = (root->l+root->r)/2;\n            int l = root->l, r = root->r;\n            if(root->left==NULL && root->right==NULL){\n                root->left = new node(l,m,root->lz*(m-l+1),root->lz);\n                root->right = new node(m+1,r,root->lz*(r-(m+1)+1),root->lz);\n            }\n            else if(root->lz>0){\n                root->left->k=root->lz*(root->left->r-root->left->l+1);\n                root->left->lz=root->lz; \n                \n                root->right->k=root->lz*(root->right->r-root->right->l+1);\n                root->right->lz=root->lz; \n            }\n        }\n    }\n    void update(node* root,int l,int h,int val){\n       if(l<=h){\n           int m = (root->l+root->r)/2;\n           if(root->r<l || root->l>h) return;\n           if(l <= root->l && root->r <= h){\n               root->k=val*(root->r-root->l+1); root->lz=val; return;\n           }\n           find(root);\n           update(root->left,l,h,val);\n           update(root->right,l,h,val);\n           root->k = root->left->k + root->right->k;\n       }\n    }\npublic:\n    CountIntervals() {\n        root = new node(0,1000000001,0,0);\n    }\n    \n    void add(int left, int right) {\n        update(root,left,right,1);\n    }\n    \n    int count() {\n        return root->k;\n    }\n};\n```
['Tree', 'C']