Advertisement
Kali_prasad

count numbers present in intervals

May 15th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 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.             it++;
  18.              
  19.         }
  20.        cout<<flag<<endl;
  21.         x.insert(make_pair(left,right));
  22.        
  23.     }
  24.    
  25.     int count() {
  26.        int count=0;
  27.         for(auto v:x)
  28.         {
  29.            
  30.             count+=v.second-v.first+1;
  31.         }
  32.         return count;
  33.     }
  34. };
  35.  
  36. /**
  37.  * Your CountIntervals object will be instantiated and called as such:
  38.  * CountIntervals* obj = new CountIntervals();
  39.  * obj->add(left,right);
  40.  * int param_2 = obj->count();
  41.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement