Segment tree + lazy propagation 
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```javascript []
class Node {
  constructor(lx = 0, rx = 0) {
    this.count = 0;
    this.lazy = 0;
    this.isLazy = false;
    this.left = null;
    this.right = null;
    this.lx = lx;
    this.rx = rx;
  }
}

class SegmentTree {

  constructor() {
    this.root = new Node(1, 1e9 + 10);
  }

  query() {
    return this.root.count;
  }

  propagate(node) {

    if (!node.isLazy) 
        return;

    node.count = (node.rx - node.lx) * node.lazy;

    if (node.rx - node.lx > 1) {

      if (!node.left) 
          node.left = new Node(node.lx, (node.lx + node.rx) >> 1);

      if (!node.right) 
          node.right = new Node((node.lx + node.rx) >> 1, node.rx);

      node.left.lazy = node.lazy;
      node.right.lazy = node.lazy;

      node.left.isLazy = true;
      node.right.isLazy = true;

    }

    node.isLazy = false;
    node.lazy = 0;

  }

  update(l, r, val, node = this.root) {

    if (!node) 
        return;

    this.propagate(node);

    if (l >= node.rx || r <= node.lx) 
        return;

    if (l <= node.lx && r >= node.rx) {
      node.lazy = val;
      node.isLazy = true;
      this.propagate(node);
      return;
    }

    let m = (node.lx + node.rx) >> 1;

    if (!node.left) 
        node.left = new Node(node.lx, m);

    if (!node.right) 
        node.right = new Node(m, node.rx);

    this.update(l, r, val, node.left);
    this.update(l, r, val, node.right);

    node.count = node.left.count + node.right.count;
      
  }
}

class CountIntervals {
  constructor() {
    this.segTree = new SegmentTree();
  }

  add(left, right) {
    this.segTree.update(left, right + 1, 1);
  }

  count() {
    return this.segTree.query();
  }
}
```
['Segment Tree', 'JavaScript']