Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CountIntervals {
- public:
- set<pair<int,int>> x;
- CountIntervals() {
- }
- void add(int left, int right) {
- int flag=0;
- auto it=x.begin();
- for(auto v:x)
- {
- if(v.first<=left&&left<=v.second&&v.second<right) {x.erase(it);left=v.first;flag=1;}
- else if(left<v.first&&v.first<=right&&right<=v.second) {x.erase(it);right=v.second;flag=1;}
- else if(left<v.first&&v.second<right) {x.erase(it);flag=1;}
- else if(v.first<=left&&right<=v.second) {x.erase(it);left=v.first;right=v.second;flag=1;}
- it++;
- }
- //cout<<flag<<endl;
- x.insert(make_pair(left,right));
- }
- int count() {
- int count=0;
- for(auto v:x)
- {
- count+=v.second-v.first+1;
- cout<<v.first<<" "<<v.second<<endl;
- }
- return count;
- }
- };
- /**
- * Your CountIntervals object will be instantiated and called as such:
- * CountIntervals* obj = new CountIntervals();
- * obj->add(left,right);
- * int param_2 = obj->count();
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement