Advertisement
thewitchking

Untitled

Dec 14th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. class RangeModule {
  2.  
  3.     TreeMap<Integer,Integer> map;
  4.     public RangeModule() {
  5.         map = new TreeMap<>();
  6.     }
  7.    
  8.     public void addRange(int left, int right) {
  9.         var l = map.floorEntry(left);
  10.         var r = map.floorEntry(right);
  11.  
  12.         if(l!=null && l.getValue()>=left) left=l.getKey();
  13.         if(r!=null && r.getValue()>right) right=r.getValue();
  14.  
  15.         map.subMap(left,right).clear();
  16.         map.put(left,right);
  17.     }
  18.    
  19.     public boolean queryRange(int left, int right) {
  20.         var l=map.floorEntry(left);
  21.         return l!=null && l.getValue()>=right;
  22.     }
  23.    
  24.     public void removeRange(int left, int right) {
  25.         var l = map.floorEntry(left);
  26.         var r = map.floorEntry(right);
  27.  
  28.         if (l != null && l.getValue() > left) map.put(l.getKey(), left);
  29.         if (r != null && r.getValue() > right) map.put(right, r.getValue());
  30.  
  31.         map.subMap(left, right).clear();
  32.     }
  33. }
  34.  
  35. /**
  36.  * Your RangeModule object will be instantiated and called as such:
  37.  * RangeModule obj = new RangeModule();
  38.  * obj.addRange(left,right);
  39.  * boolean param_2 = obj.queryRange(left,right);
  40.  * obj.removeRange(left,right);
  41.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement