Advertisement
Kali_prasad

count Integer intervals

May 18th, 2022
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. class CountIntervals {
  2. public:
  3.     set<pair<int,int>> x;
  4.     CountIntervals() {
  5.        
  6.     }
  7.    
  8.     void add(int left, int right) {
  9.         int flag=0;
  10.         auto it=x.begin();
  11.         for(auto v:x)
  12.         {
  13.            
  14.             if(v.first<=left&&left<=v.second&&v.second<right)  {x.erase(it);left=v.first;flag=1;}
  15.             else if(left<v.first&&v.first<=right&&right<=v.second)  {x.erase(it);right=v.second;flag=1;}
  16.             else if(left<v.first&&v.second<right)  {x.erase(it);flag=1;}
  17.             else if(v.first<=left&&right<=v.second)  {x.erase(it);left=v.first;right=v.second;flag=1;}
  18.             it++;
  19.              
  20.         }
  21.        //cout<<flag<<endl;
  22.        
  23.         x.insert(make_pair(left,right));
  24.        
  25.     }
  26.    
  27.     int count() {
  28.        int count=0;
  29.         for(auto v:x)
  30.         {
  31.            
  32.             count+=v.second-v.first+1;
  33.             cout<<v.first<<" "<<v.second<<endl;
  34.         }
  35.         return count;
  36.     }
  37. };
  38.  
  39. /**
  40.  * Your CountIntervals object will be instantiated and called as such:
  41.  * CountIntervals* obj = new CountIntervals();
  42.  * obj->add(left,right);
  43.  * int param_2 = obj->count();
  44.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement