C++ Segment Tree Solution
I thought for this question my solution worth to be posted, I used segment tree to solved this problem, in the first glance is not possible to solve this proble
I thought for this question my solution worth to be posted, I used segment tree to solved this problem, in the first glance is not possible to solve this problem using regular segment tree as your segment is from 0 to 10^9. The point that we should consider is that we have only 10^5 queries so it is possible to make the regular segment tree using map(note that we can\'t have an array with size 10^9). After building our segment, our add and get function will be pretty straight forward, seg[v] equals the number of elements in subtree of v. Obviously the solution to each count is the number of elements in the root or seg[1]. Please ask me if you have any question about this solution.\n```\nclass CountIntervals {\npublic:\n    unordered_map<int, int> seg;\n    void update(int l, int r, int v = 1, int s = 0, int e = 1000000000)\n    {\n        if (l <= s && e <= r)\n        {\n            seg[v] = (e - s);\n            return;\n        }\n        if (r <= s || e <= l)\n        {\n            return;\n        }\n        if (seg[v] == e - s)\n            return;\n        int mid = (e + s) / 2;\n        int lc = 2 * v;\n        int rc = lc + 1;\n        update(l, r, lc, s, mid);\n        update(l, r, rc, mid, e);\n        seg[v] = seg[lc] + seg[rc];\n    }\n    CountIntervals() {\n        seg[0] = 0;\n    }\n    void add(int left, int right) {\n        update(left - 1, right);\n    }\n    \n    int count() {\n        return seg[1];\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 ```
['Tree']